欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

uva 12412

程序员文章站 2024-03-20 09:49:28
...

无聊水题,代码没A,找不到BUG 不改了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

const double eps = 1e-5;

struct node
{
    string sid,name;
    int cid,score[6];
    int ra,sum;
}sp[10005];

struct node1
{
    int sum = 0;
    int p; //在sp中的位置
}l[10005];

int len = 0,re[10005]; //当前长度
char xk[5][20] = {"Chinese", "Mathematics", "English", "Programming"};

bool cmp(node1 a,node1 b)
{
    return a.sum > b.sum;
}

bool f(string s)
{
    for(int i = 0 ; i < len ;i++){
        if(s == sp[i].sid && !re[i]) return false;
    }
    return true;
}
void Add()
{
    string s;
    while(1){
        bool flag = true;
        printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
        cin >> s;
        if(s == "0") break;
        else if(f(s)){
            printf("Duplicated SID.\n");
            flag = false;
        }
        sp[len].sid = s;
        cin >> sp[len].cid >> sp[len].name;
        int sum = 0 ;
        for(int i = 0 ; i < 4 ; i ++) {
                scanf("%d",&sp[len].score[i]);
                sum += sp[len].score[i];
        }
        sp[len].sum = l[len].sum = sum;
        l[len].p = len;

        if(flag) len++;
    }
}

void Remove()
{
    string s;
    while(1){
        printf("Please enter SID or name. Enter 0 to finish.\n");
        cin >> s;
        int cnt = 0;
        if(s == "0") break;
        for(int i = 0 ; i < len ; i++){
            if(!re[i]){
                if(sp[i].name == s || sp[i].sid == s){
                    re[i] = 1;
                    cnt++;
                }
            }
        }
        printf("%d student(s) removed.\n",cnt);
    }
}

void Query()
{
    string s;
    sort(l,l+len,cmp);
    int p = 1,w = 0,sum = 1;
    for(int i = 0 ; i < len ; i++){
        if(!re[l[i].p]){
            if(l[i].sum == w) sp[l[i].p].ra = p;
            else{
                p = sum;
                w = l[i].sum;
                sp[l[i].p].ra = p;
            }
            sum++;
        }
    }

    while(1){
        printf("Please enter SID or name. Enter 0 to finish.\n");
        cin >> s;
        if(s == "0") break;
        for(int i = 0 ; i < len ; i++){
            if(!re[i]){
                if(sp[i].name == s || sp[i].sid == s){
                    cout << sp[i].ra << " " <<  sp[i].sid << " " << sp[i].cid << " " << sp[i].name << " " ;
                    for(int j = 0 ; j < 4;j++) printf("%d ",sp[i].score[j]);
                    printf("%d %.2lf\n",sp[i].sum,(sp[i].sum * 1.0 / 4 + eps));
                }
            }
        }
    }
}

void Showr()
{
    printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
}

void Shows()
{
        int a;
        int ch = 0,ma = 0,en = 0,pro = 0,sum = 0;
        int t[5],t1[5],t2[10],t3[10];
        memset(t,0,sizeof t);
        memset(t1,0,sizeof t1);
        memset(t2,0,sizeof t2);
        memset(t3,0,sizeof t3);
        printf("Please enter class ID, 0 for the whole statistics.\n");
        scanf("%d",&a);
        if(a == 0){
            for(int i = 0 ; i < len ; i++){
                if(!re[i]){
                    int cnt = 0;
                    sum++;
                    ch += sp[i].score[0];
                    ma += sp[i].score[1];
                    en += sp[i].score[2];
                    pro += sp[i].score[3];
                      for(int j = 0 ; j < 5 ; j++){
                        if(sp[i].score[j] >= 60) {
                                t[j]++;
                                cnt++;
                        }
                        else t1[j]++;
                    }
                    if(cnt == 4) t2[4]++;
                    if(cnt >= 3) t2[3]++;
                    if(cnt >= 2) t2[2]++;
                    if(cnt >= 1) t2[1]++;
                    if(cnt == 0) t2[0]++;
                }
            }
        }
        else{
            for(int i = 0 ; i < len ; i++){
                if(!re[i] && sp[i].cid == a){
                    sum++;
                    int cnt = 0;
                    ch += sp[i].score[0];
                    ma += sp[i].score[1];
                    en += sp[i].score[2];
                    pro += sp[i].score[3];
                    for(int j = 0 ; j < 5 ; j++){
                        if(sp[i].score[j] >= 60) {
                                t[j]++;
                                cnt++;
                        }
                        else t1[j]++;
                    }
                    if(cnt == 4) t2[4]++;
                    if(cnt >= 3) t2[3]++;
                    if(cnt >= 2) t2[2]++;
                    if(cnt >= 1) t2[1]++;
                    if(cnt == 0) t2[0]++;
                }
            }
        }
        for(int i = 0 ; i < 4 ;i++){
            printf("%s\n",xk[i]);
            printf("Average Score: %.2f\n",t3[i] * 1.0 / sum + eps);
            printf("Number of passed students: %d\n",t[i]);
            printf("Number of failed students: %d\n\n",t1[i]);
        }

        printf("Overall:\n");
        printf("Number of students who passed all subjects: %d\n",t2[4]);
        printf("Number of students who passed 3 or more subjects: %d\n",t2[3]);
        printf("Number of students who passed 2 or more subjects: %d\n",t2[2]);
        printf("Number of students who passed 1 or more subjects: %d\n",t2[1]);
        printf("Number of students who failed all subjects: %d\n\n",t2[0]);

}

int main()
{
    //freopen("E://text//text.txt","w",stdout);
    int a;
    memset(re,0,sizeof re);
    while(1){
        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");

        scanf("%d",&a);

        switch(a)
        {
            case 1:
                Add();
                break;
            case 2:
                Remove();
                break;
            case 3:
                Query();
                break;
            case 4:
                Showr();
                break;
            case 5:
                Shows();
                break;
            case 0:
                return 0;
        }
    }
}