1)能够管理各参赛队的基本信息(包含参赛队编号,参赛作品名称,参赛学校,赛事类别,参赛者,指导老师),赛事类别共11项(参见大赛官网jsjds.blcu.edu.cn);包括增加、删除、修改参赛队伍的信息。
#include <iostream>
#include <string>
#include<fstream>
#include <vector>
#include<algorithm>
#include <queue>
#include <chrono>
#include <unordered_map>
#include<cmath>
using namespace std;
struct Information {
string Works;
string School;
string Sort;
string Teacher;
string Participant;
int Number;
};
class Team_Manager {
public:
vector<Information> teams;
void add(const Information& team) {
teams.push_back(team);
}
// 添加参赛队伍信息
void AddTeam(int Number, const string& Works, const string& School, const string& Sort, const string& Participant, const string& Teacher) {
Information team;
team.Number = Number;
team.School = School;
team.Works = Works;
team.Sort = Sort;
team.Participant = Participant;
team.Teacher = Teacher;
teams.push_back(team);
cout << "添加成功!" << endl;
}
// 删除参赛队伍信息
void DeleteTeam(int Number) {
for (auto it = teams.begin(); it != teams.end(); ++it) {
if ((*it).Number == Number) {
teams.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该参赛队伍信息!" << endl;
}
// 修改参赛队伍信息
void AmendTeam(int Number, const string& Works, const string& School, const string& Sort, const string& Participant, const string& Teacher) {
for (auto it = teams.begin(); it != teams.end(); ++it) {
if ((*it).Number == Number) {
(*it).Works = Works;
(*it).School = School;
(*it).Sort = Sort;
(*it).Participant = Participant;
(*it).Teacher = Teacher;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该参赛队伍信息!" << endl;
}
};
从team.txt中读取参赛队伍的基本信息,实现基于二叉排序树的查找。根据提示输入参赛队编号,若查找成功,输出该赛事类别对应的基本信息(参赛作品名称、参赛学校、赛事类别、参赛者和指导老师信息),同时,输出查找成功时的平均查找长度ASL;否则,输出“查找失败!”
struct TreeNode {
Information data;
TreeNode* Leftchild;
TreeNode* Rightchild;
};
// 插入结点到二叉排序树中
void Insert(TreeNode *&root,Information team) {
if (root == nullptr) { // 根结点为空,直接插入为根结点
root = new TreeNode{ team, nullptr, nullptr };
return;
}
if (team.Number < root->data.Number) { // 插入到左子树中
Insert(root->Leftchild, team);
}
else if (team.Number > root->data.Number) { // 插入到右子树中
Insert(root->Rightchild, team);
}
}
void SearchNumber(TreeNode *root, int Number) {
if (root == nullptr) { // 查找失败
cout << "为空";
return;
}
if (Number == root->data.Number) {
cout << "查找成功!" << endl;
cout << "参赛作品名称:" << root->data.Works << endl;
cout << "参赛学校:" << root->data.School << endl;
cout << "赛事类别:" << root->data.Sort << endl;
cout << "参赛者:" << root->data.Participant << endl;
cout << "指导老师:" << root->data.Teacher << endl;
return;
}
if (Number < root->data.Number)
SearchNumber(root->Leftchild,Number);
else
SearchNumber(root->Rightchild,Number);
}
3)能够提供按参赛学校查询参赛团队(或根据赛事类别查询参赛团队),即,根据提示输入参赛学校名称(赛事类别),若查找成功,输出该学校参赛的(该赛事类别的)所有团队的基本信息,输出的参赛团队按赛事类别有序输出。(排序算法可从选择排序、插入排序、希尔排序、归并排序、堆排序中任意选择,并为选择算法的原因做出说明。)
#void SearchSchool(TreeNode* root, string School) {
if (root == nullptr) {
return;
}
vector<Information> Data;
SearchSchool(root->Rightchild, School);
if (root->data.School == School)
{
Information Getteam;
Getteam.Number = root->data.Number;
Getteam.Participant = root->data.Participant;
Getteam.School = root->data.School;
Getteam.Sort = root->data.Sort;
Getteam.Teacher = root->data.Teacher;
Getteam.Works = root->data.Works;
Data.push_back(Getteam);
}
SearchSchool(root->Leftchild, School);
sort(Data.begin(),Data.end(), [](Information &a, Information &b) {
return a.Number < b.Number;
});
for (auto element : Data) {
cout << element.Number << "\t" << element.Participant << "\t" << element.School << "\t" << element.Sort << "\t" << element.Teacher << "\t" << element.Works << endl;
}
}
for (int i = 0; i < 398; i++) {
Insert(root, team[i]);
}
cout << "输入学校名字";
string SchoolName;
cin >> SchoolName;
SearchSchool(root, SchoolName);
}
(4)为省赛现场设计一个决赛叫号系统。所有参赛队按赛事组织文件中的赛事类别分到9个决赛室,决赛室按顺序叫号,被叫号参赛队进场,比赛结束后,下一参赛队才能进赛场。请模拟决赛叫号系统,演示省赛现场各决赛室的参赛队进场情况。(模拟时,要能直观展示叫号顺序与进场秩序一致)
class FinalsSystem {
public:
FinalsSystem() : currentCategory("") {}
void addTeam(const Information& team) {
teams[team.Sort].push(team);
}
void start() {
for (auto& p : teams) {
currentCategory = p.first;
while (!p.second.empty()) {
auto team = p.second.front();
cout << "赛事类别 " << currentCategory << " Team: " << team.Number << " 进入决赛室." << endl;
p.second.pop();
}
}
};
int main(){
for (int i = 0; i < 398; i++) {
system.addTeam(team[i]);
}
// 开始比赛
system.start();}
(5)赛事系统为参赛者提供赛地的校园导游程序。为参赛者提供各种路径导航的查询服务。以我校长山校区提供比赛场地为例,(请为参赛者提供不少于10个目标地的导航。可为参赛者提供校园地图中任意目标地(建筑物)相关信息的查询;提供图中任意目标地(建筑物)的问路查询,即查询任意两个目的地(建筑物)之间的一条最短的简单路径。
// 定义图的节点结构体
struct Node {
string name; // 节点名称
int id; // 节点编号
vector<pair<int, int>> neighbors; // 相邻节点及其距离
};
// 定义比较函数,用于优先队列中的节点排序
struct CompareNode {
bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second;
}
};
// Dijkstra算法计算最短路径
vector<int> dijkstra(const vector<Node>& graph, int start, int end) {
vector<int> dist(graph.size(), INT_MAX); // 到各个节点的距离
vector<int> prev(graph.size(), -1); // 记录最短路径中每个节点的前驱节点
priority_queue<pair<int, int>, vector<pair<int, int>>, CompareNode> pq; // 优先队列,用于存储待处理的节点
dist[start] = 0;
pq.push(make_pair(start, 0));
while (!pq.empty()) {
int curr = pq.top().first;
pq.pop();
if (curr == end) {
break;
}
for (auto neighbor : graph[curr].neighbors) {
int next = neighbor.first;
int weight = neighbor.second;
if (dist[curr] + weight < dist[next]) {
dist[next] = dist[curr] + weight;
prev[next] = curr;
pq.push(make_pair(next, dist[next]));
}
}
}
// 构造最短路径
vector<int> path;
int curr = end;
while (curr != -1) {
path.push_back(curr);
curr = prev[curr];
}
reverse(path.begin(), path.end());
return path;
}