C++ 会议室管理系统

系统功能

  • 添加/删除会议室
  • 预约/取消预约会议室
  • 查询会议室状态
  • 显示所有会议室信息
  • 保存/加载数据

代码

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <ctime>
#include <algorithm>
#include <iomanip>

using namespace std;

// 预约时间结构体
struct ReservationTime {
    int year;
    int month;
    int day;
    int startHour;
    int endHour;
    
    ReservationTime(int y, int m, int d, int sh, int eh) 
        : year(y), month(m), day(d), startHour(sh), endHour(eh) {}
    
    // 重载小于运算符,用于时间比较
    bool operator<(const ReservationTime& other) const {
        if (year != other.year) return year < other.year;
        if (month != other.month) return month < other.month;
        if (day != other.day) return day < other.day;
        if (startHour != other.startHour) return startHour < other.startHour;
        return endHour < other.endHour;
    }
    
    // 检查时间是否重叠
    bool overlaps(const ReservationTime& other) const {
        if (year != other.year || month != other.month || day != other.day) 
            return false;
        
        return !(endHour <= other.startHour || startHour >= other.endHour);
    }
};

// 预约记录结构体
struct Reservation {
    string reservator;
    ReservationTime time;
    
    Reservation(const string& r, const ReservationTime& t) 
        : reservator(r), time(t) {}
};

// 会议室类
class MeetingRoom {
private:
    int roomId;
    string roomName;
    int capacity;
    bool hasProjector;
    vector<Reservation> reservations;
    
public:
    MeetingRoom(int id, const string& name, int cap, bool projector)
        : roomId(id), roomName(name), capacity(cap), hasProjector(projector) {}
    
    // 检查时间段是否可用
    bool isAvailable(const ReservationTime& time) const {
        for (const auto& reservation : reservations) {
            if (reservation.time.overlaps(time)) {
                return false;
            }
        }
        return true;
    }
    
    // 添加预约
    bool addReservation(const string& reservator, const ReservationTime& time) {
        if (!isAvailable(time)) {
            return false;
        }
        reservations.emplace_back(reservator, time);
        sort(reservations.begin(), reservations.end(), 
             [](const Reservation& a, const Reservation& b) { 
                 return a.time < b.time; 
             });
        return true;
    }
    
    // 取消预约
    bool cancelReservation(const string& reservator, const ReservationTime& time) {
        for (auto it = reservations.begin(); it != reservations.end(); ++it) {
            if (it->reservator == reservator && 
                it->time.year == time.year &&
                it->time.month == time.month &&
                it->time.day == time.day &&
                it->time.startHour == time.startHour &&
                it->time.endHour == time.endHour) {
                reservations.erase(it);
                return true;
            }
        }
        return false;
    }
    
    // 显示会议室信息
    void display() const {
        cout << "会议室ID: " << roomId << endl;
        cout << "名称: " << roomName << endl;
        cout << "容量: " << capacity << "人" << endl;
        cout << "投影仪: " << (hasProjector ? "有" : "无") << endl;
        cout << "预约记录:" << endl;
        
        for (const auto& reservation : reservations) {
            cout << "  " << reservation.reservator << ": " 
                 << reservation.time.year << "-"
                 << setw(2) << setfill('0') << reservation.time.month << "-"
                 << setw(2) << setfill('0') << reservation.time.day << " "
                 << setw(2) << setfill('0') << reservation.time.startHour << ":00-"
                 << setw(2) << setfill('0') << reservation.time.endHour << ":00" << endl;
        }
        cout << endl;
    }
    
    // 获取会议室ID
    int getId() const { return roomId; }
    
    // 获取会议室名称
    string getName() const { return roomName; }
    
    // 获取容量
    int getCapacity() const { return capacity; }
    
    // 是否有投影仪
    bool hasProj() const { return hasProjector; }
    
    // 获取所有预约记录
    const vector<Reservation>& getReservations() const { return reservations; }
    
    // 保存数据到文件
    void saveToFile(ofstream& file) const {
        file << roomId << " " << roomName << " " << capacity << " " << hasProjector << endl;
        file << reservations.size() << endl;
        for (const auto& reservation : reservations) {
            file << reservation.reservator << " "
                 << reservation.time.year << " "
                 << reservation.time.month << " "
                 << reservation.time.day << " "
                 << reservation.time.startHour << " "
                 << reservation.time.endHour << endl;
        }
    }
    
    // 从文件加载数据
    void loadFromFile(ifstream& file) {
        int reservationCount;
        file >> roomId >> roomName >> capacity >> hasProjector;
        file >> reservationCount;
        
        reservations.clear();
        for (int i = 0; i < reservationCount; ++i) {
            string reservator;
            int y, m, d, sh, eh;
            file >> reservator >> y >> m >> d >> sh >> eh;
            reservations.emplace_back(reservator, ReservationTime(y, m, d, sh, eh));
        }
    }
};

// 会议室管理系统类
class MeetingRoomManager {
private:
    vector<MeetingRoom> rooms;
    
public:
    // 添加会议室
    void addRoom(int id, const string& name, int capacity, bool hasProjector) {
        rooms.emplace_back(id, name, capacity, hasProjector);
        cout << "添加会议室成功!" << endl;
    }
    
    // 删除会议室
    bool removeRoom(int id) {
        for (auto it = rooms.begin(); it != rooms.end(); ++it) {
            if (it->getId() == id) {
                rooms.erase(it);
                cout << "删除会议室成功!" << endl;
                return true;
            }
        }
        cout << "未找到该会议室!" << endl;
        return false;
    }
    
    // 预约会议室
    bool reserveRoom(int id, const string& reservator, const ReservationTime& time) {
        for (auto& room : rooms) {
            if (room.getId() == id) {
                if (room.addReservation(reservator, time)) {
                    cout << "预约成功!" << endl;
                    return true;
                } else {
                    cout << "该时间段已被预约!" << endl;
                    return false;
                }
            }
        }
        cout << "未找到该会议室!" << endl;
        return false;
    }
    
    // 取消预约
    bool cancelReservation(int id, const string& reservator, const ReservationTime& time) {
        for (auto& room : rooms) {
            if (room.getId() == id) {
                if (room.cancelReservation(reservator, time)) {
                    cout << "取消预约成功!" << endl;
                    return true;
                } else {
                    cout << "未找到该预约记录!" << endl;
                    return false;
                }
            }
        }
        cout << "未找到该会议室!" << endl;
        return false;
    }
    
    // 查询会议室状态
    void queryRoom(int id) const {
        for (const auto& room : rooms) {
            if (room.getId() == id) {
                room.display();
                return;
            }
        }
        cout << "未找到该会议室!" << endl;
    }
    
    // 显示所有会议室
    void displayAllRooms() const {
        if (rooms.empty()) {
            cout << "暂无会议室数据!" << endl;
            return;
        }
        
        for (const auto& room : rooms) {
            room.display();
        }
    }
    
    // 根据条件查询可用会议室
    void findAvailableRooms(const ReservationTime& time, int minCapacity = 0, bool needProjector = false) {
        cout << "符合以下条件的可用会议室:" << endl;
        cout << "时间: " << time.year << "-" << time.month << "-" << time.day 
             << " " << time.startHour << ":00-" << time.endHour << ":00" << endl;
        cout << "最小容量: " << minCapacity << endl;
        cout << "需要投影仪: " << (needProjector ? "是" : "否") << endl;
        cout << "----------------------------------------" << endl;
        
        bool found = false;
        for (const auto& room : rooms) {
            if (room.getCapacity() >= minCapacity && 
                (!needProjector || room.hasProj()) && 
                room.isAvailable(time)) {
                cout << "会议室ID: " << room.getId() << endl;
                cout << "名称: " << room.getName() << endl;
                cout << "容量: " << room.getCapacity() << "人" << endl;
                cout << "投影仪: " << (room.hasProj() ? "有" : "无") << endl;
                cout << "----------------------------------------" << endl;
                found = true;
            }
        }
        
        if (!found) {
            cout << "没有找到符合条件的可用会议室!" << endl;
        }
    }
    
    // 保存数据到文件
    void saveToFile(const string& filename) {
        ofstream file(filename);
        if (!file) {
            cout << "无法打开文件进行保存!" << endl;
            return;
        }
        
        file << rooms.size() << endl;
        for (const auto& room : rooms) {
            room.saveToFile(file);
        }
        
        file.close();
        cout << "数据已保存到 " << filename << endl;
    }
    
    // 从文件加载数据
    void loadFromFile(const string& filename) {
        ifstream file(filename);
        if (!file) {
            cout << "无法打开文件进行加载!" << endl;
            return;
        }
        
        int roomCount;
        file >> roomCount;
        
        rooms.clear();
        for (int i = 0; i < roomCount; ++i) {
            int id, capacity;
            string name;
            bool hasProjector;
            
            file >> id >> name >> capacity >> hasProjector;
            MeetingRoom room(id, name, capacity, hasProjector);
            room.loadFromFile(file);
            rooms.push_back(room);
        }
        
        file.close();
        cout << "已从 " << filename << " 加载数据" << endl;
    }
};

// 显示主菜单
void displayMenu() {
    cout << "===== 会议室管理系统 =====" << 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;
    cout << "0. 退出系统" << endl;
    cout << "请选择操作: ";
}

// 获取时间输入
ReservationTime getTimeInput() {
    int y, m, d, sh, eh;
    cout << "请输入日期 (年 月 日): ";
    cin >> y >> m >> d;
    cout << "请输入开始时间和结束时间 (小时 0-23): ";
    cin >> sh >> eh;
    return ReservationTime(y, m, d, sh, eh);
}

int main() {
    MeetingRoomManager manager;
    int choice;
    
    do {
        displayMenu();
        cin >> choice;
        
        switch (choice) {
            case 1: {
                int id, capacity;
                string name;
                bool hasProjector;
                cout << "请输入会议室ID: ";
                cin >> id;
                cout << "请输入会议室名称: ";
                cin >> name;
                cout << "请输入会议室容量: ";
                cin >> capacity;
                cout << "是否有投影仪 (1/0): ";
                cin >> hasProjector;
                manager.addRoom(id, name, capacity, hasProjector);
                break;
            }
            case 2: {
                int id;
                cout << "请输入要删除的会议室ID: ";
                cin >> id;
                manager.removeRoom(id);
                break;
            }
            case 3: {
                int id;
                string reservator;
                cout << "请输入要预约的会议室ID: ";
                cin >> id;
                cout << "请输入预约人姓名: ";
                cin >> reservator;
                ReservationTime time = getTimeInput();
                manager.reserveRoom(id, reservator, time);
                break;
            }
            case 4: {
                int id;
                string reservator;
                cout << "请输入要取消预约的会议室ID: ";
                cin >> id;
                cout << "请输入预约人姓名: ";
                cin >> reservator;
                ReservationTime time = getTimeInput();
                manager.cancelReservation(id, reservator, time);
                break;
            }
            case 5: {
                int id;
                cout << "请输入要查询的会议室ID: ";
                cin >> id;
                manager.queryRoom(id);
                break;
            }
            case 6:
                manager.displayAllRooms();
                break;
            case 7: {
                int minCapacity;
                bool needProjector;
                cout << "请输入最小容量要求: ";
                cin >> minCapacity;
                cout << "是否需要投影仪 (1/0): ";
                cin >> needProjector;
                ReservationTime time = getTimeInput();
                manager.findAvailableRooms(time, minCapacity, needProjector);
                break;
            }
            case 8:
                manager.saveToFile("meeting_rooms.txt");
                break;
            case 9:
                manager.loadFromFile("meeting_rooms.txt");
                break;
            case 0:
                cout << "感谢使用会议室管理系统!" << endl;
                break;
            default:
                cout << "无效选择,请重新输入!" << endl;
        }
        
        cout << endl;
    } while (choice != 0);
    
    return 0;
}

功能

  1. 会议室管理

    • 添加会议室:设置ID、名称、容量和是否有投影仪
    • 删除会议室:根据ID删除会议室
  2. 预约管理

    • 预约会议室:选择会议室、预约人和时间
    • 取消预约:取消已有的预约记录
    • 查询会议室状态:查看特定会议室的详细信息
  3. 查询功能

    • 显示所有会议室:列出系统中所有会议室
    • 查找可用会议室:根据时间、容量和投影仪需求查找可用会议室
  4. 数据持久化

    • 保存数据:将会议室和预约信息保存到文件
    • 加载数据:从文件加载之前保存的数据

编译和运行

  1. 将代码保存为 meeting_room_system.cpp

  2. 使用C++编译器编译:

    g++ -std=c++11 meeting_room_system.cpp -o meeting_room_system
    
  3. 运行程序:

    ./meeting_room_system
    

参考系统 C++编写的会议室管理系统 www.youwenfan.com/contentcne/102871.html

扩展建议

  1. 用户认证:添加用户登录和权限管理功能
  2. 图形界面:使用Qt等框架开发图形用户界面
  3. 网络功能:添加网络支持,实现远程预约
  4. 邮件通知:预约成功或取消时发送邮件通知
  5. 数据库支持:使用SQLite或MySQL替代文件存储
  6. 日历集成:与Google Calendar或Outlook集成
posted @ 2025-08-27 16:36  徐中翼  阅读(11)  评论(0)    收藏  举报