数据结构课程设计预习报告
任务一:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Team {
int teamNumber;
string teamName;
string school;
string category;
string participants;
int score;
};
struct TreeNode {
Team team;
TreeNode* left;
TreeNode* right;
};
void insertTeam(TreeNode*& root, Team team) {
if (root == nullptr) {
root = new TreeNode;
root->team = team;
root->left = nullptr;
root->right = nullptr;
} else if (team.teamNumber < root->team.teamNumber) {
insertTeam(root->left, team);
} else {
insertTeam(root->right, team);
}
}
TreeNode* searchTeam(TreeNode* root, int teamNumber) {
if (root == nullptr || root->team.teamNumber == teamNumber) {
return root;
}
if (teamNumber < root->team.teamNumber) {
return searchTeam(root->left, teamNumber);
} else {
return searchTeam(root->right, teamNumber);
}
}
void displayTeamInfo(Team team) {
cout << "参赛队编号: " << team.teamNumber << endl;
cout << "参赛作品名称: " << team.teamName << endl;
cout << "参赛学校: " << team.school << endl;
cout << "赛事类别: " << team.category << endl;
cout << "参赛者: " << team.participants << endl;
}
void calculateASL(TreeNode* root, int level, int& sum, int& count) {
if (root != nullptr) {
sum += level;
count++;
calculateASL(root->left, level+1, sum, count);
calculateASL(root->right, level+1, sum, count);
}
}
int main() {
TreeNode* root = nullptr;
ifstream inFile("team.txt");
if (!inFile) {
cerr << "无法打开文件" << endl;
return 1;
}
while (!inFile.eof()) {
Team team;
inFile >> team.teamNumber >> team.teamName >> team.school >> team.category >> team.participants;
team.score = rand() % 41 + 60; // 随机生成60~100之间的初赛成绩
insertTeam(root, team);
}
inFile.close();
int teamNumber;
cout << "请输入参赛队编号: ";
cin >> teamNumber;
TreeNode* result = searchTeam(root, teamNumber);
if (result != nullptr) {
displayTeamInfo(result->team);
} else {
cout << "未找到该参赛队" << endl;
}
int sum = 0;
int count = 0;
calculateASL(root, 1, sum, count);
double asl = static_cast<double>(sum) / count;
cout << "全部参赛队的平均查找长度ASL: " << asl << endl;
return 0;
}
任务二:
采用归并排序,因为是最近学到的,所以想实践以下
头文件:
#include <iostream>
#include <vector>
#include <algorithm>
struct Team {
std::string name;
int score;
};
void merge(std::vector<Team>& teams, int left, int mid, int right) {
int i = left;
int j = mid + 1;
std::vector<Team> temp;
while (i <= mid && j <= right) {
if (teams[i].score >= teams[j].score) {
temp.push_back(teams[i]);
i++;
} else {
temp.push_back(teams[j]);
j++;
}
}
while (i <= mid) {
temp.push_back(teams[i]);
i++;
}
while (j <= right) {
temp.push_back(teams[j]);
j++;
}
for (int k = left; k <= right; k++) {
teams[k] = temp[k - left];
}
}
void mergeSort(std::vector<Team>& teams, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(teams, left, mid);
mergeSort(teams, mid + 1, right);
merge(teams, left, mid, right);
}
}
int main() {
std::vector<Team> teams;
int numOfTeams;
std::cout << "请输入参赛队伍数量:";
std::cin >> numOfTeams;
for (int i = 0; i < numOfTeams; i++) {
Team team;
std::cout << "请输入第" << i+1 << "个队伍的名称:";
std::cin >> team.name;
std::cout << "请输入第" << i+1 << "个队伍的初赛成绩:";
std::cin >> team.score;
teams.push_back(team);
}
mergeSort(teams, 0, numOfTeams - 1);
std::cout << "\n决赛秩序册如下:" << std::endl;
for (int i = 0; i < numOfTeams; i++) {
std::cout << "决赛室" << i % 17 + 1 << ":" << teams[i].name << std::endl;
}
return 0;
}
任务三:
#include <iostream>
#include <vector>
#include <map>
// 定义地点结构体
struct Location {
std::string name;
std::vector<std::string> nearbyLocations;
};
// 定义地图类
class Map {
private:
std::map<char, Location> locations; // 地点映射表
public:
// 添加地点
void addLocation(char code, std::string name, std::vector<std::string> nearbyLocations) {
Location location;
location.name = name;
location.nearbyLocations = nearbyLocations;
locations[code] = location;
}
// 根据地点编码获取地点名称
std::string getLocationName(char code) {
if (locations.count(code) > 0) {
return locations[code].name;
}
return "";
}
// 获取地点的附近地点列表
std::vector<std::string> getNearbyLocations(char code) {
if (locations.count(code) > 0) {
return locations[code].nearbyLocations;
}
return {};
}
// 导航查询
std::vector<char> navigate(char start, char end) {
std::vector<char> path;
std::map<char, bool> visited;
std::map<char, char> previous;
std::vector<char> queue;
// 初始化
visited[start] = true;
previous[start] = '\0';
queue.push_back(start);
// 广度优先搜索
while (!queue.empty()) {
char current = queue.front();
queue.erase(queue.begin());
if (current == end) {
// 找到目标地点,构建路径
char node = current;
while (node != '\0') {
path.insert(path.begin(), node);
node = previous[node];
}
break;
}
// 遍历当前地点的邻近地点
std::vector<std::string> nearbyLocations = getNearbyLocations(current);
for (const std::string& nearbyLocation : nearbyLocations) {
char nearbyCode = nearbyLocation[0];
if (!visited[nearbyCode]) {
visited[nearbyCode] = true;
previous[nearbyCode] = current;
queue.push_back(nearbyCode);
}
}
}
return path;
}
};
int main() {
Map map;
// 添加地点和邻近地点信息
map.addLocation('A', "A", {"B", "C"});
map.addLocation('B', "B", {"A", "D"});
map.addLocation('C', "C", {"A", "E"});
map.addLocation('D', "D", {"B", "F"});
map.addLocation('E', "E", {"C", "G"});
map.addLocation('F', "F", {"D", "H"});
map.addLocation('G', "G", {"E", "I"});
map.addLocation('H', "H", {"F", "J"});
map.addLocation('I', "I", {"G", "K"});
map.addLocation('J', "J", {"H", "L"});
map.addLocation('K', "K", {"I", "M"});
map.addLocation('L', "L", {"J", "N"});
map.addLocation('M', "M", {"K"});
map.addLocation('N', "N", {"L"});
// 查询地点信息
std::cout << "地点信息查询:" << std::endl;
char code;
std::cout << "请输入地点编码:";
std::cin >> code;
std::string locationName = map.getLocationName(code);
if (!locationName.empty()) {
std::cout << "地点名称:" << locationName << std::endl;
std::vector<std::string> nearbyLocations = map.getNearbyLocations(code);
std::cout << "附近地点:";
for (const std::string& nearbyLocation : nearbyLocations) {
std::cout << nearbyLocation << " ";
}
std::cout << std::endl;
} else {
std::cout << "无效的地点编码!" << std::endl;
}
// 导航查询
std::cout << "\n导航查询:" << std::endl;
char start, end;
std::cout << "请输入起点编码:";
std::cin >> start;
std::cout << "请输入终点编码:";
std::cin >> end;
std::vector<char> path = map.navigate(start, end);
if (!path.empty()) {
std::cout << "导航路径:";
for (char location : path) {
std::cout << location << " ";;
}
std::cout << std::endl;
} else {
std::cout << "无法找到合适的导航路径!" << std::endl;
}
return 0;
}
决赛地图
#include <queue>
// 决赛地图
std::map<char, std::vector<char>> graph = {
{'A', {'B', 'C'}},
{'B', {'A', 'D'}},
{'C', {'A', 'D', 'E'}},
{'D', {'B', 'C', 'F'}},
{'E', {'C', 'G'}},
{'F', {'D', 'H', 'I', 'J'}},
{'G', {'E', 'K'}},
{'H', {'F', 'I'}},
{'I', {'F', 'H', 'J', 'L'}},
{'J', {'F', 'I', 'M'}},
{'K', {'G', 'N'}},
{'L', {'I', 'M'}},
{'M', {'J', 'L'}},
{'N', {'K'}}
};
// 寻找路径
std::vector<char> findPath(char start, char end) {
std::queue<std::vector<char>> queue;
std::vector<char> visited;
queue.push({start});
visited.push_back(start);
while (!queue.empty()) {
std::vector<char> path = queue.front();
queue.pop();
char current = path.back();
if (current == end) {
return path;
}
std::vector<char> neighbors = graph[current];
for (char neighbor : neighbors) {
if (std::find(visited.begin(), visited.end(), neighbor) == visited.end()) {
std::vector<char> newPath = path;
newPath.push_back(neighbor);
queue.push(newPath);
visited.push_back(neighbor);
}
}
}
return {};
}
int main() {
char start, end;
std::cout << "请输入起始地点:";
std::cin >> start;
std::cout << "请输入目标地点:";
std::cin >> end;
std::vector<char> path = findPath(start, end);
if (path.empty()) {
std::cout << "无法找到路径。" << std::endl;
} else {
std::cout << "导航路径为:";
for (char location : path) {
std::cout << location << " -> ";
}
std::cout << "目标地点" << std::endl;
}
return 0;
}