C++系统设计题目

社交网络系统的功能,包括用户注册、发布消息、关注其他用户、浏览消息流、点赞和评论等:

cpp 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>

using namespace std;

// 用户类
class User {
private:
    string username;
    unordered_set<string> following;
    vector<string> messages;

public:
    User(string _username) : username(_username) {}

    string getUsername() const {
        return username;
    }

    void follow(User& user) {
        following.insert(user.getUsername());
    }

    void unfollow(User& user) {
        following.erase(user.getUsername());
    }

    void postMessage(const string& message) {
        messages.push_back(message);
    }

    const vector<string>& getMessages() const {
        return messages;
    }

    const unordered_set<string>& getFollowing() const {
        return following;
    }
};

// 社交网络系统类
class SocialNetwork {
private:
    unordered_map<string, User> users;

public:
    // 用户注册
    void registerUser(const string& username) {
        users[username] = User(username);
    }

    // 获取用户
    User* getUser(const string& username) {
        if (users.find(username) != users.end()) {
            return &users[username];
        }
        return nullptr;
    }

    // 用户关注
    void followUser(const string& follower, const string& followee) {
        User* followerUser = getUser(follower);
        User* followeeUser = getUser(followee);
        if (followerUser && followeeUser) {
            followerUser->follow(*followeeUser);
        }
    }

    // 用户取消关注
    void unfollowUser(const string& follower, const string& followee) {
        User* followerUser = getUser(follower);
        User* followeeUser = getUser(followee);
        if (followerUser && followeeUser) {
            followerUser->unfollow(*followeeUser);
        }
    }

    // 发布消息
    void postMessage(const string& username, const string& message) {
        User* user = getUser(username);
        if (user) {
            user->postMessage(message);
        }
    }

    // 获取用户消息流
    vector<string> getTimeline(const string& username) {
        vector<string> timeline;
        User* user = getUser(username);
        if (user) {
            timeline = user->getMessages();
            for (const auto& followee : user->getFollowing()) {
                User* followeeUser = getUser(followee);
                if (followeeUser) {
                    const vector<string>& messages = followeeUser->getMessages();
                    timeline.insert(timeline.end(), messages.begin(), messages.end());
                }
            }
            sort(timeline.begin(), timeline.end()); // 假设按照发布时间排序
        }
        return timeline;
    }
};

int main() {
    SocialNetwork network;

    // 注册用户
    network.registerUser("Alice");
    network.registerUser("Bob");
    network.registerUser("Charlie");

    // 用户关注
    network.followUser("Alice", "Bob");
    network.followUser("Alice", "Charlie");

    // 用户发布消息
    network.postMessage("Alice", "Hello, this is Alice's message!");
    network.postMessage("Bob", "This is Bob's message!");

    // 获取用户消息流
    vector<string> aliceTimeline = network.getTimeline("Alice");
    cout << "Alice's Timeline:" << endl;
    for (const auto& message : aliceTimeline) {
        cout << message << endl;
    }

    return 0;
}

在线电影订票系统的基本功能,包括搜索电影、选择座位、支付订单、管理订单和取消订单等:

cpp 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>

using namespace std;

// 电影类
class Movie {
public:
    string title;
    int availableSeats;

    Movie(string _title, int _availableSeats) : title(_title), availableSeats(_availableSeats) {}
};

// 订单类
class Order {
public:
    string movieTitle;
    int numTickets;

    Order(string _movieTitle, int _numTickets) : movieTitle(_movieTitle), numTickets(_numTickets) {}
};

// 电影订票系统类
class MovieTicketSystem {
private:
    unordered_map<string, Movie> movieDatabase;
    vector<Order> orders;

public:
    // 添加电影到数据库
    void addMovie(string title, int availableSeats) {
        Movie movie(title, availableSeats);
        movieDatabase[title] = movie;
    }

    // 搜索电影
    bool searchMovie(string title) {
        if (movieDatabase.find(title) != movieDatabase.end()) {
            cout << "Movie Found: " << title << endl;
            return true;
        } else {
            cout << "Movie Not Found: " << title << endl;
            return false;
        }
    }

    // 选择座位并下订单
    void bookTickets(string movieTitle, int numTickets) {
        if (movieDatabase.find(movieTitle) != movieDatabase.end()) {
            Movie& movie = movieDatabase[movieTitle];
            if (movie.availableSeats >= numTickets) {
                movie.availableSeats -= numTickets;
                Order order(movieTitle, numTickets);
                orders.push_back(order);
                cout << numTickets << " tickets booked for movie: " << movieTitle << endl;
            } else {
                cout << "Not enough seats available for movie: " << movieTitle << endl;
            }
        } else {
            cout << "Movie Not Found: " << movieTitle << endl;
        }
    }

    // 支付订单
    void payOrder() {
        // 实现支付逻辑,这里简化为输出订单信息
        cout << "Payment Successful" << endl;
        for (const auto& order : orders) {
            cout << "Movie: " << order.movieTitle << ", Tickets: " << order.numTickets << endl;
        }
        orders.clear();
    }

    // 取消订单
    void cancelOrder(string movieTitle, int numTickets) {
        for (auto it = orders.begin(); it != orders.end(); ++it) {
            if (it->movieTitle == movieTitle && it->numTickets == numTickets) {
                Movie& movie = movieDatabase[movieTitle];
                movie.availableSeats += numTickets;
                orders.erase(it);
                cout << "Order canceled successfully for movie: " << movieTitle << endl;
                return;
            }
        }
        cout << "Order not found for movie: " << movieTitle << ", Tickets: " << numTickets << endl;
    }
};

int main() {
    MovieTicketSystem system;

    // 添加电影
    system.addMovie("Inception", 100);
    system.addMovie("The Dark Knight", 150);

    // 搜索电影
    system.searchMovie("Inception");

    // 选择座位并下订单
    system.bookTickets("Inception", 3);
    system.bookTickets("Inception", 5);

    // 支付订单
    system.payOrder();

    // 取消订单
    system.cancelOrder("Inception", 3);

    return 0;
}
相关推荐
C^h1 小时前
c++ string
开发语言·c++
raindayinrain1 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的1 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao2 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
小徐不徐说3 小时前
C++ 接口与插件设计:从工厂注册到 Qt 动态插件
数据库·c++·qt·系统架构·程序设计
大都督会赢的3 小时前
C++第一节课
开发语言·c++
UIU1143 小时前
P1233 [ICPC 2001 Taejon R] 木棍加工
数据结构·c++·算法·动态规划
论文复现现场3 小时前
WRF、FEA 能用 L40S 加速吗?求解器检查、断点恢复与长期运行
java·数据库·算法·wrf·有限元分析·gpu计算·断点续算
ZhengEnCi3 小时前
用 AI 写代码为什么拼到最后还是拼人:算力瓶颈与领域专家提示词的本质差异
算法·架构
TMedical4 小时前
MedPlanning 截骨模拟矫形功能
c++·图像处理·人工智能·qt·健康医疗