#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int no;
char name[20];
char gender;
double score[3];
double sum;
double ave;
struct student *next;
}stu;
void input(stu *t)
{
int i;
t->sum=0;
scanf("%d ",t->no);
gets(t->name);
t->gender=getchar();
for(i=0;i<3;i++)
{
scanf("%lf",&t->score[i]);
t->sum+=t->score[i];
}
t->ave=t->sum*1.0/3;
}
int seek(stu *head,int no)
{
int i,j=0;
stu *p;
p=head->next;
while(p)
{
if(p->no==no)
{
printf("%d %s ",p->no,p->name);
for(i=0;i<3;i++)
printf("%.2lf ",p->score[i]);
printf("%.2lf %.2lf\n",p->ave,p->sum);
return 1;
}
p=p->next;
}
return 0;
}
stu *creatlist(int n)
{
stu *head,*p,*q;
int i;
head=(stu*)malloc(sizeof(stu));
head->next=NULL;
q=head;
for(i=0;i<n;i++)
{
p=(stu*)malloc(sizeof(stu));
input(p);
q->next=p;
q=p;
}
q->next=NULL;
return head;
}
void output(stu *head)
{
int i;
stu *p;
p=head->next;
while(p)
{
printf("%d %s ",p->no,p->name);
for(i=0;i<3;i++)
printf("%.2lf ",p->score[i]);
printf("%.2lf %.2lf\n",p->ave,p->sum);
p=p->next;
}
}
stu *delete(stu *head,int no)
{
stu *p,*pre;
pre=head;
p=head->next;
while(p)
{
if(p->no==no)
{
pre->next=p->next;
return 1;
}
pre=p;
p=p->next;
}
return head;
}
void store(FILE *fp,stu *t,int n)
{
fp=fopen("student.dat.txt","w+");
if(fp==NULL)
{
printf("error open file\n");
exit(1);
}
fwrite(t,sizeof(stu),n,fp);
fclose(fp);
}
stu *insert(stu *head,stu *x)
{
stu *p,*pre;
pre=head;
p=head->next;
while(p)
{
if(x->no<p->no)
{
pre->next=x;
x->next=p;
return 1;
}
pre=p;
p=p->next;
}
pre->next=x;
x->next=NULL;
return head;
}
void menu()
{
printf("please enter your section on the keyboard\n");
printf("1---create 2---seek\n");
printf("3---delete 3---add\n");
printf("5---print 6---quit\n");
}
int main()
{
FILE *fp;
stu *head=NULL,new;
int ncom,n,no;
while(1)
{
menu();
scanf("%d",&ncom);
{
switch(ncom)
{case 1:
{
scanf("%d",&n);
head=creatlist(n);
break;
}
case 2:
{
scanf("%d",&no);
if(!seek(head,no))
printf("no person\n");
break;
}
case 3:
{
scanf("%d",&no);
head=delete(head,no);
break;
}
case 4:
{
//store(head,fp,n);
printf("please enter information of new student\n");
input(&new);
head=insert(head,&new);
break;
}
case 5:
{
output(head);
break;
}
case 6:exit(0);
}
}
}
return 0;
}