数据结构代码

1-

#include <iostream>#include <fstream>#include <vector>#include <sstream>#include <string>#include <iomanip>using namespace std;struct Team { int number = 0; string work_name = ""; string school = ""; string category = ""; string participants = ""; string mentor = "";};vector<string> split_string(const string& s, char delimiter) { vector<string> elems; stringstream ss(s); string item; while (getline(ss, item, delimiter)) { elems.push_back(item); } return elems;}Team string_to_team(const string& line) { vector<string> team_data = split_string(line, '#'); Team t; try { t.number = stoi(team_data[0]); } catch (const invalid_argument& e) { t.number = -1; } t.work_name = team_data[1]; t.school = team_data[2]; t.category = team_data[3]; t.participants = team_data[4]; t.mentor = team_data[5]; return t;}string team_to_string(const Team& t) { return to_string(t.number) + "#" + t.work_name + "#" + t.school + "#" + t.category + "#" + t.participants + "#" + t.mentor;}vector<Team> read_teams_from_file(const string& file_path) { ifstream team_file(file_path); string line; vector<Team> teams; getline(team_file, line); // Ignore header line while (getline(team_file, line)) { teams.push_back(string_to_team(line)); } team_file.close(); return teams;}void write_teams_to_file(const vector<Team>& teams, const string& file_path) { ofstream team_file(file_path); team_file << "参赛队编号\t#\t参赛作品名称\t#\t参赛学校\t#\t赛事类别\t#\t参赛者\t#\t指导教师" << endl; for (const auto& team : teams) { team_file << team_to_string(team) << endl; } team_file.close();}void add_team(vector<Team>& teams, const Team& team) { teams.push_back(team);}void delete_team(vector<Team>& teams, int number) { teams.erase(remove_if(teams.begin(), teams.end(), [number](const Team& t) { return t.number == number; }), teams.end());}bool team_exists(vector<Team>& teams, int number) { auto it = find_if(teams.begin(), teams.end(), [number](const Team& t) { return t.number == number; }); return it != teams.end();}Team get_team(vector<Team>& teams, int number) { auto it = find_if(teams.begin(), teams.end(), [number](const Team& t) { return t.number == number; }); return it != teams.end() ? *it : Team();}void modify_team(vector<Team>& teams, const Team& team) { auto it = find_if(teams.begin(), teams.end(), [team](const Team& t) { return t.number == team.number; }); if (it != teams.end()) { *it = team; }}string input_with_default(const string& prompt, const string& default_value) { cout << prompt << "(默认值: " << default_value << "): "; string input; getline(cin, input); return input.empty() ? default_value : input;}int main() { string file_path = "C:/Users/yh/Desktop/team.txt"; vector<Team> teams = read_teams_from_file(file_path); int operation = -1; while (operation != 0) { cout << "请输入操作代码(0: 退出, 1: 添加, 2: 删除, 3: 修改):"; cin >> operation; if (operation == 0) { cout << "退出操作。" << endl; break; } Team team; cout << "请输入参赛队编号: "; cin >> team.number; if (!team_exists(teams, team.number) && operation != 1) { cout << "参赛队编号不存在。" << endl; continue; } if (operation == 1) { if (team_exists(teams, team.number)) { cout << "参赛队编号已存在。" << endl; continue; } cout << "请输入参赛作品名称: "; cin.ignore(); getline(cin, team.work_name); cout << "请输入参赛学校: "; getline(cin, team.school); cout << "请输入赛事类别: "; getline(cin, team.category); cout << "请输入参赛者: "; getline(cin, team.participants); cout << "请输入指导教师: "; getline(cin, team.mentor); add_team(teams, team); write_teams_to_file(teams, file_path); cout << "添加成功!" << endl; } else if (operation == 2) { delete_team(teams, team.number); write_teams_to_file(teams, file_path); cout << "删除成功!" << endl; } else if (operation == 3) { Team old_team = get_team(teams, team.number); cin.ignore(); team.work_name = input_with_default("请输入新的参赛作品名称", old_team.work_name); team.school = input_with_default("请输入新的参赛学校", old_team.school); team.category = input_with_default("请输入新的赛事类别", old_team.category); team.participants = input_with_default("请输入新的参赛者", old_team.participants); team.mentor = input_with_default("请输入新的指导教师", old_team.mentor); modify_team(teams, team); write_teams_to_file(teams, file_path); cout << "修改成功!" << endl; } else { cout << "无效的操作代码!" << endl; } } return 0;}

2-

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

struct Team {
    int number = 0;
    string work_name = "";
    string school = "";
    string category = "";
    string participants = "";
    string mentor = "";
};

struct Node {
    Team data;
    Node* left;
    Node* right;

    Node(Team val) : data(val), left(NULL), right(NULL) {}
};

class TeamBST {
public:
    TeamBST() : root(NULL) {}

    void add_member(Team t) {
        if (t.number >= 0) {
            root = insert_member(root, t);
        }
    }

    bool find_member(int number, Team& t, int& depth) {
        depth = 0;
        return search_member(root, number, t, depth);
    }

    double avg_search_length() {
        int total_depth = 0;
        int total_nodes = 0;
        calculate_ASL(root, total_depth, total_nodes);
        return (double)total_depth / total_nodes;
    }

private:
    Node* root;

    Node* insert_member(Node* node, Team t) {
        if (node == NULL) {
            return new Node(t);
        }

        if (t.number < node->data.number) {
            node->left = insert_member(node->left, t);
        }
        else if (t.number > node->data.number) {
            node->right = insert_member(node->right, t);
        }

        return node;
    }

    bool search_member(Node* node, int number, Team& t, int& depth) {
        if (node == NULL) {
            return false;
        }

        depth++;

        if (number < node->data.number) {
            return search_member(node->left, number, t, depth);
        }
        else if (number > node->data.number) {
            return search_member(node->right, number, t, depth);
        }
        else {
            t = node->data;
            return true;
        }
    }

    void calculate_ASL(Node* node, int& total_depth, int& total_nodes, int depth = 0) {
        if (node == NULL) {
            return;
        }

        depth++;

        total_depth += depth;
        total_nodes++;

        calculate_ASL(node->left, total_depth, total_nodes, depth);
        calculate_ASL(node->right, total_depth, total_nodes, depth);
    }
};

vector<string> split_string(const string& s, char delimiter) {
    vector<string> elems;
    stringstream ss(s);
    string item;
    while (getline(ss, item, delimiter)) {
        elems.push_back(item);
    }
    return elems;
}

Team string_to_team(const string& line) {
    vector<string> team_data = split_string(line, '#');
    Team t;
    try {
        t.number = stoi(team_data[0]);
    }
    catch (const invalid_argument& e) {
        t.number = -1;
    }
    t.work_name = team_data[1];
    t.school = team_data[2];
    t.category = team_data[3];
    t.participants = team_data[4];
    t.mentor = team_data[5];
    return t;
}

int main() {
    ifstream team_file("C:/Users/yh/Desktop/team.txt");
    string line;
    TeamBST team_tree;

    while (getline(team_file, line)) {
        team_tree.add_member(string_to_team(line));
    }

    team_file.close();

    int number;
    cout << "请输入参赛队编号: ";
    cin >> number;
    Team t;
    int depth;

    if (team_tree.find_member(number, t, depth)) {
        cout << "查找成功!" << endl;
        cout << "参赛作品名称: " << t.work_name << endl;
        cout << "参赛学校: " << t.school << endl;
        cout << "赛事类别: " << t.category << endl;
        cout << "参赛者: " << t.participants << endl;
        cout << "指导教师: " << t.mentor << endl;
        cout << "查找成功时的平均查找长度ASL: " << fixed << setprecision(2) << team_tree.avg_search_length() << endl;
    }
    else {
        cout << "查找失败!" << endl;
    }

    return 0;
}

3-

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

struct Team {
    int number=0;
    string work_name;
    string school;
    string category;
    string participants;
    string mentor;
};

vector<string> split_string(const string& s, char delimiter) {
    vector<string> elems;
    stringstream ss(s);
    string item;
    while (getline(ss, item, delimiter)) {
        elems.push_back(item);
    }
    return elems;
}

Team string_to_team(const string& line) {
    if (line.empty()) {
        // Empty line, not creating a Team
        return Team();
    }

    vector<string> team_data = split_string(line, '#');
    // Removing any potential spaces
    for (string& s : team_data) {
        s.erase(remove_if(s.begin(), s.end(), [](unsigned char x) { return isspace(x); }), s.end());
    }

    // Skipping the header line
    if (team_data[0] == "参赛队编号") {
        return Team();
    }

    Team t;
    t.number = stoi(team_data[0]);
    t.work_name = team_data[1];
    t.school = team_data[2];
    t.category = team_data[3];
    t.participants = team_data[4];
    t.mentor = team_data[5];
    return t;
}

// 插入排序(根据Category字段排序)
void insertion_sort(vector<Team>& matched_teams) {
    for (int i = 1; i < matched_teams.size(); i++) {
        int j = i;
        Team tmp = matched_teams[i];
        while (j > 0 && matched_teams[j - 1].category > tmp.category) {
            matched_teams[j] = matched_teams[j - 1];
            j--;
        }
        matched_teams[j] = tmp;
    }
}

int main() {
    ifstream team_file("C:/Users/yh/Desktop/team.txt");
    string line;
    vector<Team> all_teams;

    while (getline(team_file, line)) {
        Team team = string_to_team(line);
        if (team.number != 0) {
            all_teams.push_back(team);
        }
    }

    team_file.close();

    string input_query;
    cout << "请输入参赛学校或赛事类别:";
    cin >> input_query;

    vector<Team> matched_teams;

    for (const auto& team : all_teams) {
        if (team.category == input_query || team.school == input_query) {
            matched_teams.push_back(team);
        }
    }

    if (!matched_teams.empty()) {
        insertion_sort(matched_teams);

        cout << "参赛团队:" << endl;
        for (const auto& team : matched_teams) {
            cout << "编号:" << team.number
                << " 作品名称:" << team.work_name
                << " 参赛学校:" << team.school
                << " 赛事类别:" << team.category
                << " 参赛者:" << team.participants
                << " 指导教师:" << team.mentor << endl;
        }
    }
    else {
        cout << "没有找到符合条件的团队。" << endl;
    }

    return 0;
}

 

4-

 

#include <iostream>

#include <fstream>

#include <vector>

#include <sstream>

#include <string>

#include <map>

#include <set>

#include <algorithm>

#include <cctype>

#include <thread>

#include <chrono>

 

using namespace std;

 

struct Team {

    int number;

    string work_name;

    string school;

    string category;

    string participants;

    string mentor;

};

 

vector<string> split_string(const string& s, char delimiter) {

    vector<string> elems;

    stringstream ss(s);

    string item;

    while (getline(ss, item, delimiter)) {

        elems.push_back(item);

    }

    return elems;

}

 

Team string_to_team(const string& line) {

    if (line.empty()) {

        // Empty line, not creating a Team

        return Team();

    }

 

    vector<string> team_data = split_string(line, '#');

    // Removing any potential spaces

    for (string& s : team_data) {

        s.erase(remove_if(s.begin(), s.end(), [](unsigned char x) { return isspace(x); }), s.end());

    }

 

    // Skipping the header line

    if (team_data[0] == "参赛队编号") {

        return Team();

    }

 

    Team t;

    t.number = stoi(team_data[0]);

    t.work_name = team_data[1];

    t.school = team_data[2];

    t.category = team_data[3];

    t.participants = team_data[4];

    t.mentor = team_data[5];

    return t;

}

 

vector<Team> load_teams(const string& filename) {

    ifstream team_file(filename);

    string line;

    vector<Team> all_teams;

 

    while (getline(team_file, line)) {

        Team team = string_to_team(line);

        if (team.number != 0) {

            all_teams.push_back(team);

        }

    }

 

    team_file.close();

    return all_teams;

}

 

void announce_teams(const vector<Team>& teams) {

    string current_category;

 

    for (size_t i = 0; i < teams.size(); ++i) {

        const auto& team = teams[i];

 

        if (team.category != current_category) {

            if (!current_category.empty()) {

                cout << "当前赛事类别:" << current_category << " 结束。" << endl;

            }

            current_category = team.category;

            cout << "当前赛事类别:" << current_category << " 开始。" << endl;

        }

 

        cout << "请参赛队编号 " << team.number

             << "(" << team.school << " - " << team.work_name << "),进入赛场。" << endl;

        this_thread::sleep_for(chrono::seconds(1));

    }

 

    cout << "当前赛事类别:" << current_category << " 结束。" << endl;

}

 

int main() {

    string filename = "C:/Users/yh/Desktop/team.txt";

    vector<Team> all_teams = load_teams(filename);

 

    // Create a map to store all teams based on categories

    map<string, vector<Team>> categorized_teams;

    for (const auto& team : all_teams) {

        categorized_teams[team.category].push_back(team);

    }

 

    int num_rooms = 9;

    vector<vector<Team>> room_teams(num_rooms);

 

    int room_index = 0;

    for (const auto& entry : categorized_teams) {

        for (const auto& team : entry.second) {

            room_teams[room_index % num_rooms].push_back(team);

        }

        room_index++;

        if (room_index >= num_rooms) {

            room_index = 0;

        }

    }

 

    for (int i = 0; i < num_rooms; i++) {

        cout << "决赛室 " << i + 1 << ":" << endl;

        announce_teams(room_teams[i]);

    }

 

    return 0;

}

5-

 

#include <iostream>

#include <vector>

#include <string>

#include <algorithm>

#include <map>

#include <limits.h>

 

using namespace std;

 

// Dijkstra algorithm to find shortest path between two nodes

vector<int> dijkstra(vector<vector<pair<int, int> > >& graph, int start, int end) {

    int n = graph.size();

    vector<int> dist(n, INT_MAX);

    vector<bool> visited(n, false);

    vector<int> prev(n, -1);

    dist[start] = 0;

 

    for (int i = 0; i < n; i++) {

        // Find the vertex with minimum distance

        int min_dist = INT_MAX, u = -1;

        for (int j = 0; j < n; j++) {

            if (!visited[j] && dist[j] < min_dist) {

                min_dist = dist[j];

                u = j;

            }

        }

        visited[u] = true;

 

        // Update the distance value of the adjacent vertices

        for (auto& neighbor : graph[u]) {

            int v = neighbor.first, weight = neighbor.second;

            if (!visited[v] && dist[u] + weight < dist[v]) {

                dist[v] = dist[u] + weight;

                prev[v] = u;

            }

        }

    }

 

    vector<int> route;

    for (int u = end; u != -1; u = prev[u]) {

        route.push_back(u);

    }

    reverse(route.begin(), route.end());

    return route;

}

 

int main() {

    int n, m;

    n = 10; // Number of nodes (buildings)

    m = 14; // Number of edges (paths between buildings)

    map<int, string> node_to_name = { {0, "Building1"}, {1, "Building2"}, {2, "Building3"},

                                      {3, "Building4"}, {4, "Building5"}, {5, "Building6"},

                                      {6, "Building7"}, {7, "Building8"}, {8, "Building9"},

                                      {9, "Building10"} };

    // Adding edges in graph

    vector<vector<pair<int, int> > > graph(n);

    graph[0] = { {1, 100}, {3, 200} };

    graph[1] = { {0, 100}, {2, 80}, {3, 150} };

    graph[2] = { {1, 80}, {4, 120},{5,110} };

    graph[3] = { {0, 200}, {1, 150}, {4, 50} };

    graph[4] = { {2, 120}, {3, 50}, {7, 150},{8,230} };

    graph[5] = { {2, 110}, {6, 80}, {7, 60} };

    graph[6] = { {5, 80}, {9, 100} };

    graph[7] = { {5, 60}, {8, 90}, {9, 70} };

    graph[8] = { {7, 90}, {9, 50} };

    graph[9] = { {6, 100},{8,50} };

 

    int start, end;

    cout << "请输入起始建筑编号(1-10): " << endl;

    cin >> start;

    cout << "请输入目标建筑编号(1-10): " << endl;

    cin >> end;

 

    vector<int> route = dijkstra(graph, start-1, end-1);

    cout << "从 " << node_to_name[start-1] << " 到 " << node_to_name[end-1] << " 的最短路径为:" << endl;

    for (int i = 0; i < route.size(); i++) {

        cout << node_to_name[route[i]];

        if (i < route.size() - 1) {

            cout << " -> ";

        }

        else {

            cout << endl;

        }

    }

 

    return 0;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值