Data Structure Course Design——Sports Games Score Statistics System (C++ Version)

1.1② Sports meeting score statistics
[Problem description]
  The n schools participating in the sports meeting are numbered 1~n. The competition is divided into m men's events and w women's events, and the event numbers are 1~m and m+1~m+w respectively. Due to the large difference in the number of participants in each project, some projects only take the top five, and the scoring order is 7, 5, 3, 2, 1; some projects only take the top three, and the scoring order is 5; 3, 2. Write a statistical program to generate various transcripts and score reports.
[Basic requirements]
  Generate transcripts for each school, including the project number, ranking (score), name and score of each achievement obtained by each school; generate a team total score report, including the school number, total score of the men's team, Women's team total score and overall team score.
[Test data]
  For n=4, m=3, w=2, the top five items with odd numbers are selected, and the top three items with even numbers are selected. Design a set of example data.
[Implementation Tips]
  You can assume that n≤20, m≤30, w≤20, and the name length does not exceed 20 characters. At the end of each event, enter its number and type (differentiate between the top five or the top three), and enter the athlete's name, school name (and results) in order of ranking.
[Selected Content]
Allows users to specify other ranking methods for a certain project.

Without further ado, let’s get straight to the code! (Private messages and comments welcome!)

#include<math.h>
#include<process.h>
#include <iostream>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;

#define N 20 //学校最大数目
#define M 30 //男子项目最大数
#define W 20 //女子项目最大数

//存放项目信息的结构体
typedef struct
{
    int inum;//项目编号
    int top;//取名次的数目
    int range[5];//名次
    int mark[5];//分数
}itemnode;

//存放学校信息的结构体
typedef struct
{
    int snum;//学校编号
    int score;//学校总分
    int mscore;//男子总分
    int wscore;//女子总分
    itemnode t[M + W];//项目数组
}snode;
snode a[N];//定义一个学校数组

//菜单函数
void menu(int n, int m, int w)
{//n代表学校数,m代表男子数,w代表女子数
    int c;
    void input(int n, int m, int w);//输入功能
    void output(int n, int m, int w);//输出功能
    void sortput(int n, int m, int w);//排序输出
    void search(int n, int m, int w);//查询功能
    cout<<"\t\t\t欢迎使用\t\t\t\t\t"<<endl;
    cout<<"运动会分数统计系统"<<endl;
    cout << endl;
    cout<<"1.信息输入"<<endl;
    cout<<"2.统计输出" << endl;
    cout <<"3.排序输出" << endl;
    cout<<"4.信息查询"<<endl;
    cout<<"5.退出系统"<<endl;
    cout << endl;
    cout<<"======================================================="<<endl;
    cout << endl;
    cout<<"请输入您想要实现的功能(0--4):"<<endl;
    cin >> c;
    switch (c) {
    case 1:
        input(n, m, w);
        break;
    case 2:
        output(n, m, w);
        break;
    case 3:
        sortput(n, m, w);
        break;
    case 4:
        search(n, m, w);
        break;
    case 5:
        cout<<"感谢使用,祝您天天开心!!"<<endl;
        exit(0);//正常退出
    default:
        cout<<"您输入有误,请重新输入!";
        menu(n, m, w);
    }
}

//将信息写入文件中
void savetofile()
{
    FILE* fp;//定义一个文件指针
    int i;
        if (NULL == (fp = fopen("file.txt", "w"))) {
        cout<<"打开文件失败!"<<endl;
        return;
    }
    for (i = 0; i < N; i++) {
        if ('\0' != a[i].snum)
            if (fwrite(&a[i], sizeof(snode), 1, fp) != 1) {
                cout << "存入信息失败!" << endl;
                return;
            }
    }
    fclose(fp);//关闭文件
}

//将信息从文件里取出
void readfromfile()
{
    int i;
    FILE* fp;
    if ((fp = fopen("file.txt", "rb")) == NULL) {
        cout<<"文件打开失败!"<<endl;
        return;
    }
    for (i = 0; i < N; i++) {
        fread(&a[i], sizeof(snode), 1, fp);
    }
    fclose(fp);
}

//信息输入功能
void input(int n, int m, int w)
{
    int i, j, s, k, q = 1;
    for (i = 0; i < n; i++) {
        cout<<"请输入学校的编号:"<<endl;
        cin >> a[i].snum;
        for (j = 0; j < m + w; j++) {//总的项目的输入
            cout << "请输入项目编号:";
            cin>> a[i].t[j].inum;
            /*
            cout << "请输入该项目取前3还是前5(输入3或5):";
            cin >> a[i].t[j].top;
            */
            if (a[i].t[j].inum % 2 == 0) {
            cout<<"编号为偶数取前三" << endl;
                    cout << "获得的名次的个数(1--5):";
                }
                else if (a[i].t[j].inum % 2 == 1) {
                cout <<"编号为奇数的项目取前五"<<endl;
                    cout << "获得的名次的个数(1--5):";
                }
                else {
                    cout << "输入有误!程序退出....";
                    return;
                }
            cin >> k;//输入获得名次的个数
            for (s = 0; s < k; s++) {
                //if (3 == a[i].t[j].top) {
                if(a[i].t[j].inum % 2 == 0){
                    cout<<"请输入获得的名次(1--3):";
                }
                else {
                    cout<<"请输入获得的名次(1--5):";
                }
                cin >> a[i].t[j].range[s];//输入所获得的名次的信息
            }
            cout << endl;
        }
    }
    for (i = 0; i < n; i++) {
        //初始化分数
        a[i].score = 0;//学校总分
        a[i].mscore = 0;//男子总分
        a[i].wscore = 0;//女子总分
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < m + w; j++) {
            cout << "项目" << j + 1 << "取得是前3还是前5(输入3或5) :";
            cin >> a[i].t[j].top;
            for (s = 0; s < 5; s++) {
                if (a[i].t[j].top == 3) {
                    switch (a[i].t[j].range[s]) {
                    case 0:
                        a[i].t[j].mark[s] = 0;
                        break;
                    case 1:
                        a[i].t[j].mark[s] = 5;
                        break;
                    case 2:
                        a[i].t[j].mark[s] = 3;
                        break;
                    case 3:
                        a[i].t[j].mark[s] = 2;
                        break;
                    }
                }
                else if (a[i].t[j].top == 5) {
                    switch (a[i].t[j].range[s]) {
                    case 0:
                        a[i].t[j].mark[s] = 0;
                        break;
                    case 1:
                        a[i].t[j].mark[s] = 7;
                        break;
                    case 2:
                        a[i].t[j].mark[s] = 5;
                        break;
                    case 3:
                        a[i].t[j].mark[s] = 3;
                        break;
                    case 4:
                        a[i].t[j].mark[s] = 2;
                        break;
                    case 5:
                        a[i].t[j].mark[s] = 1;
                        break;
                    }
                }
                /*else {
                    cout << "信息输入错误!程序退出" << endl;
                    cout << endl;
                    exit(0);
                }*/
                a[i].score = a[i].score + a[i].t[j].mark[s];//学校总分
                if (j < m) {
                    a[i].mscore = a[i].mscore + a[i].t[j].mark[s];
                }
                else {//女子总分
                    a[i].wscore = a[i].wscore + a[i].t[j].mark[s];
                }
            }
        }
    }
    cout<<"输入完毕!(返回菜单请输入1):";
    cin >> q;
    cout << endl;
    if (q != 1) {
        cout<<"不能再添加信息了!";
    }
    cout << endl;
    savetofile();//保存文件
    menu(n, m, w);
}

#if(1)
void output(int n, int m, int w)                                /*2.统计输出*/
{
    readfromfile();
    int i, j, s, q = 0;
    for (i = 0; i < n; i++)  /*显示结果*/
    {
        cout << "学校编号:" << a[i].snum << endl;
        cout << "学校总分:" << a[i].score << endl;
        cout << "男子总分" << a[i].mscore <<"  " << "女子总分" << a[i].wscore << endl;
        for (j = 0; j < m + w; j++)
        {
           // cout<<"项目编号:%d  所取名次数量:%d\n", a[i].t[j].inum, a[i].t[j].top;
            cout << "项目编号:" << a[i].t[j].inum <<"  " << "所取名次取前:" << a[i].t[j].top<<"名"<< endl;
            for (s = 0; s < 5; s++)
            {
                if (a[i].t[j].range[s] != 0)
                    cout<<"名次:"<< a[i].t[j].range[s] <<"  " << "分数:"<< a[i].t[j].mark[s]<<"  "<<endl;
            }
        }
        cout << endl;
    }
    cout<<"\n";
    cout<<"统计完毕!返回?  1是 2否";    /*返回菜单*/
    cin >> q;
    cout << endl;
    if (q != 1)
        cout<<"统计已经结束!";
    cout << endl;
    menu(n, m, w);
}
#endif

//排序输出
void sortput(int n, int m, int w)//n为学校数,m为男子数,w为女子数
{
    readfromfile();
    int c, i, j, k, q = 0;
    int temp[N]={};
    cout << "\t**************排序输出系统**************" << endl;
    cout << endl;
    cout << "\t\t****1.按学校编号输出****" << endl;
    cout<<"\t\t****2.按学校总分输出****"<<endl;
    cout<<"\t\t****3.按男子总分输出****"<<endl;
    cout<<"\t\t****4.按女子总分输出****"<<endl;
    cout<<"======================================================="<<endl;
    cout << endl;
    do {
        cout<<"请选择您想实现的功能的编号(1--4):";
        cin >> c;
        switch (c) {
        case 1:
            for (i = 0; i < n; i++) {
                temp[i] = i;
            }
            //用的是冒泡排序输出
            for (i = 0; i < n; i++) {
                for (j = i + 1; j < n; j++) {
                    if (a[temp[i]].snum > a[j].snum) {
                        k = temp[i];
                        temp[i] = temp[j];
                        temp[j] = k;
                    }
                }
            }
            for (i = 0; i < n; i++) {
                //cout<<"学校标号:%d  学校总分:%d  男子总分:%d   女子总分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore;
                cout << "学校标号:" << a[temp[i]].snum << "  " << "学校总分:" << a[temp[i]].score << endl;
                cout << "男子总分" << a[temp[i]].mscore << "  " << "女子总分:" << a[temp[i]].wscore << endl;
            }
            break;
        case 2:
            for (i = 0; i < n; i++) {
                temp[i] = i;
            }
            for (i = 0; i < n; i++) {
                for (j = i + 1; j < n; j++) {
                    if (a[temp[i]].score < a[j].score) {
                        k = temp[i];
                        temp[i] = temp[j];
                        temp[j] = k;
                    }
                }
            }
            for (i = 0; i < n; i++) {
               // cout<<"学校编号:%d  学校总分:%d   男子总分:%d   女子总分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore;
                cout << "学校标号:" << a[temp[i]].snum << "  " << "学校总分:" << a[temp[i]].score << endl;
                cout << "男子总分" << a[temp[i]].mscore << "  " << "女子总分:" << a[temp[i]].wscore << endl;
            }
            break;
        case 3:
            for (i = 0; i < n; i++) {
                temp[i] = i;
            }
            for (i = 0; i < n; i++) {
                for (j = i + 1; j < n; j++) {
                    if (a[temp[i]].mscore < a[j].mscore) {
                        k = temp[i];
                        temp[i] = temp[j];
                        temp[j] = k;
                    }
                }
            }
            for (i = 0; i < n; i++) {
                printf("学校编号:%d  学校总分:%d  男团总分:%d  女团总分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore);
            }
            break;
        case 4:
            for (i = 0; i < n; i++) {
                temp[i] = i;
            }
            for (i = 0; i < n; i++) {
                for (j = i + 1; j < n; j++) {
                    if (a[temp[i]].wscore < a[j].wscore) {
                        k = temp[i];
                        temp[i] = temp[j];
                        temp[j] = k;
                    }
                }
            }
            for (i = 0; i < n; i++) {
                printf("学校编号:%d  学校总分:%d  男团总分:%d  女团总分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore);
            }
            break;
        default:
            printf("您的输入有误!请从新输入...");
        }
        printf("请选择 1.返回主菜单  0.继续");
        scanf_s("%d", &q);
        printf("\n");
    }
    //=======================
    while (0 == q);
    printf("\n");
    //=======================
    if (q != 0) {
        menu(n, m, w);
    }
}

//查询功能
void search(int n, int m, int w)
{
    readfromfile();
    int c, i, j, k, d, l, q = 0;
    cout<<"\t****************查询系统****************"<<endl;
    cout << endl;
    cout << "\t\t****1.按学校编号查询****"<<endl;
    cout << "\t\t****2.按项目编号查询****" << endl;
    cout << "======================================================="<<endl;
    cout << endl;
    do
    {
        k = -1; d = -1; l = -1;
        cout<<"请选择要实现功能的编号(1--2):";
        //scanf_s("%d", &c);
        cin >> c;
        switch (c) {
        case 1:
            cout<<"要查询的学校编号:";       /*查找学校编号下标*/
           // scanf_s("%d", &c);
            cin >> c;
            for (i = 0; i < n; i++) {
                if (c == a[i].snum) {
                    k = i;
                }
            }
            if (-1 == k) {
                cout<<"错误:这个学校没有参加此次运动会!"<<endl;
            }
            else {
                cout<<"要查询的项目编号:";         /*查找项目编号下标*/
                //scanf_s("%d", &c);
                cin >> c;
                for (j = 0; j < m + w; j++) {
                    if (c == a[k].t[j].inum) {
                        d = j;
                    }
                }
                if (-1 == d) {
                  cout<<"此次运动会没有这个项目"<<endl;
                }
                else {
                    //cout<<"这个项目取前 %d名,该学校的成绩如下:\n", a[k].t[d].top);
                    cout << "这个项目取前" << a[k].t[d].top << "名,该学校的成绩如下:" << endl;
                    for (i = 0; i < 5; i++) {
                        if (a[k].t[d].range[i] != 0) {
                            //cout<<"名次:%d\n", a[k].t[d].range[i];
                            cout << "名次:" << a[k].t[d].range[i];
                        }
                    }
                }
            }
            break;
        case 2:
            cout<<"要查询的项目编号:";     /*查找项目编号下标*/
            //scanf_s("%d", &c);
            cin >> c;
            for (i = 0; i < n; i++) {
                for (j = 0; j < m + w; j++) {
                    if (c == a[i].t[j].inum) {
                        l = j;
                    }
                    if (-1 == l) {
                        cout<<"此次运动会没有该项目";
                    }
                    else {
                        //printf("该项目取前 %d名,取得名次的学校\n", a[0].t[l].top);
                        cout << "该项目取前a[0].t[l].top名,取得名次的学校" << endl;
                        for (i = 0; i < n; i++) {
                            for (j = 0; j < 5; j++) {
                                if (a[i].t[l].range[j] != 0) {
                                    //cout<<"学校编号:%d,名次:%d\n", a[i].snum, a[i].t[l].range[j]);
                                    cout << "学校编号:" << a[i].snum << "  " << ", 名次:" << a[i].t[l].range[j] << endl;
                                }
                            }
                        }
                    }
                }
            }
            break;
        default:
            cout << "输入错误,请重试!" << endl;
        }
        cout<<"请选择:1.返回主菜单 0.继续";
        //scanf_s("%d", &q);
        cin >> q;
        //printf("\n");
        cout << endl;
    } while (0 == q);
    //printf("\n");
    cout << endl;
    if (q != 0) {
        menu(n, m, w);
    }
}

//主函数
int main()
{
    int n, m, w;//n为学校个数,m为男子数,w为女子数
    cout<<"\t\t\t欢迎使用\t\t\t\t"<<endl;
    cout << endl;
    cout << "\t***********运动会分数统计系统***********" << endl;
    cout << endl;
    cout<<"请先输入运动会主要信息"<<endl;
   cout<<"输入学校个数:";
   cin >> n;
   cout<<"输入男子项目个数:";
   cin >> m;
   cout<<"输入女子项目个数:";
   cin >> w;
    menu(n, m, w);
 
}

Screenshot of results 

 

Guess you like

Origin blog.csdn.net/m0_74039530/article/details/131146944