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

来源:互联网 发布:淘宝拍摄衣服怎么布光 编辑:程序博客网 时间:2024/05/17 01:46

写一个简单的管理系统, 我WA在了要删除的有重名, 而且重名的挨着, 会删不掉- -


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 110#define EPS 1e-5using namespace std;struct Student {    char SID[MAXN], name[MAXN];    int CID;    int Chinese, Math, English, Program, Sum;} S[MAXN];int student_num;int Rank(int k) {    int n = 1;    for(int i = 0; i < student_num; i++)        if(S[i].Sum > S[k].Sum) n++;    return n;}void Add() {                                  //Adding a Student    getchar();    while(1) {        printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");        bool ok = true;        char str[MAXN];        gets(str);        if(!strcmp(str, "0")) break;        sscanf(str, "%s %d %s %d %d %d %d", S[student_num].SID, &S[student_num].CID,  //读入学生信息               S[student_num].name, &S[student_num].Chinese, &S[student_num].Math,               &S[student_num].English, &S[student_num].Program);        S[student_num].Sum = S[student_num].Chinese + S[student_num].Math                             + S[student_num].English + S[student_num].Program;        for(int i = 0; i < student_num; i++)                      //检查是否有重复            if(!strcmp(S[student_num].SID, S[i].SID)) {                ok = false;                break;            }        if(ok) student_num++;        else printf("Duplicated SID.\n");    }}void Remove() {                              //Removing a Student    getchar();    while(1) {        printf("Please enter SID or name. Enter 0 to finish.\n");        int n = 0;        char str[MAXN];        gets(str);        if(strlen(str) == 1&& str[0] == '0') break;        for(int i = 0; i < student_num; i++)            if(!strcmp(str, S[i].name)|| !strcmp(str, S[i].SID)) {                n++;                student_num--;                for(int j = i; j < student_num; j++)                    memcpy(S + j, S + (j + 1), sizeof(Student));                i--;            }        printf("%d student(s) removed.\n", n);    }}void Query() {                               //Querying Students    getchar();    while(1) {        printf("Please enter SID or name. Enter 0 to finish.\n");        char str[MAXN];        gets(str);        if(!strcmp(str, "0")) break;        for(int i = 0; i < student_num; i++)            if(!strcmp(str, S[i].name)|| !strcmp(str, S[i].SID)) {                printf("%d %s %d %s %d %d %d %d %d %.2lf\n", Rank(i), S[i].SID, S[i].CID,                       S[i].name, S[i].Chinese, S[i].Math,                       S[i].English, S[i].Program, S[i].Sum, S[i].Sum / 4.0 + EPS);;            }    }}void ShowRanking() {                         //Showing the Ranklist    printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");}void ShowStatistics() {                      //Showing Statistics    printf("Please enter class ID, 0 for the whole statistics.\n");    int id, n = 0, Ch = 0, Ma = 0, En = 0, Pr = 0;    int Ch_pas = 0, Ma_pas = 0, En_pas = 0, Pr_pas = 0;    int one = 0, two = 0, three = 0, all = 0;    double Ch_res = 0.0, Ma_res = 0.0, En_res = 0.0, Pr_res = 0.0;    scanf("%d", &id);    for(int i = 0; i < student_num; i++) {        if(id&& S[i].CID != id) continue;        n++;        int pas = 0;        if(S[i].Chinese >= 60) {            Ch_pas++;            pas++;        }        if(S[i].Math >= 60) {            Ma_pas++;            pas++;        }        if(S[i].English >= 60) {            En_pas++;            pas++;        }        if(S[i].Program >= 60) {            Pr_pas++;            pas++;        }        Ch += S[i].Chinese;        Ma += S[i].Math;        En += S[i].English;        Pr += S[i].Program;        if(pas == 1) one++;        else if(pas == 2) two++;        else if(pas == 3) three++;        else if(pas == 4) all++;    }    if(n) {        Ch_res = Ch * 1.0 / n + EPS;        Ma_res = Ma * 1.0 / n + EPS;        En_res = En * 1.0 / n + EPS;        Pr_res = Pr * 1.0 / n + EPS;    }    printf("Chinese\n");    printf("Average Score: %.2lf\n", Ch_res);    printf("Number of passed students: %d\n", Ch_pas);    printf("Number of failed students: %d\n\n", n - Ch_pas);    printf("Mathematics\n");    printf("Average Score: %.2lf\n", Ma_res);    printf("Number of passed students: %d\n", Ma_pas);    printf("Number of failed students: %d\n\n", n - Ma_pas);    printf("English\n");    printf("Average Score: %.2lf\n", En_res);    printf("Number of passed students: %d\n", En_pas);    printf("Number of failed students: %d\n\n", n - En_pas);    printf("Programming\n");    printf("Average Score: %.2lf\n", Pr_res);    printf("Number of passed students: %d\n", Pr_pas);    printf("Number of failed students: %d\n\n", n - Pr_pas);    printf("Overall:\n");    printf("Number of students who passed all subjects: %d\n", all);    printf("Number of students who passed 3 or more subjects: %d\n", all + three);    printf("Number of students who passed 2 or more subjects: %d\n", all + three + two);    printf("Number of students who passed 1 or more subjects: %d\n", all + three + two + one);    printf("Number of students who failed all subjects: %d\n\n", n - (all + three + two + one));}int main() {//#ifndef ONLINE_JUDGE//    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);//#endif // ONLINE_JUDGE    int cmd;    student_num = 0;    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");        cin >> cmd;        if(!cmd) break;        switch(cmd) {        case 1:            Add();            continue;        case 2:            Remove();            continue;        case 3:            Query();            continue;        case 4:            ShowRanking();            continue;        case 5:            ShowStatistics();            continue;        default:            break;        }    }    return 0;}


0 0
原创粉丝点击