|

- 发帖数
- 7
- 在线时间
- 0 小时
|
楼主
发表于 2010-3-13 14:19
| 只看该作者
麻烦帮我改一下,谢
#include "stdio.h"
#include "string.h"
#define N 30
#define M 5
#define HEADER1 "------------------------student---------------------------\n"
#define HEADER2 "| number | name |score[0]|score[1]|score[2]|score[3]|score[4]|sum |ave|mingci|\n"
#define HEADER3 "|------------|--------------|------|-----|------|-------|------|------|-----|------|"
#define FORMAT "| %-10S|%-15S|%4d|%4d|%4d|%4d|%4d|%.2f|%4d|\n"
#define DATER p->data.num,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2],p->data.score[3],p->data.score[4],p->data.total,p->data.ave,p->data.mingci
#define END "-----------------------------------------------------------------\n"
int saveflag=0;
typedef struct student
{
char num[10];
char name[15];
int score[M];
float ave;
int mingci;
int total;
};
typedef struct node
{
struct student data;
struct node *next;
}Node,*Link;
void main()
{
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 ;
}
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 failure!\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 records number is:%d.\n",count);
menu();
while(1)
{
system("cls");
menu();
p=r;
printf("\n please Enter your choice(0~9):");
scanf("%d",&select);
if(select==0)
{
if(saveflag==1)
{getchar();
printf("\n=====>Whether save the modified record to file?(y/n);");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(1);
}print("=====>thank you for useness!");
getchar();
break;
}
switch(select)
{
case 1:Add(l);break;
case 2:Del(l);break;
case 3:Qur(l);break;
case 4:Modify(l);break;
case 5:Insert(l);break;
case 6:Tongji(l);break;
case 7:Sort(l);break;
case 8:save(l);break;
case 9:system("cls");Disp(l);break;
default:Wrong();getchar();break;
}
}
}
void menu()
{
system("cls");
textcolor(10);
gotoxy(10,5);
cprintf(" The students'Grade Management System\n");
gotoxy(10,8);
cprintf("**********************menu************************\n");
gotoxy(10,9);
cprintf(" * 1 input record 2 delete record *\n");
gotoxy(10,10);
cprintf(" * 3 search record 4 modify record *\n");
gotoxy(10,11);
cprintf(" * 5 insert record 6 count record *\n");
gotoxy(10,12);
cprintf(" * 7 scort record 8 save record *\n");
gotoxy(10,13);
cprintf(" * 9 display record 0 quit record *\n");
gotoxy(10,14);
cprintf("******************************************\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();
}
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 thisstudent!\n");
}
Node* Locate(Link l,char findmess[],char nameornum[])
{
Node *r;
if(strcmp(nameornum,"num")==0)
{
r=l->next;
while(r)
{
if(strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
}
else if(strcmp(nameornum,"name")==0)
{
r=l->next;
while(r)
{
if(strcmp(r->data.name,findmess)==0)
return r;
r=r->next;
}
}
return 0;
}
void stringinput(char *t,int lens,char *notice)
{
char n[255];
do
{
printf(notice);
scanf("%s",n);
if(strlen(n)>lens)
printf("\n exceed the required length!\n");
}while(strlen(n)>lens);
strcpy(t,n);
}
int numberinput(char *notice)
{
int t=0;
do
{
printf(notic);
scanf("%d",&t);
if(t>100||t<0)
printf("\n score must in [0,100]!\n");
}while(t>100||t<0);
return t;
}
void Add(Link l)
{
Noid *p,*r,*s;
char ch,flag=0,num[10];
r=1;
s=l->next;
system("cls");
Disp(l);
while(r->next!=NULL)
r=r->next;
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("=====>The number %s is not existing,tr 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.score[0]=numberinput("score[0] Score[0-100]):");
p->data.score[1]=numberinput("score[1] Score[0-100]):");
p->data.score[2]=numberinput("score2] Score[0-100]):");
p->data.score[3]=numberinput("score[3] Score[0-100]):");
p->data.score[4]=numberinput("score[4] Score[0-100]):");
p->data.total=p->data.score[0]+p->data.score[1]+p->data.score[2]+p->data.score[3]+p->data.score[4];
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;
if(!l->next)
{
system("cls");
printf("\n=====>No student record!\n");
getchar();
return;
}
system("cls");
printf("\n =====>1 Search by number =====>2 Search by name\n");
printf(" please choice[1,2]:");
scanf("%d",&select);
if(select==1)
{
stringinput(searchinput,10,"input the existing student number:");
p=Locate(l,searchinput,"num");
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:");
p=Locate(l,searchinput,"name");
if(p)
{
printheader();
printdata(p);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else
Wrong();
getchar();
}
void Del(Link l)
{
int sel;
Node *p,*r;
char findmess[20];
if(!l->next)
{
system("cls");
printf("\n=====>No student record!\n");
getchar();
return;
}
system("cls");
Disp(l);
printf("\n =====>1 Delete by number =====>2 Delete by name\n"");
printf(" please choice[1,2]:");
scanf("%d",&sel);
if(sel==1)
{
stringinput(findmess,10,"input the existing student number:");
p=Locate(l,findmess,"num");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>delete success!\n");
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
else if(sel==2)
{
stringinput(findmess,15,"input the existing student name");
p=Locate(l,findmess,"name");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>delete success!\n");
getchar();
saveflag=1;
}
else
Wrong();
getchar();
}
void Modify()
{
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=Locate(l,findmess,"num");
if(p)
{
printf("Number:%s,\n",p->data.num);
printf("Name:%s,",p->data.name);
stringinput(p->data.name,15,"input new name:");
printf("score[0]:%d,",p->data.score[0]);
p->data.score[0]=numberinput("score[0] Score[0-100]:");
printf("score[1]:%d,",p->data.score[1]);
p->data.score[1]=numberinput("score[1] Score[0-100]:");
printf("score[2]:%d,",p->data.score[2]);
p->data.score[2]=numberinput("score[2] Score[0-100]:");
printf("score[3]:%d,",p->data.score[3]);
p->data.score[3]=numberinput("score[3] Score[0-100]:");
printf("score[4]:%d,",p->data.score[4]);
p->data.score[4]=numberinput("score[4] Score[0-100]:");
p->data.total=p->data.score[0]+p->data.score[1]+p->data.score[2]+p->data.score[3]+p->data.score[4];
p->data.ave=(float)(p->data.total/3);
p->data.mingci=0;
printf("\n=====>modify success!\n");
Disp(l);
saveflag=l;
}
else
Nofind();
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,num)==0)
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;}
}
}
strintinput(num,10,"input new student Number:");
v=l->next;
while(v)
{
if(strcmp(v->data.num,s)==0)
{
printf("=====>Sorry,the new number:'%s' is existing!\n",num);
printheader();
printdata(v);
printf("\n");
getchar();
return;
}
v=v->next;
}
newinfo=(Node *p)malloc(sizeof(Node));
if(!newinfo)
{
printf("\n allocate memory failure");
return;
}
strcpy(newinfo->data.num,num);
stringinput(newinfo->data.name,15,"Name:");
newinfo->data.score[0]=numberinput(score[0] Score[0-100]:");
newinfo->data.score[1]=numberinput(score[1] Score[0-100]:");
newinfo->data.score[2]=numberinput(score[2] Score[0-100]:");
newinfo->data.score[3]=numberinput(score[3] Score[0-100]:");
newinfo->data.score[4]=numberinput(score[4] Score[0-100]:");
newinfo->data.total=newinfo->data.score[0]+newinfo->data.score[1]+newinfo->data.score[2]+newinfo->data.score[3]+newinfo->data.score[4];
newinfo->data.ave=(float)(newinfo->data.total/3);
newinfo-.data.mingci=0;
newinfo->next=NULL;
saveflag=l;
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 Tongji(Link l)
{
Node *p0,*p1,*p2,*p3,*p4,*pt;
Node *r=l->next;
int count0=0,count1=0,count2=0,count3=0,count4=0;
if(!r)
{ system("cls");
printf("\n======>Not student record!\n");
getchar();
return;
}
system("cls");
Disp(l);
p0=p1=p2=p3=p4=pt;
while(r)
{
if(r->data.score[0]<60) count0++;
if(r->data.score[1]<60) count1++;
if(r->data.score[2]<60) count2++;
if(r->data.score[3]<60) count3++;
if(r->data.score[4]<60) count4++;
if(r->data.score[0]>=p0->data.score[0]) p0=r;
if(r->data.score[1]>=p1->data.score[1]) p1=r;
if(r->data.score[2]>=p2->data.score[2]) p0=r;
if(r->data.score[3]>=p3->data.score[3]) p3=r;
if(r->data.score[4]>=p4->data.score[4]) p4=r;
if(r->data.total>=pt->data.total) pt=r;
r=r->next;
printf("\n-----the Tongji result-------------------------\n");
printf("score[0]<60:%d(ren)\n",count0);
printf("score[1]<60:%d(ren)\n",count1);
printf("score[2]<60:%d(ren)\n",count2);
printf("score[3]<60:%d(ren)\n",count3);
printf("score[4]<60:%d(ren)\n",count4);
printf("-------------------------------------------------\n");
printf("The highest student by total score name:%s total score:%d\n",pt->data.name,pt->data.total);
printf("The highest student by score[0] name:%s total score:%d\n",p0->data.name,p0->data.score[0]);
printf("The highest student by score[1] name:%s total score:%d\n",p1->data.name,p1->data.score[1]);
printf("The highest student by score[2] name:%s total score:%d\n",p2->data.name,p2->data.score[2]);
printf("The highest student by score[3] name:%s total score:%d\n",p3->data.name,p3->data.score[3]);
printf("The highest student by score[4] name:%s total score:%d\n",p4->data.name,p4->data.score[4]);
printf("\n\npress any key to return");
getchar();
}
void Sort(Link l)
{
Link ll;
Node *p,*rr,*s;
int i=0;
if(l->next==NULL)
{system("cls");
printf("\n=====>Not student record!\n");
getchar();
return;
}
ll=(Node*)malloc(sizeof(Node));
if(!ll)
{
printf("\n allocate memory failure");
return;
}
ll->next=NULL;
system("cls");
Disp(l);
p=p->next;
while(p)
{
s=(Node*)malloc(sizeof(Node));
if(!s)
{
printf("\n allocate memory failure");
return;
}
s->data=p->data;
s->next=NULL;
rr=ll;
while(rr->next!=NULL&&rr->next->data.total>=p->data.total)
{rr=rr->next;}
if(rr->next==NULL)
rr->next=s;
else
{
s->next=rr->next;
rr->next=s;
}
p=p->next;
}
l->next=ll->next;
p=l->next;
while(p!=NULL)
{
i++;
p->data.mingci=i;
p=p->next;
}
Disp(l);
saveflag=l;
printf("\n =====>sort complete!\n");
}
void Save(Link l)
{FILE* fp;
Node *p;
int count=0;
fp=fopen("c:\\student","web");
if(fp==NULL)
{printf("\n=====>open file error!\n");
getchar();
return;
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node),l,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);
} |
|