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

题目链接如下:

Online Judge

这道题折磨了我很久。最后是用了如何比较两个txt文件内容的细微差别-百度经验

这里介绍的fc 1.txt 2.txt 来比较两个答案的差别,最后查出了bug。用的测试样例是UDebug上的一组大数据:12412 | uDebug

为了比较答案,另外还学到了下面这个很有用的用法。

#define debug

#ifdef debug
    freopen("0.txt","r",stdin);
	freopen("1.txt","w",stdout);
#endif

#ifdef debug
    fclose(stdin);
	fclose(stdout);
#endif

我起先的代码问题出在:一个student被删除后,可以重新加回;但我是用mp[sid](map)来判断这个student还存不存在,被加回后,起先被删除但仍留在vector中的也变成存在了,造成了vector中有了两个重复的student。所以只能在student的结构体中加入了existed这个判断是否存在的变量。(确切来说,mp[sid]可以判断学生是否存在,若存在,则在vec[mp[sid] - 1]这个位置上;existed判断vector上这个student是否存在。)

我的代码如下:

#include 
#include 
#include 
#include 
#include 
// #define debug

struct stu{
    std::string sid;
    int cid;
    std::string name;
    std::vector score = std::vector (4);
    int rank;
    int totalScore = 0;
    int existed = 1;
};

struct course{
    int tot = 0;
    int passed = 0;
    int failed = 0;
};

int cnt, task;
std::vector vec, vec1;
std::map mp;
std::string fourCourse[] = {"Chinese", "Mathematics", "English", "Programming"};

void mainmenu(){
    printf("Welcome to Student Performance Management System (SPMS).\n\n");
    printf("1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n");
}

void addStudent(){
    stu temp;
    while(1){
        printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
        std::cin >> temp.sid;
        if(temp.sid == "0"){
            break;
        }
        scanf("%d", &temp.cid);
        std::cin >> temp.name;
        temp.totalScore = 0;
        for(int i = 0; i < 4; ++i){
            scanf("%d", &temp.score[i]);
            temp.totalScore += temp.score[i];
        }
        if(mp[temp.sid]){
            printf("Duplicated SID.\n");
        } else{
            mp[temp.sid] = ++cnt;
            vec.push_back(temp);
        }
    }
}

void removeStudent(){
    std::string str;
    while(1){
        printf("Please enter SID or name. Enter 0 to finish.\n");
        std::cin >> str;
        if(str == "0"){
            break;
        }
        int xx = 0;
        if(mp[str]){
            vec[mp[str] - 1].existed = 0;
            mp[str] = 0;
            xx = 1;
        } else{
            for(int i = 0; i < vec.size(); ++i){
                if(vec[i].existed && vec[i].name == str){
                    ++xx;
                    vec[i].existed = 0;
                    mp[vec[i].sid] = 0;
                }
            }
        }
        printf("%d student(s) removed.\n", xx);
    }
}

bool cmp1(const stu &a, const stu &b){
    return a.totalScore > b.totalScore;
}

bool cmp2(const stu &a, const stu &b){
    return mp[a.sid] < mp[b.sid];
}

void queryStudent(){
    vec1.clear();
    for(int i = 0; i < vec.size(); ++i){
        if(vec[i].existed){
            vec1.push_back(vec[i]);
        }
    }
    sort(vec1.begin(), vec1.end(), cmp1);
    vec1[0].rank = 1;
    for(int i = 1; i < vec1.size(); ++i){
        if(vec1[i].totalScore == vec1[i - 1].totalScore){
            vec1[i].rank = vec1[i - 1].rank;
        } else{
            vec1[i].rank = i + 1;
        }
    }
    sort(vec1.begin(), vec1.end(), cmp2);
    std::string str;
    while(1){
        printf("Please enter SID or name. Enter 0 to finish.\n");
        std::cin >> str;
        if(str == "0"){
            break;
        }
        for(int i = 0; i < vec1.size(); ++i){
            if(vec1[i].sid == str || vec1[i].name == str){
                printf("%d %s %d %s %d %d %d %d %d %.2f\n", vec1[i].rank, vec1[i].sid.c_str(), vec1[i].cid,
                    vec1[i].name.c_str(), vec1[i].score[0], vec1[i].score[1], vec1[i].score[2], vec1[i].score[3],
                    vec1[i].totalScore, vec1[i].totalScore / 4.0 + 0.00001);
            }
        }
    }
}

void showStatistics(){
    printf("Please enter class ID, 0 for the whole statistics.\n");
    int cid;
    scanf("%d", &cid);
    course a[4];
    int tot[5] = {0, 0, 0, 0, 0};
    int stuNbr = 0;
    int p = 0;
    for(int i = 0; i < vec.size(); ++i){
        if(vec[i].existed && (vec[i].cid == cid || !cid)){
            stuNbr++;
            int failNum = 0;
            for(int j = 0; j < 4; ++j){
                a[j].tot += vec[i].score[j];
                if(vec[i].score[j] >= 60){
                    a[j].passed++;
                } else{
                    a[j].failed++;
                    failNum++;
                }
            }
            tot[failNum]++;
        }
    }
    for(int i = 0; i < 4; ++i){
        printf("%s\n", fourCourse[i].c_str());
        printf("Average Score: %.2f\n", stuNbr == 0 ? 0 : a[i].tot * 1.0 / stuNbr + 0.00001);
        printf("Number of passed students: %d\n", a[i].passed);
        printf("Number of failed students: %d\n\n", a[i].failed);
    }
    p += tot[0];
    printf("Overall:\nNumber of students who passed all subjects: %d\n", p);
    for(int i = 1; i <= 3; ++i){
        p += tot[i];
        printf("Number of students who passed %d or more subjects: %d\n", 4 - i, p);
    }
    printf("Number of students who failed all subjects: %d\n\n", tot[4]);
}

int main(){
    #ifdef debug
    freopen("0.txt","r",stdin);
	freopen("1.txt","w",stdout);
    #endif
    cnt = 0;
    while(1){
        mainmenu();
        scanf("%d", &task);
        if(!task){
            break;
        }
        if(task == 1){
            addStudent();
        } else if(task == 2){
            removeStudent();
        } else if(task == 3){
            queryStudent();
        } else if(task == 4){
            printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
        } else{
            showStatistics();
        }
    }
    #ifdef debug
    fclose(stdin);
	fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,c++)