#include <stdio.h> 
#include <stdlib.h> /*˵*/ 
#include <string.h> /*ַ*/ 
#include <time.h>

#define LEN sizeof(STUDENT) 
typedef struct stu /*ṹڻ*/ 
{
	char  num[6]; 
	char  name[5]; 
	int   score[3]; 
	int   sum; 
	float average; 
	int   order; 
	struct stu *next; 
}STUDENT; 

/*ʼ*/ 
STUDENT *Init() 
{ 
	return NULL; /*ؿָ*/ 
} 

/*˵ѡ*/ 
int Menu_Select() 
{
	int n;
	struct tm *pt; /*ʱṹ*/ 
	time_t t;

	t=time(NULL);
	pt=localtime(&t); /*ȡϵͳڲŵṹ*/ 
	printf("\nһ˵...... \n"); /*һ˵*/ 
	//getch(); /*Ӽ̶ȡһַ,ʾĻ*/ 
	system("pause");
	system("cls"); /**/ 
	printf("********************************************************************************\n"); 
	printf("\t\t ӭ Welcome to\n"); 
	printf("\n\t\t\t ʹѧϵͳ1.0\n"); 
	printf("*************************************MENU***************************************\n"); 
	printf("\t\t\t1. ѧɼ¼ Enter the record\n"); /*ѧɼ¼*/ 
	printf("\t\t\t2. ʾ Print the record\n"); /*ʾ*/ 
	printf("\t\t\t3. Ѱ Search record on name\n"); /*Ѱ*/ 
	printf("\t\t\t4. ɾ Delete a record\n"); /*ɾ*/ 
	printf("\t\t\t5.  Sort to make New a file\n"); /**/ 
	printf("\t\t\t6.  Insert record to list\n"); /**/ 
	printf("\t\t\t7.  Save the file\n"); /**/ 
	printf("\t\t\t8. ȡ Load the file\n"); /*ȡ*/ 
	printf("\t\t\t9. ˳ Quit\n"); /*˳*/  
	printf("\n********************************************************************************\n"); 
	printf("\t\t\t\tǰϵͳ:%d-%d-%d\n",pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday); /*ʾǰϵͳ*/ 
	do
	{ 
		printf("\n\t\t\tѡEnter your choice(1~9):"); 
		fflush(stdin);
		scanf("%d",&n); 
	}while(n<1||n>9); /*ѡ1~9֮*/ 
	return(n); /*ѡݸӦĺ*/ 
} 

/*뺯*/ 
STUDENT *Create() 
{
	int i,s; 
	STUDENT *head=NULL,*p; /* 庯.˺һָͷָ*/ 
	system("cls"); 
	for(;;) 
	{
		p=(STUDENT *)malloc(LEN); /*һµĵԪ*/ 
		if(!p) /*ָpΪ*/ 
		{
			printf("\nڴ. Out of memory."); /*ڴ*/ 
			return (head); /*ͷָ,ͬ*/ 
		} 
		printf("ѧEnter the num(0:list end):"); 
		scanf("%s",p->num); 
		if(p->num[0]=='0') break; /*ѧַΪ0*/ 
		printf("Enter the name:"); 
		scanf("%s",p->name); 
		printf("3ųɼPlease enter the %d scores\n",3); /*ʾʼɼ*/ 
		s=0; /*ÿѧֵܷ֣Ϊ0*/ 
		for(i=0;i<3;i++) /*3ſγѭ3*/ 
		{ 
			do
			{ 
				printf("ɼscore%d:",i+1); 
				scanf("%d",&p->score[i]); 
				if(p->score[i]<0 || p->score[i]>100) /*ȷɼ0~100֮*/ 
					printf("ݴ, Data error,please enter again.\n"); 
			}while(p->score[i]<0 || p->score[i]>100); 
			s=s+p->score[i]; /*ۼӸųɼ*/ 
		} 
		p->sum=s; /*ֱܷ*/ 
		p->average=(float)s/3; /*ǿתsתfloat,ƽֵ*/ 
		p->order=0; /*δǰֵΪ0*/ 
		p->next=head; /*ͷΪĺ̽*/ 
		head=p; /*Ϊµͷ*/ 
	} 
	return(head); 
} 

/* ʾȫ¼*/ 
void Print(STUDENT *head) 
{
	int i=0; /* ͳƼ¼*/ 
	STUDENT *p; /*ƶָ*/ 
	system("cls"); 
	p=head; /*ֵΪͷָ*/ 
	printf("\n************************************STUDENT************************************\n"); 
	printf("-------------------------------------------------------------------------------\n"); 
	printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n"); 
	printf("-------------------------------------------------------------------------------\n"); 
	while(p!=NULL) 
	{ 
		i++; 
		printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n", 
			i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); 
		p=p->next; 
	} 
	printf("-------------------------------------------------------------------------------\n"); 
	printf("**************************************END**************************************\n"); 
} 

/*Ҽ¼*/ 
void Search(STUDENT *head) 
{
	STUDENT *p; /* ƶָ*/ 
	char s[5]; /*õַ*/ 
	system("cls"); 
	printf(". Please enter name for searching.\n"); 
	scanf("%s",s); 
	p=head; /*ͷָ븳p*/ 
	while(strcmp(p->name,s) && p != NULL) /*¼Ҫҵģָ벻Ϊʱ*/ 
		p=p->next; /*ƶָ룬ָһ*/ 
	if(p!=NULL) /*ָ벻Ϊ*/ 
	{
		printf("\n*************************************FOUND************************************\n"); 
		printf("-------------------------------------------------------------------------------\n"); 
		printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n"); 
		printf("-------------------------------------------------------------------------------\n"); 
		printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n", 
			p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); 
		printf("-------------------------------------------------------------------------------\n"); 
		printf("***************************************END**************************************\n"); 
	} 
	else 
		printf("\nûиѧ There is no num %s student on the list.\n",s); /*ʾûиѧ*/ 
} 

/*ɾ¼*/ 
STUDENT *Delete(STUDENT *head) 
{
	//int n; 
	STUDENT *p1,*p2; /*p1ΪҵҪɾĽָ룬p2Ϊǰָ*/ 
	char c,s[6]; /*s[6]ѧ,cĸ*/ 
	system("cls"); 
	printf("Ҫɾѧ Please enter the Deleted num: "); 
	scanf("%s",s); 
	p1=p2=head; /*p1p2ֵͷָ*/ 
	while(strcmp(p1->num,s) && p1 != NULL) /*¼ѧŲҪҵģָ벻Ϊʱ*/ 
	{
		p2=p1; /*p1ֵָp2Ϊp1ǰָ*/ 
		p1=p1->next; /*p1ָָһ¼*/ 
	} 
	if(strcmp(p1->num,s)==0) /*ѧҵ*/ 
	{
		printf("**************************************FOUND************************************\n"); 
		printf("-------------------------------------------------------------------------------\n"); 
		printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n"); 
		printf("-------------------------------------------------------------------------------\n"); 
		printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n", 
			p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order); 
		printf("-------------------------------------------------------------------------------\n"); 
		printf("***************************************END**************************************\n"); 
		printf("\nǷҪɾ,Yɾ,N˳\nAre you sure to Delete the student Y/N ?"); /*ʾǷҪɾ,Yɾ,N˳*/ 
		for(;;) 
		{
			scanf("%c",&c); 
			if(c=='n'||c=='N') break; /*ɾ,ѭ*/ 
			if(c=='y'||c=='Y') 
			{ 
				if(p1==head) /*p1==head˵ɾ׽*/ 
					head=p1->next; /*ѵڶַhead*/ 
				else 
					p2->next=p1->next; /*һ½ַǰһַ*/ 
				//n=n-1; 
				printf("\nѧΪ(Num): %s ѧԱɾ(student have been Deleted.)\n",s); 
				printf("˱. Don't forget to Save.\n");break; /*ɾѭ*/ 
			} 
		} 
	} 
	else 
		printf("\nûѧڱ\nThere is no num %s student on the list.\n",s); /*Ҳý*/ 
	return(head); 
} 

/**/ 
STUDENT *Sort(STUDENT *head) 
{
	int i=0; /**/ 
	STUDENT *p1,*p2,*t,*temp; /*ʱָ*/ 
	temp=head->next; /*ԭͷָָһͷָ*/ 
	head->next=NULL; /*һΪ±ͷ*/ 
	while(temp!=NULL) /*ԭΪʱ*/ 
	{ 
		t=temp; /*ȡԭͷ*/ 
		temp=temp->next; /*ԭͷָ*/ 
		p1=head; /*趨ƶָp1ͷָ뿪ʼ*/ 
		p2=head; /*趨ƶָp2Ϊp1ǰֵΪͷָ*/ 
		while(t->average<p1->average&&p1!=NULL) /*ɼƽֱȽ*/ 
		{ 
			p2=p1; /*ֵС±ָ*/ 
			p1=p1->next; 
		} 
		if(p1==p2) /*p1==p2˵ֵӦλ*/ 
		{ 
			t->next=p1; /*ĺΪp*/ 
			head=t; /*ͷΪ*/ 
		} 
		else /*Ӧмĳλp2p1֮䣬pΪβ*/ 
		{ 
			t->next=p1; /*tĺp1*/ 
			p2->next=t; /*p2ĺt*/ 
		} 
	} 
	p1=head; /*źͷָ븳p1׼д*/ 
	while(p1!=NULL) /*p1Ϊʱв*/ 
	{ 
		i++; /**/ 
		p1->order=i; /*Ÿֵ*/ 
		p1=p1->next; /*ָ*/ 
	} 
	printf("ɹ Sorting is sucessful.\n"); /*ɹ*/ 
	return (head); 
} 

/*¼*/ 
STUDENT *Insert(STUDENT *head,STUDENT *New) 
{
	STUDENT *p0,*p1,*p2; 
	//int n;
	int sum1,i; 
	p1=head; /*ʹp1ָһ*/ 
	p0=New; /*p0ָҪĽ*/ 
	printf("\nPlease enter a New record.\n"); /*ʾ¼Ϣ*/ 
	printf("ѧEnter the num:"); 
	scanf("%s",New->num); 
	printf("Enter the name:"); 
	scanf("%s",New->name); 
	printf("Please enter the %d scores.\n",3); 
	sum1=0; /*¼¼ֵܷ֣Ϊ0*/ 
	for(i=0;i<3;i++) 
	{ 
		do
		{ 
			printf("ɼscore%d:",i+1); 
			scanf("%d",&New->score[i]); 
			if(New->score[i]>100||New->score[i]<0) 
				printf("ݴData error,please enter again.\n"); 
		}while(New->score[i]>100||New->score[i]<0); 
		sum1=sum1+New->score[i]; /*ۼӸųɼ*/ 
	} 
	New->sum=sum1; /*ִܷ¼¼*/ 
	New->average=(float)sum1/3; 
	New->order=0; 
	if(head==NULL) /*ԭǿձ*/ 
	{
		head=p0;
		p0->next=NULL;
	} /*ʹp0ָĽΪͷ*/ 
	else 
	{
		while((p0->average<p1->average)&&(p1->next!=NULL)) 
		{
			p2=p1; /*ʹp2ָղp1ָĽ*/ 
			p1=p1->next; /*p1һ*/ 
		} 
		if(p0->average>=p1->average) 
		{
			if(head==p1)head=p0; /*嵽ԭһ֮ǰ*/ 
			else p2->next=p0; /*嵽p2ָĽ֮*/ 
			p0->next=p1;
		} 
		else 
		{
			p1->next=p0;
			p0->next=NULL;
		} /*嵽Ľ֮*/ 
	} 
	//n=n+1; /*1*/ 
	head=Sort(head); /*ĺ,ѧɼ*/ 
	printf("\nѧStudent %s ѱhave been inserted.\n",New->name); 
	printf("Ҫ˱Don't forget to Save the New file.\n"); 
	return(head); 
} 

/*ݵļ*/ 
void Save(STUDENT *head) 
{
	FILE *fp; /*ָļָ*/ 
	STUDENT *p; /* ƶָ*/ 
	char outfile[10]; 
	printf("ļ:c:\\score Enter outfile name,for example c:\\score\n"); 
	scanf("%s",outfile); 
	if((fp=fopen(outfile,"w"))==NULL) /*Ϊһļ,Ϊֻдʽ*/ 
	{ 
		printf("򲻿ļCannot open the file\n"); 
		return; /*򲻿򷵻ز˵*/ 
	} 
	printf("\n...Saving the file......\n"); 
	p=head; /*ƶָͷָ뿪ʼ*/ 
	while(p!=NULL) /*pΪ*/ 
	{ 
		fwrite(p,LEN,1,fp); /*дһ¼*/ 
		p=p->next; /*ָ*/ 
	} 
	fclose(fp); /*رļ*/ 
	printf("ɹ....Save the file successfully!\n"); 
} 

/* ļݺ*/ 
STUDENT *Load() 
{
	STUDENT *p1,*p2,*head=NULL; /*¼ָ*/ 
	FILE *fp; /* ָļָ*/ 
	char infile[10]; 
	printf("ļ:c:\\score Enter infile name,for example c:\\score\n"); 
	scanf("%s",infile); 
	if((fp=fopen(infile,"r"))==NULL) /*һļΪֻʽ*/ 
	{ 
		printf("򲻿ļCan not open the file.\n"); 
		return(head); 
	} 
	printf("\nѰļ...Loading the file!\n"); 
	p1=(STUDENT *)malloc(LEN); /*һµԪ*/ 
	if(!p1) 
	{ 
		printf("ڴ!Out of memory!\n"); 
		return(head); 
	} 
	head=p1; /*뵽ռ䣬Ϊͷָ*/ 
	while(!feof(fp)) /*ѭֱļβ*/ 
	{ 
		if(fread(p1,LEN,1,fp)!=1) break; /*ûݣѭ*/ 
		p1->next=(STUDENT *)malloc(LEN); /*Ϊһ㿪ٿռ*/ 
		if(!p1->next) 
		{ 
			printf("Out of memory!\n"); 
			return (head); 
		} 
		p2=p1; /*ʹp2ָղp1ָĽ*/ 
		p1=p1->next; /*ָƣ¶ǰβ*/ 
	} 
	p2->next=NULL; /*һĺָΪ*/ 
	fclose(fp); 
	printf("\nɹĴļжȡ!\nYou have success to read data from the file!\n"); 
	return (head); 
} 

/**/ 
int main() 
{
	STUDENT *head,New; 
	head=Init(); //ʼ,ʹheadֵΪNULL 
	for(;;)      //ѭ޴
	{
		switch(Menu_Select()) 
		{ 
		case 1:head=Create();break; 
		case 2:Print(head);break; 
		case 3:Search(head);break; 
		case 4:head=Delete(head);break; 
		case 5:head=Sort(head);break; 
		case 6:head=Insert(head,&New);break; //&Newʾصַ
		case 7:Save(head);break; 
		case 8:head=Load(); break; 
		case 9:exit(0); //˵ֵΪ9
		} 
	} 

	return 0;
} 