#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#define LENGTH 1000
int longth=1; //用于重分配内存
const int nnumber=0,nrank=1,nscore=2;//按学号 名词 总分 排序
typedef struct Birthday
{ int year;
int month;
int day;
}Birthday;
typedef struct Sstu
{ char name[20];
char sex[10];
Birthday birthday;
char colloge[20];
int grade;
char major[20];
int info[10];
}Sstu;
typedef struct Student
{ int stunumber;
int scorenumber;
char information[10][10];
Sstu *hole;
}Student;
void Editdeal(Student *S);
void Filedeal(Student *S);
void Fileread(FILE *fp,Student *S);
void Filesave(FILE *fp,Student *S);
void Filesaveother(FILE *fp,Student *S);
int Find(int id,Student *S,int flag);
int Getint(void);
void InitS(Student *S);
void Read(Student *S);
void Searchdeal(Student *S);
void Sortdeal(Student *S);
void Sort(int m,Student *S,int start,int end);
int Partition(int m,Student *S,int start,int end);
void Sort2(int m,Student *S,int start,int end);
int Partition2(int m,Student *S,int start,int end);
void Sort3(int m,Student *S,int start,int end);
void dellast(void);
void Print(Student *S);
void main(void)
{ Student S;
int flag;
FILE *fp=0;
InitS(&S);
if((fp=fopen("配置文件.txt","rb"))!=0)
{ printf("\n正在读取配置文件,请稍等、、、");
Fileread(fp,&S);
fclose(fp);
printf("\n读取完毕");
}
else
{ printf("\n首次使用没有生成配置文件,请首先输入学生成绩等信息");
Read(&S);
fp=fopen("配置文件.txt","wb");
Filesave(fp,&S);
fclose(fp);
}
printf("\n---------------------------------------------------------");
printf("\n| |");
printf("\n| o(∩_∩)o... 欢迎使用学生成绩管理系统 |");
printf("\n| |");
do
{ printf("\n|---------------------------------------------------|");
printf("\n|1,文件||2,编辑||3,查找||4,排序||5,输出||0,退出 |");
printf("\n|---------------------------------------------------|\n选择=");
flag=Getint();
switch(flag)
{ case 1: Filedeal(&S); break;
case 2: Editdeal(&S); break;
case 3: Searchdeal(&S); break;
case 4: Sortdeal(&S); break;
case 5: Print(&S); break;
case 0: break;
default : printf("\n选择无效,请重新选择"); break;
}
}while(flag);
printf("\n你想保存操作吗?\n1,保存||0,不保存\n选择=");
flag=Getint();
if(flag)
{ if((fp=fopen("配置文件.txt","wb"))!=0)
{ Filesave(fp,&S);
fclose(fp);
}
else printf("\n保存失败");
}
printf("\n谢谢使用");
}
void Filedeal(Student *S)
{ int flag;
FILE *fp=0;
char address[50];
printf("\n1,重新读取||2,重新键盘输入||3,保存||4,另存为||0,退出\n选择=");
flag=Getint();
switch(flag)
{ case 1: if((fp=fopen("配置文件.txt","rb"))!=0)
{ Fileread(fp,S);
fclose(fp);
}
else printf("\n读取失败");
break;
case 2: Read(S); break;
case 3: if((fp=fopen("配置文件.txt","wb"))!=0)
{ Filesave(fp,S);
fclose(fp);
}
else printf("\n保存失败");
break;
case 4: if(S->stunumber==0)
{ printf("没有学生的信息!");
return;
}
printf("\n请输入地址如:D:\\文件\\成绩.txt,保存到当前文件夹如:成绩.txt\n文件地址=");
gets(address);
if((fp=fopen(address,"w"))!=0)
{ Filesaveother(fp,S);
fclose(fp);
}
else printf("\n保存失败");
break;
case 0: break;
default : printf("\n选择无效");
}
}
void Editdeal(Student *S)
{ int flag,flag2,id,i,j,sum=0;
printf("\n1,插入||2,删除||3,编辑某个学生信息||0,退出\n选择=");
flag2=Getint();
switch(flag2)
{ case 1:
if(S->stunumber+1>=longth*LENGTH)
{ longth++;
while(!(S->hole=(Sstu *)realloc(S->hole,(longth*LENGTH)*sizeof(Sstu))))
continue;
}
Sort(nnumber,S,0,S->stunumber-1);//快速排序按学号排序////////////////////////////////////////
printf("\n请输入学号:\n学号=");
id=Getint();
flag=1;
i=Find(id,S,flag);
if(i<0)
{ printf("\n学号不能小于0!!");
return;
}
for(j=S->stunumber;j>i;j--)//////////////////
S->hole[j]=S->hole[j-1];
S->hole[i].info[nnumber]=id;
printf("\n请输入姓名\n姓名=");
gets(S->hole[i].name);
printf("\n请输入性别\n性别=");
gets(S->hole[i].sex);
printf("\n请输入出生日期 如2009,08,15\n出生日期=");
scanf("%d,%d,%d",&(*S).hole[i].birthday.year,&(*S).hole[i].birthday.month,&(*S).hole[i].birthday.day);
dellast();
printf("\n请输入所在学院\n学院=");
gets(S->hole[i].colloge);
printf("\n请输入所在专业\n专业=");
gets(S->hole[i].major);
printf("\n请输入入学时间\n入学时间=");
S->hole[i].grade=Getint();
for(j=0;j<S->scorenumber;j++)//从单科成绩开始输入
{ printf("\n请输入 %s 的成绩",S->information[j+3]);
sum+=S->hole[i].info[j+3]=Getint();
}
S->hole[i].info[nscore]=sum;
S->stunumber++;
Sort3(nscore,S,0,S->stunumber);
for(i=0;i<S->stunumber;i++)
S->hole[i].info[nrank]=i+1;
break;
case 2: if(S->stunumber==0)
{ printf("没有学生的信息!");
return;
}
Sort(nnumber,S,0,S->stunumber-1);
printf("\n请输入要删除的学号\n学号=");
id=Getint();
flag=0;
i=Find(id,S,flag);//二分查找返回结构体的位置
if(i<0||i>S->hole[S->stunumber-1].info[nnumber])
{ printf("\n没有学号是%d的学生的信息",id);
return;
}
for(j=i;j<S->stunumber;j++)
S->hole[j]=S->hole[j+1];
S->stunumber--;
Sort3(nscore,S,0,S->stunumber-1);
for(i=0;i<S->stunumber;i++)
S->hole[i].info[nrank]=i+1;
break;
case 3: if(S->stunumber==0)
{ printf("没有学生的信息!");
return;
}
Sort(nnumber,S,0,S->stunumber-1);
printf("\n请输入要编辑的学号\n学号=");
id=Getint();
flag=0;
i=Find(id,S,flag);
if(i<0||i>=S->stunumber)
{ printf("\n没有学号是%d的学生的信息3",id);
return;
}
printf("\n学号是%d的学生信息如下",id);
printf("\n-------------------------------------------------------------");
printf("\n学号:%d\t姓名:%s\t",S->hole[i].info[nnumber],S->hole[i].name);
printf("\n出生日期:%d年%d月%d日",S->hole[i].birthday.year,S->hole[i].birthday.month,S->hole[i].birthday.day);
printf("\n学院:%s\t专业:%s\t年级:%d",S->hole[i].colloge,S->hole[i].major,S->hole[i].grade);
printf("\n排名:%d\t总分:%d",S->hole[i].info[nrank],S->hole[i].info[nscore]);
for(j=0;j<S->scorenumber;j++)
printf("\t%S:%d",S->information[j+3],S->hole[i].info[j+3]);
printf("\n-------------------------------------------------------------");
printf("\n请输入新的学号\n学号=");
S->hole[i].info[nnumber]=Getint();
printf("\n请输入姓名\n姓名=");
gets(S->hole[i].name);
printf("\n请输入性别\n性别=");
gets(S->hole[i].sex);
printf("\n请输入出生日期 如2009,08,15\n出生日期=");
scanf("%d,%d,%d",&(*S).hole[i].birthday.year,&(*S).hole[i].birthday.month,&(*S).hole[i].birthday.day);
dellast();
printf("\n请输入所在学院\n学院=");
gets(S->hole[i].colloge);
printf("\n请输入所在专业\n专业=");
gets(S->hole[i].major);
printf("\n请输入入学时间\n入学时间=");
S->hole[i].grade=Getint();
for(j=0;j<S->scorenumber;j++)//从单科成绩开始输入
{ printf("\n请输入 %s 的成绩",S->information[j+3]);
sum+=S->hole[i].info[j+3]=Getint();
}
S->hole[i].info[nscore]=sum;
Sort3(nscore,S,0,S->stunumber-1);
for(i=0;i<S->stunumber;i++)
S->hole[i].info[nrank]=i+1;
break;
case 0: break;
default : break;
}
}
void Searchdeal(Student *S)
{ int id,i,j,flag=0;
if(S->stunumber==0)
{ printf("没有学生的信息!");