fanesemyk的博客

当断不断,反受其乱!

Uva12412 师兄帮帮忙

实在不想做这道题了,先扔在这里吧
wa就wa吧,感觉自己是个废人了
感觉大概是格式的问题?反正我代码很烂不管了

#include<bits/stdc++.h>
using namespace std;
class Student
{
public:
    string sid;
    int cid,score1,score2,score3,score4,totalscore,rank;
    string name;
    Student(string sid,int cid,string name,int score1,int score2,int score3,int score4):sid(sid),cid(cid),score1(score1),score2(score2),score3(score3),score4(score4),name(name)
    {

    }
    Student()
    {

    }//无参构造函数
    friend istream& operator >>(istream& in,Student& x)//重定向输入流
    {
        in>>x.cid>>x.name>>x.score1>>x.score2>>x.score3>>x.score4;
        x.totalscore=x.score1+x.score2+x.score3+x.score4;
        x.rank=0;
        return in;
    }
    friend ostream& operator <<(ostream& out,Student& x)
    {
        out<<x.rank<<" "<<x.sid<<" "<<x.cid<<" "<<x.name<<" "<<x.score1<<" "<<x.score2<<" "<<x.score3<<" ";
        out<<x.score4<<" "<<x.totalscore<<" ";
        printf("%.2f\n",x.totalscore/4.0);
        return out;
    }
};
vector<Student> myvector;
void paixu(void)
{
    int rnk=1,cnt;
    for(int i=400;i>=0;i--)
    {
        cnt=0;//cnt represents the number of students whose totalscore equals to i
        for(auto &it:myvector)
        {
            if(it.totalscore==i)
            {
                it.rank=rnk;
                cnt++;
            }
        }
        rnk+=cnt;
    }
}
void query(void)
{
    paixu();//to calculate everyone's rank
    double averagescore;
    while(true)
    {
        printf("Please enter SID or name. Enter 0 to finish.\n");
        string input;
        cin>>input;
        if(input=="0")
            break;
        if(isdigit(input[0]))//the input is student id
        {
            for(auto &it:myvector)
                if(it.sid==input)
                {
                    //printf("The rank of this student is %d\n",it.rank);
                    cout<<it;
                }
        }
        else
        {
            for(auto &it:myvector)
                if(it.name==input)
                    cout<<it;
        }
    }
}
void add(void)
{
    while(true)
    {
        printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
        Student temp;
        string tempinput;
        cin>>tempinput;
        if(tempinput=="0")
            break;
        else
        {
            temp.sid=tempinput;
            cin>>temp;
        }
        bool flag=false;
        for(auto &it:myvector)
            if(it.sid==temp.sid)
                flag=true;
        if(flag)
            printf("Duplicated SID.\n");
        else
            myvector.push_back(temp);
    }
}
void remove(void)
{
    while(true)
    {
        int cnt=0;
        printf("Please enter SID or name. Enter 0 to finish.\n");
        vector<Student>::iterator it;
        string input;
        cin>>input;
        if(input=="0")
            break;
        if(isdigit(input[0]))//the input is student id
        {
            for(it=myvector.begin();it!=myvector.end();)
            {
                if((*it).sid==input)
                {
                    cnt++;
                    it=myvector.erase(it);
                }
                else
                    it++;
            }
        }
        else//the input is a name
        {
            for(it=myvector.begin();it!=myvector.end();)
            {
                if((*it).name==input)
                {
                    cnt++;
                    it=myvector.erase(it);
                }
                else
                    it++;
            }
        }
        printf("%d student(s) removed.\n",cnt);
    }
}

void ranking(void)
{
    printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
}
void statistics(void)
{
    double average;
    int num_passed,num_failed,id;
    printf("Please enter class ID, 0 for the whole statistics.\n");
    cin>>id;
    //创建一个新的vector用来进行数据统计
    vector<Student>stat;
    stat.clear();
    if(id==0)
    {
        for(auto &it:myvector)
            stat.push_back(it);
    }
    else
    {
        for(auto &it:myvector)
        {
            if(it.cid==id)
                stat.push_back(it);
        }
    }
    //对每一门学科进行统计
    {
        average=num_passed=num_failed=0;
        for(auto &it:stat)
        {
            average+=it.score1;
            if(it.score1>=60)
                num_passed++;
            else
                num_failed++;
        }
        printf("Chinese\n");
        if(stat.size()==0)
            printf("Average Score: 0.00\n");
        else
            printf("Average Score: %.2f\n",average/stat.size());
        printf("Number of passed students: %d\n",num_passed);
        printf("Number of failed students: %d\n\n",num_failed);
    }
    {
        average=num_passed=num_failed=0;
        for(auto &it:stat)
        {
            average+=it.score2;
            if(it.score2>=60)
                num_passed++;
            else
                num_failed++;
        }
        printf("Mathematics\n");
        if(stat.size()==0)
            printf("Average Score: 0.00\n");
        else
            printf("Average Score: %.2f\n",average/stat.size());
        printf("Number of passed students: %d\n",num_passed);
        printf("Number of failed students: %d\n\n",num_failed);
    }
    {
        average=num_passed=num_failed=0;
        for(auto &it:stat)
        {
            average+=it.score3;
            if(it.score3>=60)
                num_passed++;
            else
                num_failed++;
        }
        printf("English\n");
        if(stat.size()==0)
            printf("Average Score: 0.00\n");
        else
            printf("Average Score: %.2f\n",average/stat.size());
        printf("Number of passed students: %d\n",num_passed);
        printf("Number of failed students: %d\n\n",num_failed);
    }
    {
        average=num_passed=num_failed=0;
        for(auto &it:stat)
        {
            average+=it.score4;
            if(it.score4>=60)
                num_passed++;
            else
                num_failed++;
        }
        printf("Programming\n");
        if(stat.size()==0)
            printf("Average Score: 0.00\n");
        else
            printf("Average Score: %.2f\n",average/stat.size());
        printf("Number of passed students: %d\n",num_passed);
        printf("Number of failed students: %d\n\n",num_failed);
    }
    int num_pass[5]={0,0,0,0,0};//num_pass[i] represents the number of students who pass no less than i courses
    //exception:num_pass[0] represents the number of students who failed all the tests
    for(auto &it:stat)
    {
        int cnt=0;//统计每个人过了几门课
        if(it.score1>=60)
            cnt++;
        if(it.score2>=60)
            cnt++;
        if(it.score3>=60)
            cnt++;
        if(it.score4>=60)
            cnt++;
        for(int i=1;i<=4;i++)
        {
            if(cnt>=i)
                num_pass[i]++;
        }
        if(cnt==0)
            num_pass[0]++;
    }
    printf("Overall:\n");
    printf("Number of students who passed all subjects: %d\n",num_pass[4]);
    printf("Number of students who passed 3 or more subjects: %d\n",num_pass[3]);
    printf("Number of students who passed 2 or more subjects: %d\n",num_pass[2]);
    printf("Number of students who passed 1 or more subjects: %d\n",num_pass[1]);
    printf("Number of students who failed all subjects: %d\n\n",num_pass[0]);



}
void menu(void)//主菜单
{
    int x;
    bool flag=true;//控制何时退出
    while(flag)
    {
        printf("Welcome to Student Performance Management System (SPMS).\n\n");
        printf("1 - Add\n");
        printf("2 - Remove\n");
        printf("3 - Query\n");
        printf("4 - Show ranking\n");
        printf("5 - Show Statistics\n");
        printf("0 - Exit\n\n");//watch out for this \n!!!
        cin>>x;
        switch(x)
        {
            case 1:add();
                   break;
            case 2:remove();
                   break;
            case 3:query();
                   break;
            case 4:ranking();
                   break;
            case 5:statistics();
                   break;
            case 0:flag=false;
        }
    }

}

int main(void)
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    myvector.clear();
    menu();
    return 0;
}

阅读更多
版权声明:转载请注明原博客链接 https://blog.csdn.net/fanesemyk/article/details/55271126
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭