拜托了 ,谢谢,要不然就死定了@!!
參考答案:#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
char name[20];
float s[4];
struct student *next;
};
int n;
struct student *readin(void)
{struct student *head;
struct student *p1,*p2;
float x,y,z;
n=0;
p1=(struct student *)malloc(LEN);
p2=p1;
scanf("%ld%s%f%f%f",&p1->num,p1->name,&x,&y,&z);
p1->s[0]=x;
p1->s[1]=y;
p1->s[2]=z;
p1->s[3]=((x+y+z)/3);
while(p1->num!=NULL)
{n=n+1;
if(n==1)head=p1;
else
{p2->next=p1;
p2=p1;
}
p1=(struct student *)malloc(LEN);
scanf("%ld%s%f%f%f",&p1->num,p1->name,&x,&y,&z);
p1->s[0]=x;
p1->s[1]=y;
p1->s[2]=z;
p1->s[3]=((x+y+z)/3);
}
p2->next=NULL;
return(head);
}
struct student *taxis(struct student *x)
{struct student *p3,*p4,*p5,*q;
int i;
p3=x->next;
p4=x;
for(i=0;i<n;i++)
{while(p3->num!=0)
{if(p4->s[3]<=p3->s[3])
{if(p4==x)
{q=p3;
p4->next=p3->next;
p3->next=p4;
p3=p4->next;
x=q;
p5=q;
}
else
{p5->next=p3;
p4->next=p3->next;
p3->next=p4;
p5=p3;
p3=p4->next;
}
}
else
{if(p4==x)
{p5=x;
p4=p3;
p3=p3->next;
q=x;
}
else
{p5=p4;
p4=p3;
p3=p3->next;
}
}
}
p3=x->next;
p4=x;
}
return(q);
}
struct student *insert(struct student *o)
{struct student *pl,*p6,*p7,*p8,*head;
float a,b,c;
int w=0;
printf("input a student data!\n");
pl=(struct student *)malloc(LEN);
scanf("%ld%s%f%f%f",&pl->num,pl->name,&a,&b,&c);
pl->s[0]=a;
pl->s[1]=b;
pl->s[2]=c;
pl->s[3]=((a+b+c)/3);
p6=o;
p7=o;
p8=o->next;
while(p6->num!=0&&w!=1)
{if(p6->s[3]>pl->s[3])
{if(p6==o)
{if(p8->num!=0)
{head=p6;
p6=o->next;
p8=p8->next;
}
else
{head=p6;
p6->next=pl;
pl->next=0;
w=1;
}
}
else
{if(p8->num==0)
{p6->next=pl;
pl->next=0;
w=1;
}
else
{p6=p6->next;
p7=p7->next;
p8=p8->next;
}
}
}
else
{if(p6==o)
{head=pl;
pl->next=o;
w=1;
}
else
{p7->next=pl;
pl->next=p6;
w=1;
}
}
}
return(head);
}
void search(struct student *pn)
{struct student *p4,*p5;
char na[20];
int u=1;
p4=pn;
p5=pn;
printf("please input the name:\n");
scanf("%s",&na);
while(strcmp((p4->name),na)!=0&&u!=0)
{p5=p5->next;
p4=p5;
if(p5->num==0)u=0;
}
if(p4->num!=0)
printf("%-12ld%s%8.1f%8.1f%8.1f%8.1f\n",p4->num,p4->name,p4->s[0],p4->s[1],p4->s[2],p4->s[3]);
else
{printf("Please input the right name!\n");
search(pn);
}
}
void save(struct student *ps)
{FILE *fp;
int i;
struct student *p11,*p12;
p11=ps;
p12=ps;
if((fp=fopen("D:\\score.txt","w"))==NULL)
{printf("cannot open file\n");
return;
}
while(p12->num!=0)
{fwrite(p11,sizeof(struct student),1,fp);
p12=p12->next;
p11=p12;
}
fclose(fp);
printf("save in D:\\score.txt!\n");
}
extern void show(struct student *x)
{struct student *p5;
p5=x;
if(x!=NULL)
do
{printf("%-12ld%s%8.1f%8.1f%8.1f%8.1f",p5->num,p5->name,p5->s[0],p5->s[1],p5->s[2],p5->s[3]);
p5=p5->next;
printf("\n");
}
while(p5->num!=0);
}
#define ONE "input 1 to insert a student data, input 2 to search a student data,\ninput 3 to save all students data, input 4 to printf all, input 0 to exit!\n"
void main()
{struct student *p,*k,*f;
int t;
printf("input students'number(Tab)name(Tab)score1(Tab)score2(Tab)score(Enter)\n");
printf("and input 0 0 0 0 0 to end input!\n");
p=readin();
if(n!=1)
k=taxis(p);
f=k;
printf(ONE);
scanf("%d",&t);
while(t!=0)
{if(t==1||t==2||t==3||t==4)
{if(t==1)f=insert(f);
if(t==2)search(f);
if(t==3)save(f);
if(t==4)show(f);
}
else
printf("error!\n");
scanf("%d",&t);
}
getch();
exit(0);
}
使用说明:
注意:使用TURBO C++3.0进行编译时候,需要在File\Change Directory 选项将ex保存的路径(ex文件直接放在TC的的下一层,即与Project同层)输入Directory Name 再进行编译.使用File\Open打开文件.
使用WIN-TC可以直接打开就可以编译了.
这个是我曾经做过的实验课程设计,可能不太完善.