运动会成绩管理系统c++
时间: 2023-11-30 08:42:32 浏览: 123
以下是一个简单的运动会成绩管理系统的C++代码,实现了基本要求中的功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学校结构体
struct School {
int id; // 学校编号
string name; // 学校名称
int score; // 学校总分
int male_score; // 男子团体总分
int female_score; // 女子团体总分
vector<int> male_scores; // 男子项目成绩
vector<int> female_scores; // 女子项目成绩
};
// 定义项目结构体
struct Project {
int id; // 项目编号
string name; // 项目名称
int type; // 项目类型,1为男子项目,2为女子项目
int score[5]; // 前五名积分
int score3[3]; // 前三名积分
vector<int> schools[5]; // 前五名学校编号
vector<int> schools3[3]; // 前三名学校编号
};
// 定义全局变量
vector<School> schools; // 学校列表
vector<Project> projects; // 项目列表
// 比较函数,用于排序
bool cmp_school_id(const School& a, const School& b) {
return a.id < b.id;
}
bool cmp_school_name(const School& a, const School& b) {
return a.name < b.name;
}
bool cmp_school_score(const School& a, const School& b) {
return a.score > b.score;
}
bool cmp_school_male_score(const School& a, const School& b) {
return a.male_score > b.male_score;
}
bool cmp_school_female_score(const School& a, const School& b) {
return a.female_score > b.female_score;
}
bool cmp_project_id(const Project& a, const Project& b) {
return a.id < b.id;
}
bool cmp_project_name(const Project& a, const Project& b) {
return a.name < b.name;
}
// 读取数据
void read_data() {
ifstream fin("data.txt");
int n, m, w;
fin >> n >> m >> w;
// 读取学校信息
for (int i = 0; i < n; i++) {
School school;
school.id = i + 1;
fin >> school.name;
schools.push_back(school);
}
// 读取男子项目信息
for (int i = 0; i < m; i++) {
Project project;
project.id = i + 1;
project.type = 1;
fin >> project.name;
for (int j = 0; j < 5; j++) {
fin >> project.score[j];
}
for (int j = 0; j < 3; j++) {
fin >> project.score3[j];
}
projects.push_back(project);
}
// 读取女子项目信息
for (int i = 0; i < w; i++) {
Project project;
project.id = i + m + 1;
project.type = 2;
fin >> project.name;
for (int j = 0; j < 5; j++) {
fin >> project.score[j];
}
for (int j = 0; j < 3; j++) {
fin >> project.score3[j];
}
projects.push_back(project);
}
fin.close();
}
// 保存数据
void save_data() {
ofstream fout("data.txt");
fout << schools.size() << " " << projects.size() / 2 << " " << projects.size() / 2 << endl;
// 保存学校信息
for (int i = 0; i < schools.size(); i++) {
fout << schools[i].name << endl;
}
// 保存男子项目信息
for (int i = 0; i < projects.size() / 2; i++) {
fout << projects[i].name << " ";
for (int j = 0; j < 5; j++) {
fout << projects[i].score[j] << " ";
}
for (int j = 0; j < 3; j++) {
fout << projects[i].score3[j] << " ";
}
fout << endl;
}
// 保存女子项目信息
for (int i = projects.size() / 2; i < projects.size(); i++) {
fout << projects[i].name << " ";
for (int j = 0; j < 5; j++) {
fout << projects[i].score[j] << " ";
}
for (int j = 0; j < 3; j++) {
fout << projects[i].score3[j] << " ";
}
fout << endl;
}
fout.close();
}
// 统计学校总分
void calc_score() {
for (int i = 0; i < schools.size(); i++) {
int score = 0;
int male_score = 0;
int female_score = 0;
for (int j = 0; j < projects.size(); j++) {
if (projects[j].type == 1) { // 男子项目
for (int k = 0; k < projects[j].schools[0].size(); k++) {
if (projects[j].schools[0][k] == schools[i].id) { // 学校获得第一名
score += projects[j].score[0];
male_score += projects[j].score[0];
}
}
for (int k = 0; k < projects[j].schools[1].size(); k++) {
if (projects[j].schools[1][k] == schools[i].id) { // 学校获得第二名
score += projects[j].score[1];
male_score += projects[j].score[1];
}
}
for (int k = 0; k < projects[j].schools[2].size(); k++) {
if (projects[j].schools[2][k] == schools[i].id) { // 学校获得第三名
score += projects[j].score[2];
male_score += projects[j].score[2];
}
}
for (int k = 3; k < 5; k++) {
for (int l = 0; l < projects[j].schools[k].size(); l++) {
if (projects[j].schools[k][l] == schools[i].id) { // 学校获得第四名或第五名
score += projects[j].score[k];
male_score += projects[j].score[k];
}
}
}
} else { // 女子项目
for (int k = 0; k < projects[j].schools[0].size(); k++) {
if (projects[j].schools[0][k] == schools[i].id) { // 学校获得第一名
score += projects[j].score[0];
female_score += projects[j].score[0];
}
}
for (int k = 0; k < projects[j].schools[1].size(); k++) {
if (projects[j].schools[1][k] == schools[i].id) { // 学校获得第二名
score += projects[j].score[1];
female_score += projects[j].score[1];
}
}
for (int k = 0; k < projects[j].schools[2].size(); k++) {
if (projects[j].schools[2][k] == schools[i].id) { // 学校获得第三名
score += projects[j].score[2];
female_score += projects[j].score[2];
}
}
for (int k = 3; k < 5; k++) {
for (int l = 0; l < projects[j].schools[k].size(); l++) {
if (projects[j].schools[k][l] == schools[i].id) { // 学校获得第四名或第五名
score += projects[j].score[k];
female_score += projects[j].score[k];
}
}
}
}
}
schools[i].score = score;
schools[i].male_score = male_score;
schools[i].female_score = female_score;
}
}
// 统计项目成绩
void calc_project_score() {
for (int i = 0; i < projects.size(); i++) {
for (int j = 0; j < schools.size(); j++) {
if (projects[i].type == 1) { // 男子项目
for (int k = 0; k < schools[j].male_scores.size(); k++) {
if (schools[j].male_scores[k] == i) { // 学校获得前五名
if (k == 0) {
projects[i].schools[0].push_back(schools[j].id);
} else if (k == 1) {
projects[i].schools[1].push_back(schools[j].id);
} else if (k == 2) {
projects[i].schools[2].push_back(schools[j].id);
} else if (k == 3) {
projects[i].schools[3].push_back(schools[j].id);
} else if (k == 4) {
projects[i].schools[4].push_back(schools[j].id);
}
}
}
} else { // 女子项目
for (int k = 0; k < schools[j].female_scores.size(); k++) {
if (schools[j].female_scores[k] == i) { // 学校获得前五名
if (k == 0) {
projects[i].schools[0].push_back(schools[j].id);
} else if (k == 1) {
projects[i].schools[1].push_back(schools[j].id);
} else if (k == 2) {
projects[i].schools[2].push_back(schools[j].id);
} else if (k == 3) {
projects[i].schools[3].push_back(schools[j].id);
} else if (k == 4) {
projects[i].schools[4].push_back(schools[j].id);
}
}
}
}
}
}
}
// 按学校编号查询学校某个项目的情况
void query_school_project() {
int school_id, project_id;
cout << "请输入学校编号和项目编号(以空格分隔):";
cin >> school_id >> project_id;
for (int i = 0; i < schools.size(); i++) {
if (schools[i].id == school_id) {
cout << "学校名称:" << schools[i].name << endl;
cout << "项目名称:" << projects[project_id - 1].name << endl;
if (projects[project_id - 1].type == 1) { // 男子项目
for (int j = 0; j < schools[i].male_scores.size(); j++) {
if (schools[i].male_scores[j] == project_id - 1) {
cout << "成绩:" << j + 1 << endl;
return;
}
}
} else { // 女子项目
for (int j = 0; j < schools[i].female_scores.size(); j++) {
if (schools[i].female_scores[j] == project_id - 1) {
cout << "成绩:" << j + 1 << endl;
return;
}
}
}
cout << "该学校未获得前五名" << endl;
return;
}
}
cout
阅读全文