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;
}

發佈了104 篇原創文章 · 獲贊 12 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章