/*
å¦çæ绩管çç¨åº
ç¼å¶ä¸ä¸ªç»è®¡å¦çèè¯åæ°ç管çç¨åºã
设å¦çæ绩已以ä¸ä¸ªå¦çä¸ä¸ªè®°å½çå½¢å¼åå¨å¨æ件ä¸ï¼
æ¯ä½å¦çè®°å½å
å«çä¿¡æ¯æï¼å§åï¼å¦å·ååé¨å课çæ绩ã
ç¨åºå
·æ以ä¸å 项åè½ï¼æ±åºåé¨è¯¾ç¨çæ»åï¼å¹³ååï¼æå§åï¼
æå¦å·å¯»æ¾å
¶è®°å½å¹¶æ¾ç¤ºï¼æµè§å
¨é¨å¦çæ绩åææ»åç±é«å°ä½æ¾ç¤ºå¦çä¿¡æ¯çã
*/
#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;
}
}
}
温馨提示:内容为网友见解,仅供参考