#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"ctype.h"
#define N 3
#define Filename "student.txt"
typedef struct z1
{
char no[11];
char name[15];
float sum;
int score[N];
float average;
int order;
struct z1 *next;
}STUDENT;
STUDENT *init();
STUDENT *create(); /*创建链表*/
STUDENT *Delete(STUDENT *h); /*删除记录*/
void print(STUDENT *h); /*显示所有记录*/
void search(STUDENT *h); /*查找*/
void save(STUDENT *h); /*保存*/
STUDENT *load();
void computer(STUDENT *h);
STUDENT *insert(STUDENT *h);
STUDENT *amend(STUDENT *h);
void append(); /*追加记录*/
void copy(); /*复制文件*/
STUDENT *sort(STUDENT *h); /*排序*/
STUDENT *index(STUDENT *h); /*索引*/
int menu_select();
void main()
{
//int i;
STUDENT *head;
head=init();
//clrscr();
while(1)
{
switch(menu_select())
{
case 0:head=init();break;
case 1:head=create();break;
case 2:head=Delete(head);break;
case 3:print(head);break;
case 4:search(head);break;
case 5:save(head);break;
case 6:head=load();break;
case 7:computer(head);break;
case 8:head=insert(head);break;
case 9:amend(head);break;
case 10:head=sort(head);break;
case 11:append();break;
case 12:head=index(head);break;
case 13:return;
}
}
}
int menu_select()
{
char *menu[]={"\n************MENU***********", /*定义菜单字符*/
" 0.格式化当前信息",
" 1.输入学生信息",
" 2.删除学生信息",
" 3.打印当前数据库信息",
" 4.查询学生信息",
" 5.存盘",
" 6.读盘",
" 7.计算总成绩",
" 8.在当前数据添加学生信息",
" 9.修改学生信息",
" 10.学生成绩排序",
" 11.直接添加信息到数据库",
" 12.学生学号排序",
" 13.退出系统"
};
char s[3];
int c,i;
for(i=0;i<15;i++)
{
printf("%s",menu[i]);
printf("\n");
}
do{
printf("\n 请选择(0-13):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>14);
return c;
}
STUDENT *init()
{
return NULL;
}
STUDENT *create() /*创建链表*/
{
int i,s;
STUDENT *h=NULL,*info;
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\n out of memory");
return NULL;
}
printf("\n输入学号(以@符号结束):");scanf("%s",info->no);
if(info->no[0]=='@')break;
printf("输入姓名:");scanf("%s",info->name);
printf(" 输入%d门成绩 \n",N);
s=0;
for(i=0;i<N;i++)
{
do
{
printf("score%d:",i+1);
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)printf("bad date,input repeat");
} while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i];
}
info->sum=s;
info->average=(float)s/N;
info->order=0;
info->next=h; /*将头节点作为新输入节点的后继节点*/
h=info;/*新输入节点为新的头节点 */
}
return(h);
}
void append()
{
FILE *fp;
STUDENT *info;
int s1,i;
printf("\n追加学生信息\n");
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\nout of memory");
return;
}
printf("输入学号:");scanf("%s",info->no);
printf("输入姓名:");scanf("%s",info->name);
printf("输入 %d 门成绩 \n",N);
s1=0;
for(i=0;i<N;i++)
{
do
{
printf("score%d:",i+1);
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)printf("bad date,input repeat");
} while(info->score[i]>100||info->score[i]<0);
s1=s1+info->score[i];
}
info->sum=s1;
info->average=(float)s1/N;
info->order=NULL;
info->next=NULL;
if((fp=fopen(Filename,"ab"))==NULL)
{
printf("can not open file\n");
exit(1);
}
printf("\n----Appending record!-----");
if(1!=fwrite(info,sizeof(STUDENT),1,fp))
{
printf("----file write error!----\n");
return;
}
printf("----append sucess----\n");
fclose(fp);
}
STUDENT *insert(STUDENT *h)
{
STUDENT *p,*q,*info;
char s[11];
int s1,i;
printf("\n你希望在几号学生之前追加一条记录");
scanf("%s",s);
printf("\n添加记录");
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\nout of memory"); /*如果没有申请到,内存溢出*/
return NULL;
}
printf("\n输入学号:");scanf("%s",info->no) ;
printf("\n输入姓名:");scanf("%s",info->name);
printf("\n输入 %d门成绩\n",N);
s1=0;
for(i=0;i<N;i++)
{
do
{
printf("score%d:",i+1);
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)printf("bad date,input repeat");
} while(info->score[i]>100||info->score[i]<0);
s1=s1+info->score[i];
}
info->sum=s1;
info->average=(float)s1/N;
info->order=0;
info->next=NULL;
p=h;
q=h;
while(strcmp(p->no,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
if(p==h)
h=info;
else
q->next=info;
else
if(p==h)
{
info->next=p;
h=info;
}
else
{
info->next=p;
q->next=info;
}
printf("\n------Have Inserted %s Student-------\n",info->name);
printf("------Do Not Forger Save Disk!-------\n");
return(h);
}
STUDENT *index(STUDENT *h)
{
STUDENT *p,*q,*t,*h1;
h1=h->next;
h->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while(t->no!=p->no&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
printf("index sucess!!!\n");
return h;
}
void computer(STUDENT *h)
{
STUDENT *p;
int j=0;
long s=0;
float average=0.;
p=h;
if(p==NULL)
{
printf("No Information");
return;
}
else
{
while(p!=NULL)
{
j++;
s+=p->sum;
p=p->next;
}
average=(float)s/j;
printf("\n所有学生总分:%d",s);
printf("\n平均分:%4.2f",average);
}
}
STUDENT *sort(STUDENT *h)
{
int i=0;
STUDENT *p,*q,*t,*h1;
h1=h->next;
h->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while(t->sum<p->sum&&p!=NULL)
{
q=p;
p=q->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
p=h;
while(p!=NULL)
{
i++;
p->order=i;
p=p->next;
}
printf("sort sucess!!!\n");
return h;
}
STUDENT *load()
{
STUDENT *p,*q,*h=NULL;
FILE *fp;
if((fp=fopen(Filename,"rb"))==NULL) /* 只读方式打开一个二进制文件 */
{
printf("Can't open file\n");
exit(1);
}
printf("\nLoading file... ....\n");
p=(STUDENT *)malloc(sizeof(STUDENT));
if(!p)
{
printf("out of memory!\n"); /* 内存溢出 */
return h;
}
h=p;
while(!feof(fp)) /* 循环至文件尾 */
{
if(1!=fread(p,sizeof(STUDENT),1,fp))
break;
p->next=(STUDENT *)malloc(sizeof(STUDENT));
if(!p->next)
{
printf("out of memory\n");
return h;
}
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("\n-----------Load File Success!!----------");
return h;
}
void save(STUDENT *h)
{
FILE *fp;
STUDENT *p;
if((fp=fopen(Filename,"wb"))==NULL) /* 输出打开一个二进制文件,没有则建立 */
{
printf("Can't open file\n");
return;
}
printf("\nSaving file... ...\n");
p=h;
while(p!=NULL)
{
fwrite(p,sizeof(STUDENT),1,fp);/*写入一条记录*/
p=p->next;
}
fclose(fp);
printf("--------Save Success!!-------");
}
void search(STUDENT *h)
{
STUDENT *p;
char s[15];
printf("please enter name for search\n");
scanf("%s",s);
p=h;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
if(p==NULL)
printf("\nList no %s student's information",s);
else
{
printf("\n\n\n--------------------------FOUND------------------------------\n");
printf("| no | name | sc1| sc2| sc3| sum | ave | order |\n");
printf("| | | | | | | | |\n");
printf("|%7s|%12s|%4d|%4d|%4d| %6.2f| %6.2f|%7d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("----------------------------END------------------------------\n");
}
}
void print(STUDENT *h)
{
int i=0;
STUDENT *p;
p=h;
printf("\n\n\n--------------------------STUDENT-----------------------------\n");
printf("| no | name | sc1| sc2| sc3| sum | ave | order |\n");
printf("| | | | | | | | |\n");
while(p!=NULL)
{
i++;
printf("|%7s|%12s|%4d|%4d|%4d| %6.2f| %6.2f|%7d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("----------------------------END------------------------------\n");
}
STUDENT *Delete(STUDENT *h)
{
STUDENT *p,*q;
char s[11],f[4];
printf("Please deleted no\n");
scanf("%s",s);
q=p=h;
while(strcmp(p->no,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
{
printf("\nList No %s Student",s);
return(h);
}
else
{
printf("-------------------Have--------------Found------------------\n");
printf("| no | name | sc1| sc2| sc3| sum | ave | order |\n");
printf("| | | | | | | | |\n");
printf("|%7s|%12s|%4d|%4d|%4d| %6.2f| %5.2f|%7d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("| | | | | | | | |\n");
printf("---------------------------------END-------------------------\n");
printf("press \"yes\" to be sure that you want to delete the student\n");
scanf("%s",f);
if(!strcmp(f,"yes")||!strcmp(f,"YES"))
{
if(p==h)
h=p->next;
else
q->next=p->next;
free(p);
printf("\nHave Deleted No %s Student\n",s);
printf("Don't Forget Save Disk!");
}
else
{
printf("Give Up delete!%s",f);
return(h);
}
}
return(h);
}
STUDENT *amend(STUDENT *h)
{
int i;
STUDENT *p;
char no[11];
printf("please the number of the student for amend\n");
scanf("%s",no);
p=h;
while(strcmp(p->no,no)&&p!=NULL)
p=p->next;
if(p==NULL)
printf("\nList no NO.%s student's information",no);
else
{
printf("\n\n\n--------------------------FOUND------------------------------\n");
printf("| no | name | sc1| sc2| sc3| sum | ave | order |\n");
printf("| | | | | | | | |\n");
printf("|%7s|%12s|%4d|%4d|%4d| %6.2f| %5.2f|%7d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("----------------------------END------------------------------\n");
}
printf("\nNow please input the student's information again for amend");
printf("\nendter the number:"); scanf("%s",p->no);
printf("\nendter the name:"); scanf("%s",p->name);
p->sum=0;
for(i=0;i<N;i++)
{
do
{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]>100||p->score[i]<0)printf("bad date,input repeat");
} while(p->score[i]>100||p->score[i]<0);
p->sum+=p->score[i];
}
p->average=(float)p->sum/N;
return(h);
}