#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student{
int id;
char name[10];
float chinese;
float math;
float english;
float sum;
struct student *next,*previous;
};
typedef struct object{
float chinest;
float math;
float sum;
};
typedef struct date{
int year;
int month;
int day;
};
int n=0;
/********************************************************/
struct student *delete(struct student *head)
{
int num=0;
printf("please input a id for deleting!\n");
scanf("%d",&num);
struct student *p=head,*ptr;
while(num!=p->id && p->next!=NULL)
{
ptr=p;
p=p->next;
}
if(num==p->id)
{
if(p==head)
{
head=p->next;
}
else
{
ptr->next=p->next;
printf("delete id :%d\n",num);
n=n-1;
}
}
return head;
}
/*********************************************************/
struct student *seach(struct student *head)
{
int num=0;
printf("please input a id for seaching!\n");
scanf("%d",&num);
struct student *p=head,*ptr;
while(num!=p->id && p->next!=NULL)
{
ptr=p;
p=p->next;
}
if(num==p->id)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$ id name chinese math english sum $$\n");
printf("$$ %d %s %.2f %.2f %.2f %.2f $$\n",p->id,p->name,p->chinese,p->math,p->english,p->sum);
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n\n");
}
return head;
}
/*********************************************************/
struct student *insert(struct student *head)
{ float sum=0;
FILE *input;
input=fopen("StudentManagerSystem","a+");
struct student *p=head;
struct student *stunew=(struct student *)malloc(sizeof(struct student));
while(p->next!=NULL)
{
p=p->next;
}
printf("please insert a student id :\n");
scanf("%d",&stunew->id);
printf("student name:\n");
scanf("%s",&stunew->name);
printf("student chinese score:\n");
scanf("%f",&stunew->chinese);
sum=sum+stunew->chinese;
printf("student math score:\n");
scanf("%f",&stunew->math);
sum=sum+stunew->math;
printf("student english score :\n");
scanf("%f",&stunew->english);
sum=sum+stunew->english;
stunew->sum=sum;
sum=0;
stunew->next=NULL;
printf("you will insert a id:%d\n",stunew->id);
p->next=stunew;
if(fopen("StudentManagerSystem","a+")!=NULL)
{
fwrite(stunew,sizeof(struct student),1,input);
fclose(input);
}
free(stunew);
return head;
}
/********************************************************/
struct student *create()
{
float sum=0;
FILE *fout;
fout=fopen("StudentManagerSystem","w");
struct student *head,*stunew,*stuend;
stunew=stuend=(struct studnt *)malloc(sizeof(struct student));
printf("please insert a student id :\n");
scanf("%d",&stunew->id);
printf("student name:\n");
scanf("%s",&stunew->name);
printf("student chinese score:\n");
scanf("%f",&stunew->chinese);
sum=sum+stunew->chinese;
printf("student math score:\n");
scanf("%f",&stunew->math);
sum=sum+stunew->math;
printf("student english score :\n");
scanf("%f",&stunew->english);
sum=sum+stunew->english;
stunew->sum=sum;
sum=0;
stunew->next=NULL;
fwrite(stunew,sizeof(struct student),1,fout);
fclose(fout);
while(stunew->id!=0)
{
n++;
if(n==1)
{
head=stunew;
}
else
{
stunew=(struct studnt *)malloc(sizeof(struct student));
printf("input '0' will be return!\n");
printf("please insert a student id :\n");
scanf("%d",&stunew->id);
if(stunew->id==0)
{
return head;
}else
{
printf("student name:\n");
scanf("%s",&stunew->name);
printf("student chinese score:\n");
scanf("%f",&stunew->chinese);
sum=sum+stunew->chinese;
printf("student math score:\n");
scanf("%f",&stunew->math);
sum=sum+stunew->math;
printf("student english score :\n");
scanf("%f",&stunew->english);
sum=sum+stunew->english;
stunew->sum=sum;
sum=0;
stunew->next=NULL;
stuend->next=stunew;
stuend=stunew;
FILE *a;
a=fopen("StudentManagerSystem","a+");
fwrite(stunew,sizeof(struct student),1,a);
fclose(a);
}
}
}
free(stunew);
free(stuend);
return head;
}
/*****************************************************/
struct student *show(struct student *head)
{
struct student *p=head;
printf("student infortation :\n");
printf("id name chinese math english sum \n");
while(p!=NULL)
{
printf("%d %s %.2f %.2f %.2f %.2f\n",p->id,p->name,p->chinese,p->math,p->english,p->sum);
p=p->next;
}
return head;
printf("\n\n\n");
}
/*????????????????????????????????????????????????????*/
void readlink()
{
struct student *stunew;
FILE *read;
read=fopen("StudentManagerSystem","r");
stunew=(struct student *)malloc(sizeof(struct student));
while( fread(stunew,sizeof(struct student),1,read)==1)
{
printf("%d %s %.2f %.2f %.2f %.2f\n",stunew->id,stunew->name,stunew->chinese,stunew->math,stunew->english,stunew->sum);
}
fclose(read);
free(stunew);
}
/*****************************************************/
struct student *inputlink()
{
FILE *fin;
int i=0;
struct student *head,*stunew,*stuend;
fin=fopen("StudentManagerSystem","r");
stunew=stuend=(struct student *)malloc(sizeof(struct student));
stunew->next=NULL;
head=stunew;
while(i!=1)
{
if(fread(stunew,sizeof(struct student),1,fin)==1)
{
stunew->next=NULL;
stuend->next=stunew;
stuend=stunew;
if(i!=1)
{
stunew=(struct student *)malloc(sizeof(struct student));
}
}
else i=1;
}
fclose(fin);
printf("\n\n");
return head;
}
/****************************************************/
struct student *change(struct student *head)
{
int num=0;
printf("please input a id for change!\n");
scanf("%d",&num);
struct student *p=head,*ptr;
while(num!=p->id && p->next!=NULL)
{
ptr=p;
p=p->next;
}
if(num==p->id)
{
if(p==head)
{
head=p->next;
}
else
{
ptr->next=p->next;
printf("delete id :%d\n",num);
n=n-1;
}
}
return head;
}
/****************************************************/
void save(struct student *head)
{
FILE *save_1;
struct student *p=head,*stunew;
save_1=fopen("StudentManagerSystem","w");
fwrite(p,sizeof(struct student),1,save_1);
fclose(save_1);
p=p->next;
while(p!=NULL)
{
FILE *save_2;
save_2=fopen("StudentManagerSystem","a+");
fwrite(p,sizeof(struct student),1,save_2);
p=p->next;
fclose(save_2);
}
}
/*****************************************************/
struct student *Max(struct student *head)
{
struct student *p=head,*ptr,*temp;
float max=head->sum;
for(p=head;p!=NULL;p=p->next)
{
for(ptr=p->next;ptr!=NULL;ptr=ptr->next)
{
if(max<ptr->sum)
{
temp=ptr;
}
}
}
printf("The max score student:\n");
printf("%d %s %.2f %.2f %.2f %.2f\n",temp->id,temp->name,temp->chinese,temp->math,temp->english,temp->sum);
return head;
}
/*****************************************************/
struct student *BubbleSort(struct student *head)
{
struct student *endpt;
struct student *p;
struct student *p1;
struct student *p2;
p1 = (struct student *)malloc(sizeof(struct student));
p1->next = head;
head = p1;
for (endpt=NULL; endpt!=head; endpt=p)
{
for (p=p1=head; p1->next->next!=endpt; p1=p1->next)
{
if (p1->next->sum> p1->next->next->sum)
{
p2 = p1->next->next;
p1->next->next = p2->next;
p2->next = p1->next;
p1->next = p2;
p = p1->next->next;
}
}
}
p1 = head;
head = head->next;
free(p1);
p1 = NULL;
return head;
}
/*****************************************************/
typedef enum ENUM{deleteF,insertF,showF,seachF,changeF,MaxF,BubbleSortF};//mei ju
/*****************************************************/
typedef (*function)(struct student *head);
function array[7]={delete,insert,show,seach,change,Max,BubbleSort};
int sort(struct student *head,enum ENUM ip)
{
array[ip](head);
}
/***************************************************/
void main(void){
int i=9;
enum ENUM enumF;
while(i!=0)
{
printf(" ***************welcome ***************\n");
printf("*************student manager system******************\n\n\n\n");
printf(" MENU \n");
printf(" ****** 1 *******create a student table\n");
printf(" ****** 2 *******delete a student id\n");
printf(" ****** 3 *******insert a student id\n");
printf(" ****** 4 *******seach a student id\n");
printf(" ****** 5 *******printf all students infortation\n");
printf(" ****** 6 *******read all students from the file\n");
printf(" ****** 7 *******change choose id\n");
printf(" ****** 8 *******a max student information \n");
printf(" ****** 0 *******exit system\n");
scanf("%d",&i);
struct student *head;
switch(i)
{
case 1:
head=create();
break;
case 2:
head=inputlink();
enumF=deleteF;
head=sort(head,enumF);
save(head);
break;
case 3:
head=inputlink();
enumF=insertF;
head=sort(head,enumF);
break;
case 4:
head=inputlink();
enumF=seachF;
head=sort(head,enumF);
break;
case 5:
head=inputlink();
enumF=showF;
sort(head,enumF);
break;
case 6:
head=inputlink();
enumF=BubbleSortF;
// head=sort(head);
save(head);
readlink();
break;
case 7:
head=inputlink();
enumF=changeF;
head=sort(head,enumF);
save(head);
enumF=insertF;
head=sort(head,enumF);
break;
case 8:
head=inputlink();
enumF=MaxF;
sort(head,enumF);
break;
}
}
}