//┃ C語言 學生信息管理系統 v1.0.0 beta版 ┃
//┃ 東北大學秦皇島分校 ┃
//┃ 2007-9-6 ┃
//┃ 版權沒有 盜版不究 ┃
//┃ 環境:Dev C++ Windows XP SP2 調試通過 ┃
//┗━━━━━━━━━━━━━━━━━━━━━┛
//包含的頭文件部分
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
//定義學生數據結構體
struct sctStudent
{
int iNum;//學號
char cName[10];//姓名
float fLine;//線代成績
float fEnglish;//英語成績
float fMaths;//高數成績
float fComputer;//計導成績
float fScore;//總成績
struct sctStudent *next;
};
//函數聲明部分
void vNewPause(void);
void vOutCaption(void);
void vOutSpace(int);
void vOutTableLineA(int);
void vOutTableHead(void);
void vShowLink(struct sctStudent *);
int iFindLink(struct sctStudent *);
int iMainMenu(void);
int iSaveLink(struct sctStudent *);
struct sctStudent *psctDeleteLink(struct sctStudent *);
struct sctStudent *psctGreatLink(void);
struct sctStudent *psctIndexLink(struct sctStudent *,int);
struct sctStudent *psctLoadLink(void);
struct sctStudent *psctSortLink(struct sctStudent *);
//清空輸入流並無回顯暫停等待回車
void vNewPause(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF )
clearerr(stdin);
getchar();
}
//該函數用於輸出n個空格
void vOutSpace(int n)
{
int i;
for(i=1;i<=n;i++)printf(" ");
}
//輸出首行或尾行制表符
void vOutTableLineA(int Judge)
{
int i;
if(Judge==0)printf(" ┏");
else printf(" ┗");
for(i=1;i<=37;i++)
{
if((i%5==0)&&(i!=35))
{
if(Judge==0)printf("┳");
if(Judge==1)printf("┻");
}
else printf("━");
};
if(Judge==0)printf("┓\n");
else printf("┛\n");
}
//該函數用於輸出格式頭
void vOutTableHead(void)
{
vOutTableLineA(0);
printf(" ┃學號 ┃姓名 ┃線代 ┃英語 ┃高數 ┃計導 ┃總分 ┃");
}
//該函數用於輸出程序標題
void vOutCaption(void)
{
int i;
//以下用制表符制表
printf("┏");
for(i=1;i<=38;i++)printf("━");
printf("┓┃");
vOutSpace(26);
printf("學 生 成 績 管 理 系 統 v1.0.0");
vOutSpace(20);
printf("┃");
printf("┗");
for(i=1;i<=38;i++)printf("━");
printf("┛");
}
//該函數用於繪制主界面
int iMainMenu(void)
{
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(32);
printf("1.輸入學生數據 \n");
vOutSpace(32);
printf("2.查詢學生數據\n");
vOutSpace(32);
printf("3.統計學生數據\n");
vOutSpace(32);
printf("4.排序學生數據\n");
vOutSpace(32);
printf("5.刪除學生數據\n");
vOutSpace(32);
printf("6.存儲學生數據\n");
vOutSpace(32);
printf("7.讀取學生數據\n");
vOutSpace(32);
printf("0.退出本次實例\n");
printf("\n\n\n");
vOutSpace(31);
printf("請輸入您的選項:");
int temp;
fflush(stdin);
scanf("%d",&temp);
return (temp);
}
//該函數用於建立單向鏈表並賦值
struct sctStudent *psctGreatLink(void)
{
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(26);
printf("請輸入學生成績,以學號0結束\n\n\n");
vOutSpace(32);
struct sctStudent *head,*temp1,*temp2,*temp3;
head=NULL;
temp1=(struct sctStudent *)malloc(sizeof(struct sctStudent));
printf("請輸入學生學號:");
fflush(stdin);
scanf("%d",&temp1->iNum);
if(temp1->iNum!=0)
{
vOutSpace(32);
printf("請輸入學生姓名:");
scanf("%s",temp1->cName);
vOutSpace(32);
printf("請輸入線代成績:");
scanf("%f",&temp1->fLine);
vOutSpace(32);
printf("請輸入英語成績:");
scanf("%f",&temp1->fEnglish);
vOutSpace(32);
printf("請輸入高數成績:");
scanf("%f",&temp1->fMaths);
vOutSpace(32);
printf("請輸入計導成績:");
scanf("%f",&temp1->fComputer);
temp1->fScore=temp1->fLine+temp1->fEnglish+temp1->fMaths+temp1->fComputer;
while(temp1->iNum!=0)
{
if(head==NULL)head=temp1;
temp2=(struct sctStudent *)malloc(sizeof(struct sctStudent));
temp1->next=temp2;//前壹節點的後繼指針指向新開辟的節點
temp3=temp1;//保留住前壹節點的指針
temp1=temp2;//方便後面的循環
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(26);
printf("請輸入學生成績,以學號0結束\n\n\n");
vOutSpace(32);
printf("請輸入學生學號:");
scanf("%d",&temp1->iNum);
if(temp1->iNum!=0)
{
vOutSpace(32);
printf("請輸入學生姓名:");
scanf("%s",temp1->cName);
vOutSpace(32);
printf("請輸入線代成績:");
scanf("%f",&temp1->fLine);
vOutSpace(32);
printf("請輸入英語成績:");
scanf("%f",&temp1->fEnglish);
vOutSpace(32);
printf("請輸入高數成績:");
scanf("%f",&temp1->fMaths);
vOutSpace(32);
printf("請輸入計導成績:");
scanf("%f",&temp1->fComputer);
temp1->fScore=temp1->fLine+temp1->fEnglish+temp1->fMaths+temp1->fComputer;
}
else
{
temp3->next=NULL;//前節點的後繼指針為空 ,鏈表結束於前節點
}
}
head=psctIndexLink(head,0);//直接對輸入鏈表進行排序
}
free(temp1);//釋放新開辟的未用節點,節省內存開銷
return head;
}
//該函數用於查找指定學號的學生成績
int iFindLink(struct sctStudent *temp)
{
int iSearchNum;
system("cls");
vOutCaption();
printf("\n\n");
if(temp!=NULL)
{
vOutSpace(23);
printf("請在下面輸入學生學號,輸入0返回\n\n\n");
vOutSpace(32);
printf("請輸入查詢學號:");
fflush(stdin);
scanf("%d",&iSearchNum);
while((temp->iNum!=iSearchNum)&&(temp->next!=NULL))
{
temp=(temp->next);
}
if(temp->iNum==iSearchNum)
{
system("cls");
vOutCaption();
printf("\n\n");
vOutTableHead();
printf("\n ");
printf("┃%-4d ┃",temp->iNum);
printf("%-6.6s ┃",temp->cName);
printf("%-4.1f ┃",temp->fLine);
printf("%-4.1f ┃",temp->fEnglish);
printf("%-4.1f ┃",temp->fMaths);
printf("%-4.1f ┃",temp->fComputer);
printf("%-3.2f ┃",temp->fScore);
printf("\n");
vOutTableLineA(1);
printf("\n\n\n");
vOutSpace(34);
printf("按回車鍵返回");
vNewPause();
return 0;
}
else
{
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(23);
printf("未能找到該筆數據,按回車鍵返回\n");
vOutSpace(23);
vNewPause();
return 1;
}
}
else
{
vOutSpace(23);
printf("尚未創建或讀取列表,按回車鍵返回");
vNewPause();
return 1;
}
}
//用於向屏幕輸出顯示鏈表
void vShowLink(struct sctStudent *temp)
{
system("cls");
vOutCaption();
printf("\n\n");
vOutTableHead();
printf("\n");
while(temp!=NULL)
{
printf(" ┃%-4d ┃",temp->iNum);
printf("%-6.6s ┃",temp->cName);
printf("%-4.1f ┃",temp->fLine);
printf("%-4.1f ┃",temp->fEnglish);
printf("%-4.1f ┃",temp->fMaths);
printf("%-4.1f ┃",temp->fComputer);
printf("%-3.2f ┃",temp->fScore);
printf("\n");
temp=temp->next;
}
vOutTableLineA(1);
printf("\n\n\n");
vOutSpace(34);
printf("按回車鍵返回");
vNewPause();
}
//該函數用於統計學生成績並顯示之
struct sctStudent *psctIndexLink(struct sctStudent *h,int iJudge)
{
struct sctStudent *endpt,*u,*v,*p;
u=(struct sctStudent *)malloc(sizeof(struct sctStudent));
u->next=h;
h=u;
for(endpt=NULL;endpt!=h;endpt=p)
for(p=u=h;u->next->next!=endpt;u=u->next)
if(u->next->iNum > u->next->next->iNum)
{
v= u->next->next;
u->next->next=v->next;
v->next=u->next;
u->next=v;
p=u->next->next;
}
u=h;
h=h->next;
free(u);
if(iJudge==1)vShowLink(h) ;
return (h);
}
//該函數用於排序學生成績 根據單向鏈表的特點 采用冒泡法
struct sctStudent *psctSortLink(struct sctStudent *h)
{
struct sctStudent *endpt,*u,*v,*p;
u =(struct sctStudent*)malloc(sizeof(struct sctStudent));
u->next=h;
h = u;
for(endpt=NULL;endpt!=h;endpt=p)
for(p=u=h;u->next->next!=endpt;u=u->next)
if(u->next->fScore < u->next->next->fScore)
{ /* 兩相鄰結點比較 */
v = u->next->next;
u->next->next = v->next;
v->next = u->next;
u->next = v;
p = u->next->next;
}
u = h;
h = h->next;
free(u);
vShowLink(h);
return h;
}
//該函數用於刪除學生成績
struct sctStudent *psctDeleteLink(struct sctStudent *temp)
{
struct sctStudent *psctSaveHead=temp,*psctNewTemp;
int iSearchNum;
char cJudge;
system("cls");
vOutCaption();
printf("\n\n");
if(temp!=NULL)
{
vOutSpace(23);
printf("請在下面輸入學生學號,輸入0返回\n\n\n");
vOutSpace(32);
printf("請輸入刪除的學號:");
fflush(stdin);
scanf("%d",&iSearchNum);
if(iSearchNum==0)return (temp);
while((temp->iNum!=iSearchNum)&&(temp->next!=NULL))
{
psctNewTemp=temp;
temp=(temp->next);
}
if(temp->iNum==iSearchNum)
{
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(4);
printf("您要刪除的數據是:\n");
vOutTableHead();
printf("\n ");
printf("┃%-4d ┃",temp->iNum);
printf("%-6.6s ┃",temp->cName);
printf("%-4.1f ┃",temp->fLine);
printf("%-4.1f ┃",temp->fEnglish);
printf("%-4.1f ┃",temp->fMaths);
printf("%-4.1f ┃",temp->fComputer);
printf("%-3.2f ┃",temp->fScore);
printf("\n");
vOutTableLineA(1);
printf("\n\n\n");
vOutSpace(34);
printf("確定刪除(y/n)?");
fflush(stdin);
scanf("%c",&cJudge);
if(cJudge=='y'||cJudge=='Y')
{
if(temp==psctSaveHead)
{
temp=psctSaveHead;
psctSaveHead=(psctSaveHead->next);
free(temp);
}
else
if(temp->next==NULL)
{
psctNewTemp->next=NULL;
free(temp);
}
else
{
psctNewTemp->next=temp->next;
free(temp);
}
return (psctSaveHead);
}
else
return (psctSaveHead);
}
else
{
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(23);
printf("未能找到該筆數據,按回車鍵返回\n");
vOutSpace(23);
vNewPause();
return (psctSaveHead);
}
}
else
{
vOutSpace(23);
printf("尚未創建或讀取列表,按回車鍵返回");
vNewPause();
return (psctSaveHead);
}
}
//該函數用於存儲學生成績到文件
int iSaveLink(struct sctStudent *temp)
{
FILE *pfSaveFile;
char cDir[100];
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(26);
printf("請輸入保存文件的路徑:\n");
vOutSpace(26);
scanf("%s",cDir);
if(strcmp(cDir,"0")==0)return (0);
if((pfSaveFile=fopen(cDir,"wt"))==NULL)
{
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(26);
printf("無法打開該文件!\n");
}
else
{
while(temp!=NULL)
{
fprintf(pfSaveFile,"%d %s %f %f %f %f %f\n",
temp->iNum,
temp->cName,
temp->fLine,
temp->fEnglish,
temp->fMaths,
temp->fComputer,
temp->fScore);
temp=temp->next;
}
fclose(pfSaveFile);
}
}
//該函數用於從文件讀取學生成績
struct sctStudent *psctLoadLink(void)
{
FILE *pfLoadFile;
char cDir[100];
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(26);
printf("請輸入需要讀取文件的路徑:\n");
vOutSpace(26);
scanf("%s",cDir);
if(strcmp(cDir,"0")==0)return (0);
if((pfLoadFile=fopen(cDir,"rt"))==NULL)
{
system("cls");
vOutCaption();
printf("\n\n");
vOutSpace(26);
printf("無法打開該文件!\n");
vOutSpace(26);
printf("請按回車鍵返回!");
vNewPause();
}
else
{
struct sctStudent *head,*temp1,*temp2;
head=NULL;
if(!feof(pfLoadFile))
{
temp1=(struct sctStudent *)malloc(sizeof(struct sctStudent));
fscanf(pfLoadFile,"%d %s %f %f %f %f %f\n",
&(temp1->iNum),
(temp1->cName),
&(temp1->fLine),
&(temp1->fEnglish),
&(temp1->fMaths),
&(temp1->fComputer),
&(temp1->fScore));
while(!feof(pfLoadFile))
{
if(head==NULL)head=temp1;
temp2=(struct sctStudent *)malloc(sizeof(struct sctStudent));
temp1->next=temp2;//前壹節點的後繼指針指向新開辟的節點
temp1=temp2;//方便後面的循環
fscanf(pfLoadFile,"%d %s %f %f %f %f %f\n",
&(temp1->iNum),
(temp1->cName),
&(temp1->fLine),
&(temp1->fEnglish),
&(temp1->fMaths),
&(temp1->fComputer),
&(temp1->fScore));
}
temp1->next=NULL;
fclose(pfLoadFile);
return head;
}
else
{
fclose(pfLoadFile);
return NULL;
}
}
}
//程序總入口點
int main(void)
{
int iJudge=1,t=1;
struct sctStudent *temp;
temp=NULL;
while(1)
{
switch(iMainMenu())
{
case 1:
temp=psctGreatLink();break;
case 2:
iFindLink(temp);break;
case 3:
temp=psctIndexLink(temp,1);break;
case 4:
temp=psctSortLink(temp);break;
case 5:
temp=psctDeleteLink(temp);break;
case 6:
iSaveLink(temp);break;
case 7:
temp=psctLoadLink();break;
case 0:
exit(0);
default:
break;
}
}
system("pause");
return 0;
}