实验目标:
(1)能够管理各参赛队的基本信息(包含参赛队编号,参赛作品名称,参赛学校,赛事类别,参赛者,指导老师),赛事类别共11项(参见大赛官网jsjds.blcu.edu.cn);包括增加、删除、修改参赛队伍的信息。
提醒:从team.txt中读取。
文件内容形式:
其中主要函数:
// 参赛队伍结构体
struct Team {
string number;
string name;
string school;
string event;
string participant;
string advisor;
};
struct TreeNode {
string number;
Team data;
int depth; // 新增属性:节点深度
TreeNode* left;
TreeNode* right;
};
// 从文件中读取参赛队伍信息
vector<Team> read_teams(string filename) {
vector<Team> teams;
ifstream infile(filename);
if (!infile.is_open()) {
cout << "Failed to open file: " << filename << endl;
return teams;
}
string line;
while (getline(infile, line)) {
Team team;
size_t pos;
// 解析一行
pos = line.find("\t#");
if (pos != string::npos) {
team.number = line.substr(0, pos);
line.erase(0, pos + 1);
}
pos = line.find("\t#");
if (pos != string::npos) {
team.name = line.substr(0, pos);
line.erase(0, pos + 1);
}
pos = line.find("\t#");
if (pos != string::npos) {
team.school = line.substr(0, pos);
line.erase(0, pos + 1);
}
pos = line.find("\t#");
if (pos != string::npos) {
team.event = line.substr(0, pos);
line.erase(0, pos + 1);
}
pos = line.find("\t#");
if (pos != string::npos) {
team.participant = line.substr(0, pos);
line.erase(0, pos + 1);
}
pos = line.find("\t#");
if (pos != string::npos) {
team.advisor = line.substr(0, pos);
line.erase(0, pos + 1);
}
teams.push_back(team);
}
infile.close();
return teams;
}
// 将参赛队伍信息写入文件
void write_teams(string filename, vector<Team> teams) {
ofstream outfile(filename);
if (!outfile.is_open()) {
cout << "Failed to open file: " << filename << endl;
return;
}
for (Team team : teams) {
outfile << team.number << "\t"
<< team.name << "\t"
<< team.school << "\t"
<< team.event << "\t"
<< team.participant << "\t"
<< team.advisor << endl;
}
outfile.close();
}
// 将参赛队伍信息写入文件
void write_teams(string filename, vector<Team> teams) {
ofstream outfile(filename);
if (!outfile.is_open()) {
cout << "Failed to open file: " << filename << endl;
return;
}
for (Team team : teams) {
outfile << team.number << "\t"
<< team.name << "\t"
<< team.school << "\t"
<< team.event << "\t"
<< team.participant << "\t"
<< team.advisor << endl;
}
outfile.close();
}
// 根据参赛队编号查找参赛队伍
int find_team_by_number(vector<Team>& teams, string number) {
for (int i = 0; i < teams.size(); i++) {
if (teams[i].number == number) {
return i;
}
}
return -1;
}
主函数:
cout << "========= 参赛队伍管理系统 =========" << endl;
cout << "1. 显示所有参赛队伍" << endl;
cout << "2. 增加参赛队伍" << endl;
cout << "3. 删除参赛队伍" << endl;
cout << "4. 修改参赛队伍" << endl;
cout << "0. 退出系统" << endl;
cout << "请输入操作编号:";
cin >> choice;
switch (choice) {
case 1: // 显示所有参赛队伍
for (Team team : teams) {
cout << left << setw(15) << team.number
<< left << setw(60) << team.name
<< left << setw(23) << team.school
<< left << setw(18) << team.event
<< left << setw(10) << team.participant
<< left << setw(10) << team.advisor;
cout << endl;
}
break;
case 2: { // 增加参赛队伍
Team team;
cout << "请输入参赛队编号:";
cin >> team.number;
cout << "请输入参赛作品名称:";
cin >> team.name;
cout << "请输入参赛学校:";
cin >> team.school;
cout << "请输入赛事类别:";
cin >> team.event;
cout << "请输入参赛者:";
cin >> team.participant;
cout << "请输入指导教师:";
cin >> team.advisor;
teams.push_back(team);
write_teams(filename, teams);
cout << "成功增加参赛队伍!" << endl;
break;
}
case 3: { // 删除参赛队伍
string number;
cout << "请输入要删除的参赛队编号:";
cin >> number;
int index = find_team_by_number(teams, number);
if (index == -1) {
cout << "未找到参赛队伍!" << endl;
}
else {
teams.erase(teams.begin() + index);
write_teams(filename, teams);
cout << "成功删除参赛队伍!" << endl;
}
break;
}
case 4: { // 修改参赛队伍
string number;
cout << "请输入要修改的参赛队编号:";
cin >> number;
int index = find_team_by_number(teams, number);
if (index == -1) {
cout << "未找到参赛队伍!" << endl;
}
else {
Team& team = teams[index];
cout << "请输入新的参赛作品名称(原名称为" << team.name << "):";
cin >> team.name;
cout << "请输入新的参赛学校(原学校为" << team.school << "):";
cin >> team.school;
cout << "请输入新的赛事类别(原类别为" << team.event << "):";
cin >> team.event;
cout << "请输入新的参赛者(原参赛者为" << team.participant << "):";
cin >> team.participant;
cout << "请输入新的指导教师(原教师为" << team.advisor << "):";
cin >> team.advisor;
write_teams(filename, teams);
cout << "成功修改参赛队伍!" << endl;
}
break;
} case 0: // 退出系统
break;
default:
cout << "无效操作编号,请重新输入!" << endl;
}
cout << endl;
} while (choice != 0);
return 0;
}