#include<stdio.h> //頭文件的開始
#include<stdlib.h>
struct Student1 //學生結構體
{
long int number;
char name[15],sex[5];
int age;
float score[4];
double total;
double averge;
};
struct Student //結點結構體
{
struct Student1 stu;
struct Student *next;
};
void Caidan(struct Student *p); //菜單函數
int print(); //主菜單輸出函數
void Input(struct Student *p); //數據輸入函數
void Insert(struct Student *p); //插入函數
void Sort(struct Student *p); //排序總函數
void Sort1(struct Student *p); //學號排序函數
void Sort2(struct Student *p); //成績排序函數
void Find(struct Student *p); //查找函數
void Delet(struct Student *p); //刪除函數
void Display(struct Student *p); //從函數中讀取輸出函數
void Display1(struct Student *p); //輸出函數
void out(); //退出函數
void Write(struct Student *,char s[]); //寫函數
void Read(struct Student *,char s[]); //讀函授
/*主函數開始*/
int main()
{ struct Student *p=NULL; //定義結點指針
for(;;) //死循環控制菜單
Caidan(p);
return 0;
} //主函數結束
void Caidan(struct Student *p) //菜單函數
{ int n;
n=print(); //調用菜單輸出函數
switch(n) //用switch做菜單選擇
{
case 1: Input(p);break; //輸入1調用Input函數
case 2:Insert(p);break; //輸入2調用Insert函數
case 3: Sort(p);break; //輸入3調用Sort函數
case 4:Find(p);break; //輸入4調用Find函數
case 5:Delet(p);break; //輸入5調用Delet函數
case 6:Display(p);break; //輸入6調用Display函數
case 7:out();break; //輸入7調用Out函數
}
} //菜單函數結束
int print() //菜單輸出函數
{
int n;
printf("********************目錄**********************\n");
printf("** 1.輸入 **\n");
printf("** 2.插入 **\n");
printf("** 3.排序 **\n");
printf("** 4.查找 **\n");
printf("** 5.刪除 **\n");
printf("** 6.輸出 **\n");
printf("** 7.退出 **\n");
printf("**********************************************\n");
printf("請從1--7選擇:");
scanf("%d",&n);
return n;
}
void Input(struct Student *p) //數據輸入函數
{ int count=0,N; //定義計數器count和要輸入的學生個數變量N
struct Student *a;
printf("請輸入要錄入的學生個數");
scanf("%d",&N);
printf("請輸入學號、姓名、性別、年齡和四科成績\n");
for(count=0;count<N;count++) //用循環來控制內存空間的分配,並錄入數據
{ if(count==0)
a=p=calloc(1,sizeof(structStudent));
if(count!=0)
{ p->next=calloc(1,sizeof(struct Student));
p=p->next;
}
if(count==N-1)
p->next=NULL;
scanf("%ld %s %s %d%f%f%f%f",&p->stu.number,p->stu.name,p->stu.sex,
&p->stu.age,&p->stu.score[0],&p->stu.score[1],&p->stu.score[2],&p->stu.score[3]);
p->stu.total=p->stu.score[0]+p->stu.score[1]+p->stu.score[2]+p->stu.score[3];
p->stu.averge=p->stu.total/4.0;
}
Sort1(a); //調用排序函數
Write(a,"stu.dat"); //調用寫入函數
} //數據輸入函數結束
void Write(struct Student *p,char s[]) //文件寫入函數
{ FILE *fp;
struct Student *a;
if((fp=fopen(s,"wb"))==NULL) //以二進制寫的方式打開文件
{ printf("error!!");
exit(0);
}
do
{ if(fwrite(p,sizeof(struct Student),1,fp)!=1)
printf("writeerror!!");
a=p;
p=p->next;
free(a); //內存釋放
}while(p!=NULL);
fclose(fp); //關閉文件
} //文件的寫函數結束
void Read(struct Student *p,char s[]) //文件的讀函數
{ FILE *fp;
struct Student *a,*b;
if((fp=fopen(s,"rb"))==NULL) //以二進制讀的方式打開文件
{ printf("read open error!");
exit(0);
}
a=p=calloc(1,sizeof(structStudent)); //分配內存空間
do
{ if(fread(p,sizeof(structStudent),1,fp)!=1) //讀到內存
printf("read error!!");
b=p->next;
if(b!=NULL)
{ p->next=calloc(1,sizeof(struct Student));
p=p->next;
}
}while(b!=NULL);
} //結束
void Display(struct Student *p) //從文件中讀並輸出到文件
{ struct Student *a;
p=Read(p,"stu.dat");
printf("學號 姓名 性別 年齡 高數 語文 英語 體育 總成績 平均成績\n");
do
{printf("%-5ld %-5s %-5s %-4d %-4.2f %-4.2f %-4.2f %-4.2f %-4.2lf %-4.2lf\n",p->stu.number,p->stu.name,
p->stu.sex,p->stu.age,p->stu.score[0],p->stu.score[1],p->stu.score[2],p->stu.score[3],p->stu.total,p->stu.averge);
a=p;
p=p->next;
free(a);
}while(p!=NULL);
} //文件結束
void Display1(struct Student *p) //輸出函數
{ struct Student *a;
printf("學號 姓名 性別 年齡 高數 語文 英語 體育 總成績 平均成績\n");
do
{ printf("%-5ld %-5s %-5s %-4d %-4.2f %-4.2f %-4.2f %-4.2f %-4.2lf %-4.2lf\n",p->stu.number,p->stu.name,
p->stu.sex,p->stu.age,p->stu.score[0],p->stu.score[1],p->stu.score[2],p->stu.score[3],p->stu.total,p->stu.averge);
a=p;
p=p->next;
}while(p!=NULL);
} //函數結束
void Insert(struct Student *p) //插入函數
{ int n,i,count;
struct Student *a,*c,*t,*head;
printf("請輸入要插入學生的個數:");
scanf("%d",&n);
head=Read(p,"stu.dat");
for(i=0;i<n;i++)
{ a=calloc(1,sizeof(structStudent));
scanf("%ld %s %s %d %f %f %f%f",&a->stu.number,a->stu.name,a->stu.sex,
&a->stu.age,&a->stu.score[0],&a->stu.score[1],&a->stu.score[2],&a->stu.score[3]);
a->stu.total=a->stu.score[0]+a->stu.score[1]+a->stu.score[2]+a->stu.score[3];
a->stu.averge=a->stu.total/4.0;
a->next=NULL;
count=0;
p=head;
do
{ if(a->stu.number<p->stu.number)
{ if(count==0)
{ a->next=p;
head=a;
}
else
{ t=c->next;
c->next=a;
a->next=t;
}
break;
}
c=p;
p=p->next;
count++;
}while(p!=NULL);
if(p==NULL)
{ c->next=a;
a->next=NULL;
}
}
Display1(head);
Write(head,"stu.dat");
} //函數結束
void Sort1(struct Student *p) //學號排序
{
int n=0,i,j;
struct Student1 t;
struct Student *p1,*a;
a=p;
do
{ p=p->next;
n++;
}while(p!=NULL);
p=a;
for(i=0;i<n-1;i++)
{ p=a;
for(j=0;j<n-1-i;j++)
{ p1=p->next;
if(p->stu.number>p1->stu.number)
{ t=p->stu;
p->stu=p1->stu;
p1->stu=t;
}
p=p1;
}
}
}
void Sort2(struct Student *p) //按成績排序函數
{ int n=0,i,j;
struct Student1 t;
struct Student *p1,*a;
a=Read(p,"stu.dat");
p=a;
do
{ p=p->next;
n++;
}while(p!=NULL);
p=a;
for(i=0;i<n-1;i++)
{ p=a;
for(j=0;j<n-1-i;j++)
{ p1=p->next;
if(p->stu.total>p1->stu.total)
{ t=p->stu;
p->stu=p1->stu;
p1->stu=t;
}
p=p1;
}
}
Display1(a);
} //函數結束
void Sort(struct Student *p) //總排序函數
{ printf("按學號排序:\n");
Display(p);
printf("按總成績排名:\n");
Sort2(p);
}
void out() //退出函數
{exit(0);} //函數結束
void Find(struct Student *p) //查找函數
{ long int num;
int n=0;
struct Student *a;
p=Read(p,"stu.dat");
printf("請輸入要查詢的學號:");
scanf("%ld",&num);
do
{if(num==p->stu.number)
{printf("學號 姓名 性別 年齡 高數 語文 英語 體育 總成績 平均成績\n");
printf("%-5ld%-5s %-5s %-4d %-4.2f %-4.2f %-4.2f%-4.2f %-4.2lf %-4.2lf\n",p->stu.number,p->stu.name,
p->stu.sex,p->stu.age,p->stu.score[0],p->stu.score[1],p->stu.score[2],p->stu.score[3],p->stu.total,p->stu.averge);
n=1;}
a=p;
p=p->next;
free(a);
}while(p!=NULL);
if(n==0)
printf("沒有你要查找的學生信息。\n");
}
void Delet(struct Student *p)
{ long int num;
int n=0;
struct Student *a,*head;
head=p=Read(p,"stu.dat");
printf("請輸入要刪除學生的學號:");
scanf("%ld",&num);
do
{ if(num==p->stu.number)
{ if(n==0)
{ head=p->next;
free(p);
break;
}
else
{ a->next=p->next;
free(p);
break;
}
}
a=p;
p=p->next;
}while(p!=NULL);
Display1(head);
Write(head,"stu.dat");}