12412 - A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

解题思路:我用了一个映射来进行学生学号是否存在的判断。。悲催的是一开始将 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值