學生成績管理程序
編制壹個統計學生考試分數的管理程序。
設學生成績已以壹個學生壹個記錄的形式存儲在文件中,
每位學生記錄包含的信息有:姓名,學號和各門功課的成績。
程序具有以下幾項功能:求出各門課程的總分,平均分,按姓名,
按學號尋找其記錄並顯示,瀏覽全部學生成績和按總分由高到低顯示學生信息等。
*/
#include <stdio.h>
#define SWN 3 /* 課程數 */
#define NAMELEN 20 /* 姓名最大字符數 */
#define CODELEN 10 /* 學號最大字符數 */
#define FNAMELEN 80 /* 文件名最大字符數 */
#define BUFLEN 80 /* 緩沖區最大字符數 */
/* 課程名稱表 */
char schoolwork[SWN][NAMELEN+1] = {"Chinese","Mathematic","English"};
struct record
{
char name[NAMELEN+1]; /* 姓名 */
char code[CODELEN+1]; /* 學號 */
int marks[SWN]; /* 各課程成績 */
int total; /* 總分 */
}stu;
struct node
{
char name[NAMELEN+1]; /* 姓名 */
char code[CODELEN+1]; /* 學號 */
int marks[SWN]; /* 各課程成績 */
int total; /* 總分 */
struct node *next; /* 後續表元指針 */
}*head; /* 鏈表首指針 */
int total[SWN]; /* 各課程總分 */
FILE *stfpt; /* 文件指針 */
char stuf[FNAMELEN]; /* 文件名 */
/* 從指定文件讀入壹個記錄 */
int readrecord(FILE *fpt,struct record *rpt)
{
char buf[BUFLEN];
int i;
if(fscanf(fpt,"%s",buf)!=1)
return 0; /* 文件結束 */
strncpy(rpt->name,buf,NAMELEN);
fscanf(fpt,"%s",buf);
strncpy(rpt->code,buf,CODELEN);
for(i=0;i<SWN;i++)
fscanf(fpt,"%d",&rpt->marks[i]);
for(rpt->total=0,i=0;i<SWN;i++)
rpt->total+=rpt->marks[i];
return 1;
}
/* 對指定文件寫入壹個記錄 */
writerecord(FILE *fpt,struct record *rpt)
{
int i;
fprintf(fpt,"%s\n",rpt->name);
fprintf(fpt,"%s\n",rpt->code);
for(i=0;i<SWN;i++)
fprintf(fpt,"%d\n",rpt->marks[i]);
return ;
}
/* 顯示學生記錄 */
displaystu(struct record *rpt)
{
int i;
printf("\nName : %s\n",rpt->name);
printf("Code : %s\n",rpt->code);
printf("Marks :\n");
for(i=0;i<SWN;i++)
printf(" %-15s : %4d\n",schoolwork[i],rpt->marks[i]);
printf("Total : %4d\n",rpt->total);
}
/* 計算各單科總分 */
int totalmark(char *fname)
{
FILE *fp;
struct record s;
int count,i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
for(i=0;i<SWN;i++)
total[i]=0;
count=0;
while(readrecord(fp,&s)!=0)
{
for(i=0;i<SWN;i++)
total[i]+=s.marks[i];
count++;
}
fclose(fp);
return count; /* 返回記錄數 */
}
/* 列表顯示學生信息 */
void liststu(char *fname)
{
FILE *fp;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return ;
}
while(readrecord(fp,&s)!=0)
{
displaystu(&s);
printf("\n Press ENTER to continue...\n");
while(getchar()!='\n');
}
fclose(fp);
return;
}
/* 構造鏈表 */
struct node *makelist(char *fname)
{
FILE *fp;
struct record s;
struct node *p,*u,*v,*h;
int i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return NULL;
}
h=NULL;
p=(struct node *)malloc(sizeof(struct node));
while(readrecord(fp,(struct record *)p)!=0)
{
v=h;
while(v&&p->total<=v->total)
{
u=v;
v=v->next;
}
if(v==h)
h=p;
else
u->next=p;
p->next=v;
p=(struct node *)malloc(sizeof(struct node));
}
free(p);
fclose(fp);
return h;
}
/* 順序顯示鏈表各表元 */
void displaylist(struct node *h)
{
while(h!=NULL)
{
displaystu((struct record *)h);
printf("\n Press ENTER to continue...\n");
while(getchar()!='\n');
h=h->next;
}
return;
}
/* 按學生姓名查找學生記錄 */
int retrievebyn(char *fname, char *key)
{
FILE *fp;
int c;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(readrecord(fp,&s)!=0)
{
if(strcmp(s.name,key)==0)
{
displaystu(&s);
c++;
}
}
fclose(fp);
if(c==0)
printf("The student %s is not in the file %s.\n",key,fname);
return 1;
}
/* 按學生學號查找學生記錄 */
int retrievebyc(char *fname, char *key)
{
FILE *fp;
int c;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(readrecord(fp,&s)!=0)
{
if(strcmp(s.code,key)==0)
{
displaystu(&s);
c++;
break;
}
}
fclose(fp);
if(c==0)
printf("The student %s is not in the file %s.\n",key,fname);
return 1;
}
main()
{
int i,j,n;
char c;
char buf[BUFLEN];
FILE *fp;
struct record s;
clrscr();
printf("Please input the students marks record file's name: ");
scanf("%s",stuf);
if((fp=fopen(stuf,"r"))==NULL)
{
printf("The file %s doesn't exit, do you want to creat it? (Y/N) ",stuf);
getchar();
c=getchar();
if(c=='Y'||c=='y')
{
fp=fopen(stuf,"w");
printf("Please input the record number you want to write to the file: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input the student's name: ");
scanf("%s",&s.name);
printf("Input the student's code: ");
scanf("%s",&s.code);
for(j=0;j<SWN;j++)
{
printf("Input the %s mark: ",schoolwork[j]);
scanf("%d",&s.marks[j]);
}
writerecord(fp,&s);
}
fclose(fp);
}
}
fclose(fp);
getchar();
/*clrscr();*/
puts("Now you can input a command to manage the records.");
puts("m : mean of the marks.");
puts("t : total of the marks.");
puts("n : search record by student's name.");
puts("c : search record by student's code.");
puts("l : list all the records.");
puts("s : sort and list the records by the total.");
puts("q : quit!");
while(1)
{
puts("Please input command:");
scanf(" %c",&c); /* 輸入選擇命令 */
if(c=='q'||c=='Q')
{
puts("\n Thank you for your using.");
break; /* q,結束程序運行 */
}
switch(c)
{
case 'm': /* 計算平均分 */
case 'M':
if((n=totalmark(stuf))==0)
{
puts("Error!");
break;
}
printf("\n");
for(i=0;i<SWN;i++)
printf("%-15s's average is: %.2f.\n",schoolwork[i],(float)total[i]/n);
break;
case 't': /* 計算總分 */
case 'T':
if((n=totalmark(stuf))==0)
{
puts("Error!");
break;
}
printf("\n");
for(i=0;i<SWN;i++)
printf("%-15s's total mark is: %d.\n",schoolwork[i],total[i]);
break;
case 'n': /* 按學生的姓名尋找記錄 */
case 'N':
printf("Please input the student's name you want to search: ");
scanf("%s",buf);
retrievebyn(stuf,buf);
break;
case 'c': /* 按學生的學號尋找記錄 */
case 'C':
printf("Please input the student's code you want to search: ");
scanf("%s",buf);
retrievebyc(stuf,buf);
break;
case 'l': /* 列出所有學生記錄 */
case 'L':
liststu(stuf);
break;
case 's': /* 按總分從高到低排列顯示 */
case 'S':
if((head=makelist(stuf))!=NULL)
displaylist(head);
break;
default: break;
}
}
}