数据结构课程设计完整代码

#include <iostream>
#include <string>
#include<fstream>
#include <vector>
#include<algorithm>
#include <queue>
#include <chrono>
#include <unordered_map>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

using namespace std;
const int NUM_ROOMS = 9; // 决赛室数量
const int INF = 1e9; // 无穷大


// 定义参赛队伍结构体
struct Team {
    int id; // 参赛队编号
    string name; // 参赛作品名称
    string school; // 参赛学校
    string category; // 赛事类别
    string participants; // 参赛者
    string teacher; // 指导老师
    int order; // 进场顺序
};

// 定义参赛队伍管理类
class TeamManager {
private:
    vector<Team> teams;


public:

    void addteam(const Team& team) {
        teams.push_back(team);
    }
    // 添加参赛队伍信息
    void addTeam(int id, const string& name, const string& school, const string& category, const string& participants, const string& teacher) {
        Team team;
        team.id = id;
        team.school = school;
        team.name = name;
        team.category = category;
        team.participants = participants;
        team.teacher = teacher;
        teams.push_back(team);
        cout << "添加成功!" << endl;
    }
    // 删除参赛队伍信息
    void deleteTeam(int id) {
        for (auto it = teams.begin(); it != teams.end(); ++it) {
            if (it->id == id) {
                teams.erase(it);
                cout << "删除成功!" << endl;
                return;
            }
        }
        cout << "未找到该参赛队伍信息!" << endl;
    }

    // 修改参赛队伍信息
    void modifyTeam(int id, const string& name, const string& school, const string& category, const string& participants, const string& teacher) {
        for (auto it = teams.begin(); it != teams.end(); ++it) {
            if (it->id == id) {
                it->name = name;
                it->school = school;
                it->category = category;
                it->participants = participants;
                it->teacher = teacher;
                cout << "修改成功!" << endl;
                return;
            }
        }
        cout << "未找到该参赛队伍信息!" << endl;
    }
};

// 二叉排序树结点结构体
struct BSTNode {
    Team data;
    BSTNode* lchild;
    BSTNode* rchild;
};

// 插入结点到二叉排序树中
void InsertBST(BSTNode*& root, Team team) {
    if (root == nullptr) { // 根结点为空,直接插入为根结点
        root = new BSTNode{ team, nullptr, nullptr };
        return;
    }
    if (team.id < root->data.id) { // 插入到左子树中
        InsertBST(root->lchild, team);
    }
    else if (team.id > root->data.id) { // 插入到右子树中
        InsertBST(root->rchild, team);
    }
}

// 在二叉排序树中查找指定参赛队编号的参赛队伍信息,并返回平查找长度ASL
int SearchBST(BSTNode* root, int id, int level) {
    if (root == nullptr) { // 查找失败
        return -1;
    }
    if (id == root->data.id) { // 查找成功
        cout << "查找成功!" << endl;
        cout << "参赛作品名称:" << root->data.name << endl;
        cout << "参赛学校:" << root->data.school << endl;
        cout << "赛事类别:" << root->data.category << endl;
        cout << "参赛者:" << root->data.participants << endl;
        cout << "指导老师:" << root->data.teacher << endl;
        return level; // 返回平均查找长度ASL
    }
    else if (id < root->data.id) { // 在左子树中继续查找
        return SearchBST(root->lchild, id, level + 1);
    }
    else { // 在右子树中继续查找
        return SearchBST(root->rchild, id, level + 1);
    }
}

void searchBySchool(BSTNode* root, string school) {
    if (root == nullptr) {
        return;
    }

    vector<Team> result; // 定义存储结果的容器

    searchBySchool(root->lchild, school);

    if (root->data.school == school) {
       Team node = { root->data.id, root->data.name, root->data.category, root->data.participants, root->data.teacher };
        result.push_back(node); // 将符合条件的节点数据存入容器
    }

    searchBySchool(root->rchild, school);

    sort(result.begin(), result.end(), [](const Team& a, const Team& b) {if (a.category == b.category) {
        return a.id < b.id; // 如果类别相同,则按照id升序排列
    }
    else {
        return a.category < b.category; // 否则按照类别升序排列
    } }); // 对容器进行排序

    for (auto& node : result) { // 遍历输出
        cout << node.id << " | " << node.name << " | " << node.category << " | " << node.participants << " | " << node.teacher << endl;
    
    }
}
class FinalsSystem {
public:
    FinalsSystem() : currentCategory("") {}

    void addTeam(const Team& team) {
        teams[team.category].push(team);
    }

    void start() {
        for (auto& p : teams) {
            currentCategory = p.first;
            while (!p.second.empty()) {
                auto team = p.second.front();
                cout << "Category " << currentCategory << " Team:  " << team.id << " 进入决赛室." << endl;
                p.second.pop();
            }
        }
    }

private:
    string currentCategory;
    unordered_map<string, queue<Team>> teams;
};


// 建筑物结构体
struct Building {
    string name; // 建筑物名称
    int bh; // 建筑物编号
};

// 边结构体
struct Edge {
    int to; // 目标建筑物编号
    int dist; // 边权重
};

// 邻接表存储图,使用vector实现
vector<Edge> adj[10];

// 记录每个建筑物到起点的最短距离和路径
int dist[10];
vector<int> path[10];

// Dijkstra算法求解最短路径
void dijkstra(int s) {
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push(make_pair(0, s));
    dist[s] = 0;
    while (!pq.empty()) {
        pair<int, int> p = pq.top();
        pq.pop();
        int v = p.second;
        if (dist[v] < p.first) continue;
        for (int i = 0; i < adj[v].size(); i++) {
            Edge e = adj[v][i];
            if (dist[e.to] > dist[v] + e.dist) {
                dist[e.to] = dist[v] + e.dist;
                path[e.to].clear();
                path[e.to].push_back(v);
                pq.push(make_pair(dist[e.to], e.to));
            }
            else if (dist[e.to] == dist[v] + e.dist) {
                path[e.to].push_back(v);
            }
        }
    }
}

// 输出路径
void printPath(int s, int v) {
    if (v == s) {
        cout << " " << s;
        return;
    }
    for (int i = 0; i < path[v].size(); i++) {
        printPath(s, path[v][i]);
        cout << " " << v;
    }
}


int main() {

    const int MAX_TEAMS = 1000;
    TeamManager C;
    FinalsSystem system;
    int i = 0, m = 398;
    int e = 0, a = 398;
    bool need_init = true;

    BSTNode* root = nullptr; // 根结点指针初始化为空指针
    Team* team = new Team[500];
    ifstream fin;
    fin.open("team.txt");
    string line;
    getline(fin, line);
    int count = 0;
    while (count < MAX_TEAMS && getline(fin, line)) {
        size_t pos1 = line.find("#");
        size_t pos2 = line.find("#", pos1 + 1);
        size_t pos3 = line.find("#", pos2 + 1);
        size_t pos4 = line.find("#", pos3 + 1);
        size_t pos5 = line.find("#", pos4 + 1);
        if (pos1 == string::npos || pos2 == string::npos) continue; // 非法行,跳过

        team[count].id = stoi(line.substr(0, pos1));
        team[count].name = line.substr(pos1 + 1, pos2 - pos1 - 1);
        team[count].school = line.substr(pos2 + 1, pos3 - pos2 - 1);
        team[count].category = line.substr(pos3 + 1, pos4 - pos3 - 1);
        team[count].participants = line.substr(pos4 + 1, pos5 - pos4 - 1);
        team[count].teacher = line.substr(pos5 + 1);


        team[count].name.erase(0, team[count].name.find_first_not_of("\n\r\t"));
        team[count].name.erase(team[count].name.find_last_not_of("\n\r\t") + 1);
        team[count].school.erase(0, team[count].school.find_first_not_of("\n\r\t"));
        team[count].school.erase(team[count].school.find_last_not_of("\n\r\t") + 1);
        team[count].category.erase(0, team[count].category.find_first_not_of("\n\r\t"));
        team[count].category.erase(team[count].category.find_last_not_of("\n\r\t") + 1);
        team[count].participants.erase(0, team[count].participants.find_first_not_of("\n\r\t"));
        team[count].participants.erase(team[count].participants.find_last_not_of("\n\r\t") + 1);
        team[count].teacher.erase(0, team[count].teacher.find_first_not_of("\n\r\t"));
        team[count].teacher.erase(team[count].teacher.find_last_not_of("\n\r\t") + 1);


        count++; // 处理完一行数据,计数器加1
    }

    if (!fin.is_open()) {
        cerr << "文件打开失败!" << endl;
        exit(1);
    }
    int x = 0;
    for (x; x < 398; x++) {
        C.addteam(team[x]);
    }
    int id; // 参赛队编号
    string name; // 参赛作品名称
    string school; // 参赛学校
    string category; // 赛事类别
    string participants; // 参赛者
    string teacher; // 指导老师
    int choice1, choice2;

    while (true) {
        // 显示主菜单
        cout << "请选择要进行的操作:" << endl;
        cout << "1. 功能一:增删改参赛队信息" << endl;
        cout << "2. 功能二:二叉树的查找和平均查找长度" << endl;
        cout << "3. 功能三:查询参赛学校的参赛团队:" << endl;
        cout << "4. 功能四:叫号比赛" << endl;
        cout << "5. 功能五:地图查询" << endl;
        cout << "0. 退出程序" << endl;

        // 输入选择
        cin >> choice1;

        switch (choice1) {
        case 1:
            // 显示功能一菜单
            cout << "请选择要进行的操作:" << endl;
            cout << "1. 增加参赛队信息" << endl;
            cout << "2. 删除参赛队信息" << endl;
            cout << "3. 修改参赛队信息" << endl;
            cin >> choice2;

            // 处理子功能选择
            switch (choice2) {
            case 1:

                cout << "请依次输入参赛队编号、参赛作品名称、参赛学校、赛事类别、参赛者、指导老师";
                cin >> id >> name >> school >> category >> participants >> teacher;
                cout << endl;
                C.addTeam(id, name, school, category, participants, teacher);
                // 执行子功能一代码

                break;
            case 2:
                cout << "请输入要删除的参赛队编号:";
                int s;
                cin >> s;
                C.deleteTeam(s);
                // 执行子功能二代码
                break;
            case 3:

                cout << "请依次输入参赛队编号,后面为修改的参赛作品名称、参赛学校、赛事类别、参赛者、指导老师";
                cin >> id >> name >> school >> category >> participants >> teacher;
                cout << endl;
                C.modifyTeam(id, name, school, category, participants, teacher);
            default:
                cout << "无效的选择,请重新输入!" << endl;
                break;
            }

            break;  // 结束case 1

        case 2:
            if (need_init) {
            // 显示功能二菜单
            cout << "二叉树的查找和平均查找长度" << endl;

           
            while (m--) {
                InsertBST(root, team[i]); // 插入结点到二叉排序树中
                i++;
            }

            int id; // 待查找的参赛队编号

            cout << "请输入待查找的参赛队编号:";
            cin >> id;
            //int asl = SearchBST(root, id, 1); // 在二叉排序树中查找指定参赛队编号的参赛队伍信息,并返回平均查找长度ASL
            if (SearchBST(root, id, 1) == -1) {
                cout << "查找失败!" << endl;
            }
            else {
                cout << "平均查找长度ASL:" << SearchBST(root, id, 1) << endl;
                need_init = false;
            }
                break;  // 结束case 2

        case 3:
            if (need_init) {
                // 需要执行的初始化操作
                // ...
               
            // 显示功能三菜单
            cout << "查询参赛学校的参赛团队:" << endl;
            
            while (a--) {
                InsertBST(root, team[e]); // 插入结点到二叉排序树中
                e++;
            }

            cout << "请输入待查找的学校:";
            string school;
            cin >> school;
            searchBySchool(root, school);
            need_init = false;
            }
            break;
        case 4:
            if (need_init) {
            // 显示功能四菜单
            cout << "叫号比赛" << endl;
            // 添加参赛队
            int j = 0;
            for (j; j < 398; j++) {
                system.addTeam(team[j]);
            }
            // 开始比赛
            system.start();
            need_init = false;
            }
            break;  // 结束case 4

        case 5:
            if (need_init) {
                // 需要执行的初始化操作
                // ...
               
            // 显示功能五菜单
            cout << "地图查询" << endl;
     
            cout << "0为西区宿舍  " << endl;
            cout << "1为文体中心   " << endl;
            cout << "2为东操场  " << endl;
            cout << "3为西苑食堂   " << endl;
            cout << "4为西操场  " << endl;
            cout << "5东苑食堂  " << endl;
            cout << "6为文理大楼   " << endl;
            cout << "7为图书馆  " << endl;
            cout << "8为计算机学院  " << endl;
            cout << "9为海韵湖  " << endl;

            Building b[10];
            b[0] = { "西区宿舍", 0 };
            b[1] = { "文体中心", 1 };
            b[2] = { "东操场", 2 };
            b[3] = { "西苑食堂", 3 };
            b[4] = { "西操场", 4 };
            b[5] = { "东苑食堂", 5 };
            b[6] = { "文理大楼", 6 };
            b[7] = { "图书馆", 7 };
            b[8] = { "计算机学院", 8 };
            b[9] = { "海韵湖", 9 };

            // 初始化边权重
            adj[0].push_back({ 1, 400 });
            adj[0].push_back({ 3, 150 });

            adj[1].push_back({ 0, 400 });
            adj[1].push_back({ 4, 100 });
            adj[1].push_back({ 2, 200 });

            adj[2].push_back({ 1, 200 });
            adj[2].push_back({ 5, 100 });

            adj[3].push_back({ 0, 150 });
            adj[3].push_back({ 4, 200 });
            adj[3].push_back({ 6, 400 });


            adj[4].push_back({ 3, 200 });
            adj[4].push_back({ 1, 100 });
            adj[4].push_back({ 5, 300 });
            adj[4].push_back({ 6, 200 });

            adj[5].push_back({ 2, 100 });
            adj[5].push_back({ 4, 300 });
            adj[5].push_back({ 7, 50 });

            adj[6].push_back({ 3, 400 });
            adj[6].push_back({ 4, 200 });
            adj[6].push_back({ 5, 300 });
            adj[6].push_back({ 8, 50 });

            adj[7].push_back({ 5, 50 });
            adj[7].push_back({ 6, 200 });
            adj[7].push_back({ 9, 300 });

            adj[8].push_back({ 6, 50 });
            adj[8].push_back({ 9, 100 });

            adj[9].push_back({ 7,300 });
            adj[9].push_back({ 8,200 });


            // 查询任意两个建筑物之间的最短路径
            int s, t;
            cout << "请输入起点(0~9):";
            cin >> s;
            cout << endl;
            cout << "请输入终点(0~9):";
            cin >> t;
            fill(dist, dist + 10, INF);
            dijkstra(s);
            cout << "从" << b[s].name << "到" << b[t].name << "的最短路径为:";
            printPath(s, t);
            need_init = false;
            }
            break;  // 结束case 5
            
        case 0:
            // 退出程序
            return 0;

        default:
            cout << "无效的选择,请重新输入!" << endl;
            }
        }
    }
    // ++++++++++++++++++++++++++++++++++++++

    fin.close();
    return 0;


    //+++++++++++++++++++++++++++++++++++
}


//+++++++++++++++++++++++++++++++++++++++


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值