#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int id,num,xuehao,tmp;
double scoure,res,loww,highh;
string name;
map<string,double>map_str;
map<int,double>map_int;
vector<pair<string,int> >v;
bool cmp_str_high_first(pair<string,int> a,pair<string,int> b) {
return a.first>b.first;
}
bool cmp_str_low_first(pair<string,int> a,pair<string,int> b) {
return a.first<b.first;
}
bool cmp_int_high_first(pair<string,int> a,pair<string,int> b) {
return a.second>b.second;
}
bool cmp_int_low_first(pair<string,int> a,pair<string,int> b) {
return a.second<b.second;
}
bool cmp_scoure_high_first(pair<string,int> a,pair<string,int> b) {
return map_int[a.second]>map_int[b.second];
}
bool cmp_scoure_low_first(pair<string,int> a,pair<string,int> b) {
return map_int[a.second]<map_int[b.second];
}
int main() {
begin:
puts("请问我能为您做什么?");
puts("我的功能有:");
puts(
"1.插入学生成绩。\n"
"2.查询成绩,按照输入的姓名或学号查询对应成绩。\n"
"3.显示所有成绩信息。\n"
"4.输入姓名,修改成绩信息。\n"
"5.给定姓名,删除该成绩信息。\n"
"6.统记人数,计算总分,平均分,最高分,最低分。\n"
"7.保存成绩到文件并退出。\n"
"8.从文件读取成绩。\n"
"9.给成绩排序(可选排序字段及升,降序)。\n"
);
puts("请输入编号:");
cin>>id;
system("cls");
switch(id) {
case 1:
puts("请输入学生数");
cin>>num;
for(int i=1; i<=num; i++) {
puts("请输入学号");
cin>>xuehao;
puts("请输入名字");
cin>>name;
puts("请输入成绩");
cin>>scoure;
map_str[name]=map_int[xuehao]=scoure;
v.push_back(make_pair(name,xuehao));
}
break;
case 2:
puts("请输入查询次数");
cin>>num;
for(int i=1; i<=num; i++) {
puts("如需用学号查询,请输入1,用名字查询,请输入2");
cin>>tmp;
if(tmp==1) {
puts("请输入学号");
cin>>xuehao;
printf("%d号的成绩为%.2lf\n",xuehao,map_int[xuehao]);
} else {
puts("请输入名字");
cin>>name;
printf("%s的成绩为%.2lf\n",name.c_str(),map_str[name]);
}
}
break;
case 3:
if(v.empty())
puts("还没有学生。");
else
for(vector<pair<string,int> >::iterator it=v.begin(); it!=v.end(); it++)
printf("%s,%d号,成绩为:%.2lf\n",it->first.c_str(),it->second,map_str[it->first]);
break;
case 4:
puts("请输入您要修改的学生的姓名");
cin>>name;
tmp=0x7fffffff;
for(unsigned i=0; i<v.size(); i++)
if(v[i].first==name) {
tmp=i;
xuehao=v[i].second;
break;
}
if(tmp==0x7fffffff) {
puts("学生不存在!");
break;
}
printf("请输入%s的修改后分数:",name.c_str());
cin>>scoure;
map_str[name]=map_int[xuehao]=scoure;
puts("修改完毕!");
break;
case 5:
puts("请输入您要删除的学生的姓名");
cin>>name;
tmp=0x7fffffff;
for(vector<pair<string,int> >::iterator it=v.begin(); it!=v.end(); it++)
if(it->first==name) {
tmp=1;
v.erase(it);
xuehao=it->second;
break;
}
if(tmp==0x7fffffff) {
puts("学生不存在!");
break;
}
map_int[xuehao]=map_str[name]=0;
puts("删除完毕!");
break;
case 6:
res=0,highh=-1,loww=10000001;
for(unsigned i=0; i<v.size(); i++)
res+=map_int[v[i].second],highh=max(highh,map_int[v[i].second]),loww=min(loww,map_int[v[i].second]);
if(v.size())
printf("人数:%u,总分:%.2lf,平均分:%.3lf,最高分:%.2lf,最低分:%.2lf\n",unsigned(v.size()),res,double(res)/v.size(),highh,loww);
else
printf("没有学生!");
break;
case 7:
puts("文件保存在目录下的'scoure.txt':");
freopen("scoure.txt","w",stdout);
for(unsigned i=0; i<v.size(); i++)
printf("%s %d -> %.2lf\n",v[i].first.c_str(),v[i].second,map_int[v[i].second]);
return 0;
case 8:
puts("正在读取'scoure.txt':");
freopen("scoure.txt","r",stdin);
while(cin>>name>>xuehao>>scoure) {
v.push_back(make_pair(name,xuehao));
map_int[xuehao]=map_str[name]=scoure;
}
puts("读取完毕!");
break;
case 9:
r:
puts("排序:请输入关键字");
puts("1:按名字降序排");
puts("2:按名字升序排");
puts("3:按学号降序排");
puts("4:按学号升序排");
puts("5:按成绩降序排");
puts("6:按成绩升序排");
cin>>tmp;
if(v.size()) {
if(tmp==1)
sort(v.begin(),v.end(),cmp_str_high_first);
else if(tmp==2)
sort(v.begin(),v.end(),cmp_str_low_first);
else if(tmp==3)
sort(v.begin(),v.end(),cmp_int_high_first);
else if(tmp==4)
sort(v.begin(),v.end(),cmp_int_low_first);
else if(tmp==5)
sort(v.begin(),v.end(),cmp_scoure_high_first);
else if(tmp==6)
sort(v.begin(),v.end(),cmp_scoure_low_first);
else {
puts("请重新输入");
system("cls");
goto r;
}
} else {
printf("没有学生!");
}
break;
default:
puts("???");
break;
}
puts("按任意键继续");
getch();
system("cls");
goto begin;
return 0;
}
温馨提示:答案为网友推荐,仅供参考