#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
struct sa
{
int num;
char name[105];
char sex[3];
double score[5];
double ave;
double sum;
int rank;
}data[105];
int cmp_upid(const sa&a, const sa&b)
{
return a.num<b.num;
}
int cmp_downid(const sa&a, const sa&b)
{
return a.num>b.num;
}
int cmp_upname(const sa&a, const sa&b)
{
return strcmp(a.name,b.name)<0;
}
int cmp_downname(const sa&a, const sa&b)
{
return strcmp(a.name,b.name)>0;
}
int cmp_uprank(const sa&a, const sa&b)
{
return a.sum<b.sum;
}
int cmp_downrank(const sa&a, const sa&b)
{
return a.sum> b.sum;
}
int _k=0,tmp=0;
void add()
{
printf("学号:");
scanf("%d",&data[_k].num);
printf("姓名:");
cin>>data[_k].name;
printf("性别:");
scanf("%s",data[_k].sex);
data[_k].sum=0.0;
for(int i=0;i<5;i++)
{
cin>>data[_k].score[i];
data[_k].sum+=data[_k].score[i];
}
data[_k].ave=data[_k].sum/5.0;
_k++;
return;
}
void show()
{
for(int i=0; i<_k; i++)
printf("%d %s %s %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %d\n",data[i].num,data[i].name,data[i].sex,data[i].score[0],
data[i].score[1],data[i].score[2],data[i].score[3],data[i].score[4],data[i].ave,data[i].sum,data[i].rank);
}
struct sa *Find()
{
int a;
printf("请输入学生的学号\n");
cin>>a;
int flag=0;
struct sa *p;
for(int i=0;i<_k;i++)
if(data[i].num==a)
{
printf("%d %s %s %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %d\n",data[i].num,data[i].name,data[i].sex,data[i].score[0],
data[i].score[1],data[i].score[2],data[i].score[3],data[i].score[4],data[i].ave,data[i].sum,data[i].rank);
flag=1;
p=&data[i];
return p;
}
if(flag==0)
{
puts("Sorry,您输入的学号错误,请重新输入");
return NULL;
}
}
struct sa * xg()
{
int num0,j,m;
double sc;
printf("请输入学生的学号 输入学生的课程代号 输入学生修改后的成绩\n");
cin>>num0>>j>>sc;
struct sa *p;
for(int i=0;i<_k;i++)
{
if(data[i].num==num0)
{
data[i].sum=0.0;
for(int k=0;k<5;k++)
{
if(k==j)
data[i].score[k]=sc;
data[i].sum+=data[i].score[k];
}
data[i].ave=data[i].sum/3.0;
p=&data[i];
}
}
return p;
}
struct sa *del()
{
int num0;
puts("请输入要删除的学号");
cin>>num0;
struct sa *p;
for(int i=0;i<_k;i++)
{
if(data[i].num==num0)
{
p=&data[i];
}
else
printf("%d %s %s %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf\n",data[i].num,data[i].name,data[i].sex,data[i].score[0],
data[i].score[1],data[i].score[2],data[i].score[3],data[i].score[4],data[i].ave,data[i].sum);
}
return p;
}
void sort_pe()
{
cout<<"0.按学生学号排序:"<<endl;
cout<<"1.按学生姓名排序:"<<endl;
cout<<"2.按学生排名排序:"<<endl;
int m,n;
cin>>m>>n;
switch(m)
{
case 0:
{
cout<<"1.升序:"<<endl;
cout<<"0.降序:"<<endl;
if(n == 1)
sort(data,data+_k,cmp_upid);
else if(n == 0)
sort(data,data+_k,cmp_downid);
else
cout<<"Sorry"<<endl;
}
case 1:
{
cout<<"1.升序:"<<endl;
cout<<"0.降序:"<<endl;
if(n%2 == 1)
sort(data,data+_k,cmp_upname);
else
sort(data,data+_k,cmp_downname);
}
case 2:
{
cout<<"1.升序:"<<endl;
cout<<"0.降序:"<<endl;
if(n%2 == 1)
sort(data,data+_k,cmp_uprank);
else
sort(data,data+_k,cmp_downrank);
}
}
for(int i=0; i<_k; i++)
{
int ans=1;
for(int j=0; j<_k; j++)
if(data[i].sum<data[j].sum)
ans++;
data[i].rank=ans;
}
}
void save(char *filename)
{
FILE *fp = fopen(filename,"at");
for(int i=tmp; i<_k; i++)
{
fprintf(fp,"%d %s %s %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %d\n",data[i].num,data[i].name,data[i].sex,data[i].score[0],
data[i].score[1],data[i].score[2],data[i].score[3],data[i].score[4],data[i].ave,data[i].sum,data[i].rank);
}
fclose(fp);
tmp = _k;
return ;
}
void read(char *filename)
{
char temp;
FILE *fp;
fp = fopen(filename,"r");
while((temp = fgetc(fp))!=EOF)
{
printf("%c",temp);
}
fclose(fp);
return ;
}
void build()
{
cout<<"请输入新建文件名称: ";
}
int main()
{
while(1)
{
cout<<"**********************************************************************"<<endl;
cout<<"〓〓〓〓〓〓 学 生 成 绩 管 理 系 统 〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓 〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 1.增加学生信息 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 2.修改学生信息 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 3.显示学生信息 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 4.查询学生信息 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 5.删除学生信息 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 6.对学生信息进行排序 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 7.保存学生信息至记录文件 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 8.从记录文件读取学生信息 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 9.新建学生信息文件 〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓 0.运行结束 〓〓〓〓〓〓〓"<<endl;
cout<<"\n\t\t\n\t\t请选择:"<<endl;
int m;
cin>>m;
switch(m)
{
case 0:
return 0;
case 1:
add();break;
case 2:
xg();break;
case 3:
show();break;
case 4:
Find();break;
case 5:
del(); break;
case 6:
sort_pe();break;
case 7:
save("学生管理系统.txt");break;
case 8:
read("学生管理系统.txt");break;
case 9:
build();break;
default :
puts("Sorry,您输入的编号不在操作步骤里面,请重新选择,谢谢\n");break;
}
cout<<endl;
cout<<"---------------lalalalala--------------"<<endl;
cout<<endl;
}
return 0;
}