解题思路:我用了一个映射来进行学生学号是否存在的判断。。悲催的是一开始将 p.erase(sid[i])和sid[i].clear()的顺序搞反了。。然后就悲剧了一下午~~
还需要注意浮点误差~~
#include<iostream>
#include<map>
#include<algorithm>
#include<iomanip>
using namespace std;
const double eps=1e-5;
map<string,string > p;
string sid[620],name[620];
int maxn=0,cid[620],score[620][20];
void add(){
for(;;) {
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish.\n";
cin>>sid[maxn]; if(sid[maxn]=="0") { sid[maxn].clear(); break; }
cin>>cid[maxn]>>name[maxn]>>score[maxn][0]>>score[maxn][1]>>score[maxn][2]>>score[maxn][3];
if(p.count(sid[maxn])) {
cout<<"Duplicated SID.\n"; sid[maxn].clear();name[maxn].clear();
}
else {
score[maxn][4]=0;score[maxn][4]=score[maxn][0]+score[maxn][1]+score[maxn][2]+score[maxn][3];
p[sid[maxn]]=name[maxn];
maxn++;
}
}
}
void stat(){
cout<<"Please enter class ID, 0 for the whole statistics.\n";
int ID; cin>>ID;
if(ID==0){
int china=0,china_past=0,china_failed=0,math=0,math_past=0,math_failed=0,english=0,english_past=0,english_failed=0,
com=0,com_past=0,com_failed=0,past_all=0,failed_all=0,past_1=0,past_2=0,past_3=0,kase=0;
for(int i=0;i<maxn;i++){
if(!sid[i].empty()) {
int past=0;kase++;
china+=score[i][0]; math+=score[i][1]; english+=score[i][2]; com+=score[i][3];
if(score[i][0]>=60) { china_past++; past++; } else china_failed++;
if(score[i][1]>=60) { math_past++; past++; } else math_failed++;
if(score[i][2]>=60) { english_past++;past++; }else english_failed++;
if(score[i][3]>=60) { com_past++; past++; } else com_failed++;
if(past==4) past_all++;
if(past==0) failed_all++;
if(past>=1) past_1++;
if(past>=2) past_2++;
if(past>=3) past_3++;
}
}
cout<<"Chinese\n"; cout<<"Average Score: "<<china*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<china_past<<'\n'; cout<<"Number of failed students: "<<china_failed<<"\n\n";
cout<<"Mathematics\n"; cout<<"Average Score: "<<math*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<math_past<<"\n"; cout<<"Number of failed students: "<<math_failed<<"\n\n";
cout<<"English\n"; cout<<"Average Score: "<<english*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<english_past<<"\n"; cout<<"Number of failed students: "<<english_failed<<"\n\n";
cout<<"Programming\n"; cout<<"Average Score: "<<com*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<com_past<<"\n"; cout<<"Number of failed students: "<<com_failed<<"\n\n";
cout<<"Overall:\n"; cout<<"Number of students who passed all subjects: "<<past_all<<"\n";
cout<<"Number of students who passed 3 or more subjects: "<<past_3<<"\n"; cout<<"Number of students who passed 2 or more subjects: "<<past_2<<"\n";
cout<<"Number of students who passed 1 or more subjects: "<<past_1<<"\n"; cout<<"Number of students who failed all subjects: "<<failed_all<<"\n\n";
}
else {
int china=0,china_past=0,china_failed=0,math=0,math_past=0,math_failed=0,english=0,english_past=0,english_failed=0,
com=0,com_past=0,com_failed=0,past_all=0,failed_all=0,past_1=0,past_2=0,past_3=0,kase=0;
for(int i=0;i<maxn;i++){
if(!sid[i].empty()&&ID==cid[i]) {
int past=0;kase++;
china+=score[i][0]; math+=score[i][1]; english+=score[i][2]; com+=score[i][3];
if(score[i][0]>=60) { china_past++; past++; } else china_failed++;
if(score[i][1]>=60) { math_past++; past++; } else math_failed++;
if(score[i][2]>=60) { english_past++;past++; }else english_failed++;
if(score[i][3]>=60) { com_past++; past++; } else com_failed++;
if(past==4) past_all++;
if(past==0) failed_all++;
if(past>=1) past_1++;
if(past>=2) past_2++;
if(past>=3) past_3++;
}
}
cout<<"Chinese\n"; cout<<"Average Score: "<<china*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<china_past<<'\n'; cout<<"Number of failed students: "<<china_failed<<"\n\n";
cout<<"Mathematics\n"; cout<<"Average Score: "<<math*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<math_past<<"\n"; cout<<"Number of failed students: "<<math_failed<<"\n\n";
cout<<"English\n"; cout<<"Average Score: "<<english*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<english_past<<"\n"; cout<<"Number of failed students: "<<english_failed<<"\n\n";
cout<<"Programming\n"; cout<<"Average Score: "<<com*1.0/(kase*1.0)+eps<<"\n";
cout<<"Number of passed students: "<<com_past<<"\n"; cout<<"Number of failed students: "<<com_failed<<"\n\n";
cout<<"Overall:\n"; cout<<"Number of students who passed all subjects: "<<past_all<<"\n";
cout<<"Number of students who passed 3 or more subjects: "<<past_3<<"\n"; cout<<"Number of students who passed 2 or more subjects: "<<past_2<<"\n";
cout<<"Number of students who passed 1 or more subjects: "<<past_1<<"\n"; cout<<"Number of students who failed all subjects: "<<failed_all<<"\n\n";
}
}
int rankk(int t){
int kase=1;
for(int i=0;i<maxn;i++)
if(!sid[i].empty()&&score[i][4]>score[t][4])
kase++;
return kase;
}
void DQ(int isq){
string s;
for(;;) {
cout<<"Please enter SID or name. Enter 0 to finish.\n";
cin>>s;
if(s=="0") break;
int r=0;
for(int i=0;i<maxn;i++) if(!sid[i].empty()) {
if(sid[i]==s||name[i]==s) {
if(isq) { cout<<rankk(i)<<' '<<sid[i]<<' '<<cid[i]<<' '<<name[i]<<' '<<score[i][0]<<' '<<score[i][1]<<' '
<<score[i][2]<<' '<<score[i][3]<<' '<<score[i][4]<<' '<<score[i][4]/4.0+eps<<'\n';}
else { r++; p.erase(sid[i]); sid[i].clear(); name[i].clear();
}
}
}
if(!isq) cout<<r<<" student(s) removed.\n";
}
}
void print_menu(){
cout<<"Welcome to Student Performance Management System (SPMS).\n\n";
cout<<"1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n";
}
int main(){
ios::sync_with_stdio(false);
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
for(;;){
int choice;
print_menu();
cin>>choice;
if(choice==0) break;
if(choice==1) add();
if(choice==2) DQ(0);
if(choice==3) DQ(1);
if(choice==4) cout<<"Showing the ranklist hurts students' self-esteem. Don't do that.\n";
if(choice==5) stat();
}
return 0;
}

被折叠的 条评论
为什么被折叠?



