求大神看看后边的控制函数为啥是非法的
#include<stdio.h>#include <stdlib.h>
#include <string.h>
#define HEADER1 "------------STUDENT------------\n"
#define HEADER2 "|number|name|Comp|Math|Eng|sum|ave|mici\n"
#define HEADER3 "|-----|----|----|----|---|---|---|---|"
#define FORMAT "%-10s|%-15s|%4d|%4d|%4d|%4d|%.2f|%4d|\n"
#define DATA p->data.num,p->data.name,p->data.cgrade,p->data.total,p->data.cgrade,p->data.mgrade,p->data.egrade,p->data.total,p->data.ave,p->data.mingci
#define END "---------------------------------------------------\n"
int saveflag=0;
struct student
{
char num[10];
char name[15];
int cgrade;
int mgrade;
int egrade;
int total;
float ave;
int mingci;
};
typedef struct node
{
struct student data;
struct node *next;
}Node,*Link;
void menu()
{
system("cls");
printf("The Students Grade Management System \n\n ");
printf("***********Menu************************\n");
printf("* 1 input record 2 delete record*\n");
printf("* 3 input record 4 delete record*\n");
printf("* 5 input record 6 delete record*\n");
printf("* 7 input record 8 delete record*\n");
printf("* 9 input record 0 delete record*\n");
printf("****************************************\n");
}
void printheader()
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(Node *pp)
{
Node *p;
p=pp;
printf(FORMAT,DATA);
}
void Wrong()
{
printf("\n\n\n\n\n*****Error:input has wrong!press any key to continue*****\n");
getchar();
}
void Nofind()
{
printf("\n=====>Not find this student! \n");
}
void Disp(Link l)
{
Node *p;
p=l->next;
if(!p)
{
printf("\n=====>Not student record! \n");
getchar();
return;
}
printf("\n\n");
printheader();
while(p)
{
printdata(p);
p=p->next;
printf(HEADER3);
}
getchar();
getchar();
printf("\n");
}
Node *Locateofnum(Link l,char findmess[])
{
Node *r;
r=l->next;
while(r)
{
if(strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
return 0;
}
void Locateofname(Link l,char findmess[],Node *n[])
{
Node *r;
int i=0;
r=l->next;
while(r)
{
if(strcmp(r->data.num,findmess)==0)
n[i++]=r;
r=r->next;
}
}
void stringinput(char *t,int lens,char *notice)
{
char n[225];
printf(notice);
scanf("%s",n);
while(strlen(n)> lens)
{ printf("\n exceed the required length!\n");
printf(notice);
scanf("%s",n);
}
strcpy(t,n);
}
int numberinput(char *notice)
{
int t=0;
printf(notice);
scanf("%d",&t);
while(t>100||t<0)
{ printf("\n score must in [0,100]!\n");
printf(notice);
scanf("%d",&t);
}
return t;
}
void Add(Link l)
{
Node *p,*r,*s;
char ch,flag=0,num[10];
r=l;
s=l->next;
system("cls");
Disp(l);
while(r->next!=NULL)
r=r->next;
while(l)
{
while(l)
{
stringinput(num,10,"input number (press'0'return menu):");
flag=0;
if(strcmp(num,"0")==0)
{return;}
s=l->next;
while(s)
{
if(strcmp(s->data.num,num)==0)
{
flag=1;
break;
}
s=s->next;
}
if(flag==1)
{
getchar();
printf("=====>Thenumber %s isexisting,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return;
}
else
{break;}
}
p=(Node*)malloc(sizeof(Node));
if(!p)
{
printf("\n allocate memory failure");
return;
}
strcpy(p->data.num,num);
stringinput(p->data.name,15,"Name:");
p->data.cgrade=numberinput("C language Score[0~100]:");
p->data.mgrade=numberinput("Math Score[0~100]:");
p->data.egrade=numberinput("English Score[0~100]:");
p->data.total=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=(float)(p->data.total/3);
p->data.mingci=0;
p->next=NULL;
r->next=p;
r=p;
saveflag=1;
}
return;
}
void Qur(Link l)
{
int select;
char searchinput[20];
Node *p;
Node *n[5]={NULL};
int i=0;
if(!l->next)
{
system("cls");
printf("\n=====>No student record!\n");
getchar();
return;
}
system("cls");
printf("\n=====>1 Search by number=====>Search by name\n");
printf(" please choice[1,2]:");
scanf("%d",&select);
if(select==1)
{
stringinput(searchinput,10,"input the existing student number:");
p=Locateofnum(l,searchinput);
if(p)
{
printheader();
printdata(p);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else if(select==2)
{
stringinput(searchinput,15,"input the existing student name:");
Locateofname(l,searchinput,n);
while((p=n[i++])!=NULL)
{
printheader();
printdata(p);
printf(END);
}
printf("press any key to return");
getchar();
}
else
Wrong();
getchar();
}
void Del(Link l)
{
Node *p,*r;
char findmess[20],ch;
if(! l-> next)
{system("cls");
printf("\n=====> No student record! \n");
getchar();
return;
}
system("cls");
Disp(l);
stringinput(findmess,10,"input the existing student number:");
getchar();
p=Locateofnum(l,findmess);
printf("Are you sure to delete %s(y/n)?",findmess);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{
if(p) /*p!=NULL*/
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>deletesuccess! \n");
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
}
void Modify(Link l)
{
Node *p;
char findmess[20];
if(!l->next)
{ system("cls");
printf("\n=====>No student record!\n");
getchar();
return;
}
system("cls");
printf("modify student recorder");
Disp(l);
stringinput(findmess,10,"input the existing student number:");
p=Locateofnum(l,findmess);
if(p)
{
printf("Number:%s,\n",p->data.num);
printf("Name:%s,\n",p->data.name);
stringinput(p->data.name,15,"input new name:");
printf("C language score:%d,",p->data.cgrade);
p->data.cgrade=numberinput("C language score[0~100]:");
printf("Math score:%d,",p->data.mgrade);
p->data.mgrade=numberinput("Math score[0~100]:");
printf("English score:%d,",p->data.egrade);
p->data.egrade=numberinput("English score[0~100]:");
p->data.total=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=(float)(p->data.total/3);
p->data.mingci=0;
printf("\n=====>modify success!\n");
Disp(l);
}
else
Nofind();
getchar();
getchar();
}
void Insert(Link l)
{
Link p,v,newinfo;
char ch,num[10],s[10];
int flag=0;
v=l->next;
system("cls");
Disp(l);
while(l)
{stringinput(s,10,"please input insert location after the Number:");
flag=0;v=l->next;
while(v)
{
if(strcmp(v->data.num,s)==0){flag=1;break;}
v=v->next;
}
if(flag==1)
break;
else
{getchar();
printf("\n=====>The number%s is not existing,try again?(y/n):",s);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{ continue; }
else
{ return; }
}
}
stringinput(num,10,"input new student Number:");
v=l->next;
while(v)
{
if(strcmp(v->data.num,num)==0)
{
printf("=====>sorry,the new number:'%s' is existing!\n",num);
printheader();
printdata(v);
printf("\n");
getchar();
return;
}
v=v->next;
}
newinfo=(Node*)malloc(sizeof(Node));
if(!newinfo)
{
printf("\n allocate memory failure");
return;
}
strcpy(newinfo->data.num,num);
stringinput(newinfo->data.name,15,"Name:");
newinfo->data.cgrade=numberinput("C language Score[0~100]:");
newinfo->data.mgrade=numberinput("Math score[0~100]:");
newinfo->data.mgrade=numberinput("English score[0~100]:");
newinfo->data.total=newinfo->data.egrade+newinfo->data.cgrade+newinfo->data.mgrade;
newinfo->data.ave=(float)(newinfo->data.total/3);
newinfo->data.mingci=0;
newinfo->next=NULL;
saveflag=1;
p=l->next;
while(l)
{
if(strcmp(p->data.num,s)==0)
{
newinfo->next=p->next;
p->next=newinfo;
break;
}
p=p->next;
}
Disp(l);
printf("\n\n");
getchar();
}
void Count(Link l)
{
Node *pm, *pe, *pc,*pt;
Node *r=l->next;
int countc[5]={0},countm[5]={0},counte[5]={0};
if(!r)
{system("cls");
printf("\n=====>Not student record!\n");
getchar();
return;
}
system("cls");
Disp(l);
pm=pe=pc=pt=r;
while(r)
{
if(r->data.cgrade<60)countc[0]++;
if(r->data.mgrade<60)countm[0]++;
if(r->data.egrade<60)counte[0]++;
if(r->data.cgrade>=60&&r->data.cgrade<70)countc[2]++;
if(r->data.mgrade>=60&&r->data.mgrade<70)countm[2]++;
if(r->data.egrade>=60&&r->data.egrade<70)counte[2]++;
if(r->data.cgrade>=80&&r->data.cgrade<90)countc[3]++;
if(r->data.mgrade>=80&&r->data.mgrade<90)countm[3]++;
if(r->data.egrade>=80&&r->data.egrade<90)counte[3]++;
if(r->data.cgrade>=90)countc[4]++;
if(r->data.mgrade>=90)countm[4]++;
if(r->data.egrade>=90)counte[4]++;
if(r->data.cgrade>=pc->data.cgrade)pc=r;
if(r->data.mgrade>=pm->data.mgrade)pm=r;
if(r->data.egrade>=pe->data.egrade)pe=r;
if(r->data.total>=pt->data.total)pt=r;
r=r->next;
printf("\n---------------the TongJi result----------------\n");
printf("C Language 0~59:%d(ren)\n",countc[0]);
printf("C Language 60~69:%d(ren)\n",countc[1]);
printf("C Language 70~79:%d(ren)\n",countc[2]);
printf("C Language 80~89:%d(ren)\n",countc[3]);
printf("C Language 90~100:%d(ren)\n",countc[4]);
printf("\n------------------------------------------------\n");
printf("Math 0~59:%d(ren)\n",countm[0]);
printf("Math 60~69:%d(ren)\n",countm[1]);
printf("Math 70~79:%d(ren)\n",countm[2]);
printf("Math 80~89:%d(ren)\n",countm[3]);
printf("Math 90~100:%d(ren)\n",countm[4]);
printf("\n------------------------------------------------\n");
printf("English 0~59:%d(ren)\n",counte[0]);
printf("English 60~69:%d(ren)\n",counte[1]);
printf("English 70~79:%d(ren)\n",counte[2]);
printf("English 80~89:%d(ren)\n",counte[3]);
printf("English 90~100:%d(ren)\n",counte[4]);
printf("------------------------------------\n");
printf("The highest student by total score num:%s total score :%d\n ",pt->data.num,pt->data.total);
printf("The highest student by English score num:%s total score :%d\n ",pt->data.num,pe->data.egrade);
printf("The highest student by Math score num:%s total score :%d\n ",pm->data.num,pm->data.mgrade);
printf("The highest student by C score num:%s total score :%d\n ",pc->data.num,pc->data.cgrade);
printf("\n\n press any key to return");
getchar();
}
void Sort(int Link );
{
struct student temp;
Node *p1, *p2, *p;
int i=0;
if(l->next==NULL)
{
system("cls");
printf("\n=====>Not student record! \n");
getchar();
return;
}
system("cls");
Disp(l);
p1=l->next;
while(p1)
{
p2=p1->next;
p=p1;
while(p2)
{
if(p2->data.total>=p->data.total)
p=p2;
p2=p2->next;
}
temp=p->data;
p->data=p1->data;
p1->data=temp;
p1=p1->next;
}
p=l->next;
while(p!=NULL)
{
i++;
p->data.mingci=i;
p=p->next;
}
Disp(l);
saveflag=1;
printf("\n=====>sort complete! \n");
}
void Save(int Link );
{
FILE *fp;
Node *p;
int count=0;
fp=fopen("c:\\student","wb");
if(fp==NULL)
{
printf("\n=====>open file error! \n");
getchar();
return;
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p->next;
count++;
}
else
{
break;
}
}
if(count>0)
{
getchar();
printf("\n\n\n\n\n=====>save file complete,total saved's record number is:%d\n",count);
getchar();
saveflag=0;
}
else
{
system("cls");
printf("the current link is empty,no student record is saved!\n");
getchar();
}
fclose(fp);
}
int main(void)
{
Link l;
FILE*fp;
int select;
char ch;
int count=0;
Node *p,*r;
l=(Node *)malloc(sizeof(Node));
if(!l)
{
printf("\n allocate memory failure");
return 0;
}
l->next=NULL;
r=l;
fp=fopen("C:\\student","ab+");
if(fp==NULL)
{
printf("\n=====>can not open file!\n");
exit(0);
}
while(!feof(fp))
{
p=(Node*)malloc(sizeof(Node));
if(!p)
{
printf("memory malloc filure!\n");
exit(0);
}
if(fread(p,sizeof(Node),l,fp)==1)
{
p->next=NULL;
r->next=p;
r=p;
count++;
}
}
fclose(fp);
printf("\n=====>open file sucess,the total recorde number is:%d.\n",count);
printf("\n=====>Presss any key to enter the Menu!");
getchar();
while(l)
{
system("cls");
menu();
p=r;
printf("\n please Enter your choice(0~9):");
scanf("%d",&select);
if(saveflag==1)
{ getchar();
printf("\n=====>Whether save the modified record to file?(y/n):");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(l);
}
printf("=====>thank you for useness!");
getchar();
break;
}
switch(select)
{
case 1:Add(l);break;
case 2:Del(l);break;
case 13:Qur(l);break;
case 4:Modify(l);break;
case 5:Insert(l);break;
case 6:Count(l);break ;
case 7:Sort(l);break;
case 8:Save(l);break;
case 9:System("cls");Disp(l);break;
default:Wrong();getchar();break;
}
return 0;
}
}