#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∼∩豕?辰??邦??豕??‾2?米ㄓ...... \n");
getch();
system("pause");
system("cls");
printf("********************************************************************************\n");
printf("\t\t ??車- Welcome to\n");
printf("\n\t\t\t 那1車??∫谷迆1邦角赤?米赤31.0\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. 那?豕??∫谷迆3谷?“???? 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. 谷?3y Delete a record\n");
printf("\t\t\t5. ??D辰 Sort to make New a file\n");
printf("\t\t\t6. 2?豕? 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. 赤?3? Quit\n");
printf("\n********************************************************************************\n");
printf("\t\t\t\t米㊣?∼?米赤3豕??迆:%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);
return(n);
}
STUDENT *Create()
{
int i,s;
STUDENT *head=NULL,*p;
system("cls");
for(;;)
{
p=(STUDENT *)malloc(LEN);
if(!p)
{
printf("\n Out of memory.");
return (head);
}
printf("那?豕??∫o?Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break;
printf("那?豕???℅?Enter the name:");
scanf("%s",p->name);
printf("??那?豕?3??3谷?“Please enter the %d scores\n",3);
s=0;
for(i=0;i<3;i++)
{
do
{
printf("3谷?“score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100)
printf("那y?Y∩赤?車,????D?那?豕? 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;
p->order=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("??那?豕???D???角∩2谷?辰. Please enter name for searching.\n");
scanf("%s",s);
p=head;
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??車D???∫谷迆 There is no num %s student on the list.\n",s);
}
STUDENT *Delete(STUDENT *head)
{
//int n;
STUDENT *p1,*p2;
char c,s[6];
system("cls");
printf("??那?豕?辰a谷?3y米??∫o? Please enter the Deleted num: ");
scanf("%s",s);
p1=p2=head;
while(strcmp(p1->num,s) && p1 != NULL)
{
p2=p1;
p1=p1->next;
}
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那?﹞?辰a谷?3y,那?豕?Y谷?3y,N?辰赤?3?\nAre you sure to Delete the student Y/N ?");
for(;;)
{
scanf("%c",&c);
if(c=='n'||c=='N') break;
if(c=='y'||c=='Y')
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
//n=n-1;
printf("\n?∫o??a(Num): %s ?∫谷迆辰?㊣?谷?3y(student have been Deleted.)\n",s);
printf("㊣e赤邦芍?㊣㏒∩?. Don't forget to Save.\n");break;
}
}
}
else
printf("\n??車D?a???∫谷迆?迆㊣赤谷?\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;
p2=head;
while(t->average<p1->average&&p1!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1==p2)
{
t->next=p1;
head=t;
}
else
{
t->next=p1;
p2->next=t;
}
}
p1=head;
while(p1!=NULL)
{
i++;
p1->order=i;
p1=p1->next;
}
printf("??D辰3谷1| Sorting is sucessful.\n");
return (head);
}
STUDENT *Insert(STUDENT *head,STUDENT *New)
{
STUDENT *p0,*p1,*p2;
//int n;
int sum1,i;
p1=head;
p0=New;
printf("\nPlease enter a New record.\n");
printf("那?豕??∫o?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; /*㊣㏒∩?D?????米?℅邦﹞?㏒?3??米?a0*/
for(i=0;i<3;i++)
{
do
{
printf("3谷?“score%d:",i+1);
scanf("%d",&New->score[i]);
if(New->score[i]>100||New->score[i]<0)
printf("那y?Y∩赤?車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;
}
else
{
while((p0->average<p1->average)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->average>=p1->average)
{
if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
head=Sort(head);
printf("\n?∫谷迆Student %s 辰?㊣??邦D?have been inserted.\n",New->name);
printf("2?辰a赤邦芍?㊣㏒∩?Don't forget to Save the New file.\n");
return(head);
}
void Save(STUDENT *head)
{
FILE *fp;
STUDENT *p;
char outfile[10];
printf("那?3????t角y豕?:c:\\score Enter outfile name,