永发信息网

求一个C语言的的学生成绩管理, 大家帮帮我吧!

答案:6  悬赏:0  手机版
解决时间 2021-11-12 16:17
  • 提问者网友:沉默菋噵
  • 2021-11-12 04:57
求一个C语言的的学生成绩管理, 大家帮帮我吧!
最佳答案
  • 五星知识达人网友:千杯敬自由
  • 2021-11-12 05:19
#include
#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;ifscanf(fpt,"%d",&rpt->marks[i]);
for(rpt->total=0,i=0;irpt->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;ifprintf(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;iprintf(" %-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;itotal[i]=0;
count=0;
while(readrecord(fp,&s)!=0)
{
for(i=0;itotal[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{
printf("Input the student's name: ");
scanf("%s",&s.name);
printf("Input the student's code: ");
scanf("%s",&s.code);
for(j=0;j{
printf("Input the %s mark: ",schoolwork[j]);
scanf("%d",&s.marks[j]);
}
writerecord(fp,&s);
}
fclose(fp);
}
}
fclose(fp);
getchar();

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;
}
switch(c)
{
case 'm':
case 'M':
if((n=totalmark(stuf))==0)
{
puts("Error!");
break;
}
printf("\n");
for(i=0;iprintf("%-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;iprintf("%-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;
}
}
}参考资料:baidu
全部回答
  • 1楼网友:一袍清酒付
  • 2021-11-12 09:59
楼上编的是什么`!!是我眼花还是~~~~东拉西撤的弄过来一段代码!???
  • 2楼网友:走死在岁月里
  • 2021-11-12 09:15
那难的,这里没几个像你那么有文化的,你还是去找书吧
  • 3楼网友:第幾種人
  • 2021-11-12 08:33
呵呵,有点浩大
  • 4楼网友:夜余生
  • 2021-11-12 06:56
老兄,这不可能是几天能完成的.即使顺利也要相当长的一段时间,你的忙也太大了点,我有心单位也不可能让我闲那么久!
  • 5楼网友:山君与见山
  • 2021-11-12 06:37
....工程浩大啊 ~~
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯