程序设计机房管理系统c++

2、机房管理系统

实现简单机房管理系统的功能,具体管理操作包括机房的增加、删除,机房状态的查询、修改等。

(- )系统功能要求及其说明:

1、 增加功能

能录入新机房的相关数据(一组数据包括:机房编号,最大容纳人数,机房分类,机房

使用状态》。例如

机房八80软件机房未使用

机房类别包括:软件机房,硬件机房,ERP机房,华商机房四种。

当录入了重复的机房编号时,则提示数据录入重复并取消录入:录入的新数据能按递增的顺序自动进行条目编号(并保存到文件中》。要求一次能完成多条信息的输入。

2、查询功能

输入机房编号时,可以查询该机房的详细信思,包括是否正在使用中。

3、修改功能

当选中具体的机房编号时,可修改该机房相关信息,若该机房正在使用中,则拒绝修改。修改时可选择修改机房的所属类别或者最大容纳人数以及使用状态。

、删除功能

当选中具体的机房编号时,可删除该机房相关信息,若该机房正在使用中,则拒绝删除。、分类查看功能(可选)

选择此功能时,可以根据机房炎别进行选择。

当选中某类时,显示出此类中所有数据的信息(一屏最多显示10条超过十条应能够自动分屏显示),并提示该类别机房总的数量、任在使用中的数量。

6、测试数据:要求机房记录有40个左右。

(二)其他要求:

使用C/C+语言,源程序要有适当的注释,使程序容易阅读

采用文本菜单界面

短时间难完成,因为涉及到文件操作,算比较复杂的了

该回答引用ChatGPT
根据题目描述,我们可以设计出以下程序设计机房管理系统的基本框架:

1. 定义机房结构体,包括机房编号、最大容纳人数、机房分类、机房使用状态等信息。

2. 定义机房管理类,提供机房增加、删除、查询、修改等功能。具体实现如下:

(1)机房增加功能:先从文件读取原有机房信息,添加新的机房信息后排序后保存到文件中。

(2)查询功能:根据机房编号查询相应机房信息并打印出来。

(3)修改功能:先查询机房是否正在使用中,若未使用中则可修改机房信息,否则拒绝修改。

(4)删除功能:先查询机房是否正在使用中,若未使用中则可删除机房信息,否则拒绝删除。

(5)分类查看功能:根据机房分类查询,一屏最多显示10条超过十条应自动分屏显示。

3. 使用文本菜单界面,提供用户选择相关功能。

以下是程序设计机房管理系统的详细代码实现:

c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

// 机房结构体
struct Classroom
{
string id; // 机房编号
int capacity; // 最大容纳人数
string category; // 机房分类
string state; // 机房使用状态
};

// 机房管理类
class ClassroomManager
{
public:
// 构造函数,从文件中读取机房信息
ClassroomManager()
{
ifstream ifs("classroom.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
return;
}
Classroom c;
while (ifs >> c.id && ifs >> c.capacity && ifs >> c.category && ifs >> c.state)
{
m_classrooms.push_back(c);
}
ifs.close();
}

// 析构函数,将机房信息保存到文件中
~ClassroomManager()
{
ofstream ofs("classroom.txt", ios::out);
if (!ofs.is_open())
{
cout << "文件打开失败!" << endl;
return;
}
for (auto it = m_classrooms.begin(); it != m_classrooms.end(); ++it)
{
ofs << it->id << " " << it->capacity << " " << it->category << " " << it->state << endl;
}
ofs.close();
}

// 机房增加功能
void addClassroom()
{
Classroom c;
cout << "请输入机房编号:";
cin >> c.id;
if (find(m_classrooms.begin(), m_classrooms.end(), c) != m_classrooms.end())
{
cout << "机房编号已经存在,无法添加!" << endl;
return;
}
cout << "请输入最大容纳人数:";
cin >> c.capacity;
cout << "请输入机房分类:";
cin >> c.category;
cout << "请输入机房使用状态:";
cin >> c.state;
m_classrooms.push_back(c);
sort(m_classrooms.begin(), m_classrooms.end(), [](Classroom& c1, Classroom& c2) { return c1.id < c2.id; });
cout << "机房添加成功!" << endl;
}

// 查询功能
void queryClassroom()
{
string id;
cout << "请输入机房编号:";
cin >> id;
auto it = find_if(m_classrooms.begin(), m_classrooms.end(), [id](Classroom& c) { return c.id == id; });
if (it == m_classrooms.end())
{
cout << "未找到机房信息!" << endl;
return;
}
cout << "机房编号:" << it->id << endl;
cout << "最大容纳人数:" << it->capacity << endl;
cout << "机房分类:" << it->category << endl;
cout << "机房使用状态:" << it->state << endl;
}

// 修改功能
void modifyClassroom()
{
string id;
cout << "请输入机房编号:";
cin >> id;
auto it = find_if(m_classrooms.begin(), m_classrooms.end(), [id](Classroom& c) { return c.id == id; });
if (it == m_classrooms.end())
{
cout << "未找到机房信息!" << endl;
return;
}
if (it->state == "在使用")
{
cout << "机房正在使用中,无法修改!" << endl;
return;
}
cout << "请输入新的最大容纳人数:";
cin >> it->capacity;
cout << "请输入新的机房分类:";
cin >> it->category;
cout << "请输入新的机房使用状态:";
cin >> it->state;
cout << "机房信息修改成功!" << endl;
}

// 删除功能
void deleteClassroom()
{
string id;
cout << "请输入机房编号:";
cin >> id;
auto it = find_if(m_classrooms.begin(), m_classrooms.end(), [id](Classroom& c) { return c.id == id; });
if (it == m_classrooms.end())
{
cout << "未找到机房信息!" << endl;
return;
}
if (it->state == "在使用")
{
cout << "机房正在使用中,无法删除!" << endl;
return;
}
m_classrooms.erase(it);
cout << "机房信息删除成功!" << endl;
}

// 分类查看功能
void viewClassroom()
{
vector<Classroom> v;
string category;
cout << "请输入机房分类:";
cin >> category;
for (auto it = m_classrooms.begin(); it != m_classrooms.end(); ++it)
{
if (it->category == category)
{
v.push_back(*it);
}
}
if (v.empty())
{
cout << "未找到机房信息!" << endl;
return;
}
int count = v.size();
int page = 1;
while (page <= (count + 9) / 10)
{
cout << "第" << page << "页:" << endl;
int begin = (page - 1) * 10;
int end = min(page * 10, count);
for (int i = begin; i < end; ++i)
{
cout << "机房编号:" << v[i].id << ",最大容纳人数:" << v[i].capacity << ",机房分类:" << v[i].category << ",机房使用状态:" << v[i].state << endl;
}
++page;
}
cout << "该类别机房总数:" << count << ",正在使用中的机房数量:" << countUsedClassroom(v) << endl;
}

private:
vector<Classroom> m_classrooms; // 机房数组

// 统计正在使用中的机房数量
int countUsedClassroom(vector<Classroom>& v)
{
int count = 0;
for (auto it = v.begin(); it != v.end(); ++it)
{
if (it->state == "在使用")
{
++count;
}
}
return count;
}
};

// 显示菜单
void showMenu()
{
cout << "***************************************" << endl;
cout << "***** 1. 添加机房 ************" << endl;
cout << "***** 2. 查询机房 ************" << endl;
cout << "***** 3. 修改机房 ************" << endl;
cout << "***** 4. 删除机房 ************" << endl;
cout << "***** 5. 分类查看 ************" << endl;
cout << "***** 0. 退出程序 ************" << endl;
cout << "***************************************" << endl;
}

int main()
{
ClassroomManager cm; // 创建机房管理对象
int choice;
while (true)
{
showMenu();
cout << "请输入您的选择:";
cin >> choice;
switch (choice)
{
case 1: // 添加机房
cm.addClassroom();
break;
case 2: // 查询机房
cm.queryClassroom();
break;
case 3: // 修改机房
cm.modifyClassroom();
break;
case 4: // 删除机房
cm.deleteClassroom();
break;
case 5: // 分类查看
cm.viewClassroom();
break;
case 0: // 退出程序
cout << "欢迎下次再使用本机房管理系统!" << endl;
return 0;
default:
cout << "您的输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下。
可以为您提供一个大致的思路和部分实现:
1、设计结构体或类来存储机房信息,包括机房编号、最大容纳人数、机房分类、机房使用状态等属性。

struct Classroom {
    int id; // 机房编号
    int capacity; // 最大容纳人数
    string category; // 机房分类
    bool in_use; // 机房使用状态
};

2、使用文件来保存机房信息,可以选择将每个机房信息存储在一行中,不同属性之间使用特定的分隔符隔开,方便读写。

void writeToFile(const vector<Classroom>& classrooms) {
    ofstream outfile("classrooms.txt");
    for (const auto& classroom : classrooms) {
        outfile << classroom.id << "," << classroom.capacity << "," << classroom.category << "," << classroom.in_use << endl;
    }
    outfile.close();
}

vector<Classroom> readFromFile() {
    vector<Classroom> classrooms;
    ifstream infile("classrooms.txt");
    if (!infile.is_open()) {
        cout << "Error opening file!" << endl;
        return classrooms;
    }
    string line;
    while (getline(infile, line)) {
        stringstream ss(line);
        string token;
        Classroom classroom;
        getline(ss, token, ',');
        classroom.id = stoi(token);
        getline(ss, token, ',');
        classroom.capacity = stoi(token);
        getline(ss, token, ',');
        classroom.category = token;
        getline(ss, token, ',');
        classroom.in_use = stoi(token);
        classrooms.push_back(classroom);
    }
    infile.close();
    return classrooms;
}

3、设计菜单功能,使用switch语句实现不同选项的功能。

void showMenu() {
    cout << "==============================" << endl;
    cout << "Welcome to Classroom Management System" << endl;
    cout << "1. Add Classroom" << endl;
    cout << "2. Query Classroom" << endl;
    cout << "3. Modify Classroom" << endl;
    cout << "4. Delete Classroom" << endl;
    cout << "5. View by Category" << endl;
    cout << "6. Exit" << endl;
    cout << "==============================" << endl;
}

void addClassroom(vector<Classroom>& classrooms) {
    // TODO: add classroom function
}

void queryClassroom(const vector<Classroom>& classrooms) {
    // TODO: query classroom function
}

void modifyClassroom(vector<Classroom>& classrooms) {
    // TODO: modify classroom function
}

void deleteClassroom(vector<Classroom>& classrooms) {
    // TODO: delete classroom function
}

void viewByCategory(const vector<Classroom>& classrooms) {
    // TODO: view by category function
}

int main() {
    vector<Classroom> classrooms = readFromFile();
    int choice = 0;
    while (true) {
        showMenu();
        cout << "Please enter your choice: ";
        cin >> choice;
        switch (choice) {
            case 1:
                addClassroom(classrooms);
                break;
            case 2:
                queryClassroom(classrooms);
                break;
            case 3:
                modifyClassroom(classrooms);
                break;
            case 4:
                deleteClassroom(classrooms);
                break;
            case 5:
                viewByCategory(classrooms);
                break;
            case 6:
                writeToFile(classrooms);
                cout << "Thanks for using Classroom Management System!" << endl;
                return 0;
            default:
                cout << "Invalid choice! Please try again." << endl;
                break;
        }
    }
    return 0;
}

4、实现不同功能函数,例如添加机房、查询机房、修改机房、删除机房和按分类查看机房等。


void addClassroom(vector<Classroom>& classrooms) {
    int id, capacity;
    string category;
    bool in_use = false;
    cout << "Please enter classroom id: ";
    cin >> id;
    for (const auto& classroom : classrooms) {
        if (classroom.id == id) {
            cout << "Classroom already exists!" << endl;
            return;
        }
    }
    cout << "Please enter classroom capacity: ";
    cin >> capacity;
    cout << "Please enter classroom category: ";
    cin >> category;
    classrooms.push_back({id, capacity, category, in_use});
    cout << "Classroom added

如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

引用chatGPT作答,首先,您需要设计一个机房类(Room),其成员变量包括机房编号、最大容纳人数、机房分类和机房使用状态。您可以在类中实现增加、删除、查询和修改机房信息的函数。

接下来,您需要设计一个主菜单,提供增加、查询、修改、删除和分类查看等功能。您可以使用switch语句来实现主菜单和子菜单。

对于增加机房信息的功能,您可以使用文件来保存机房信息。每次增加机房信息时,都需要打开文件,将机房信息追加到文件末尾,并关闭文件。

查询、修改和删除机房信息的功能需要先根据机房编号查询机房信息,如果找到了该机房,则可以进行相应的操作。如果没有找到该机房,需要提示用户重新输入机房编号。

分类查看功能可以根据机房分类来实现。您可以遍历机房数组,将同一分类的机房信息打印出来,并统计该类别机房总数和正在使用中的机房数量。

最后,为了使程序更易读,您需要添加适当的注释,说明每个函数和变量的作用。

以下是一个简单的代码示例:

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

using namespace std;

// 机房类
class Room {
public:
    Room(int id, int capacity, string type, bool used) :
        id_(id), capacity_(capacity), type_(type), used_(used) {}
    int getId() const { return id_; }
    int getCapacity() const { return capacity_; }
    string getType() const { return type_; }
    bool isUsed() const { return used_; }
    void setCapacity(int capacity) { capacity_ = capacity; }
    void setType(string type) { type_ = type; }
    void setUsed(bool used) { used_ = used; }
private:
    int id_; // 机房编号
    int capacity_; // 最大容纳人数
    string type_; // 机房分类
    bool used_; // 是否正在使用中
};

// 机房管理类
class RoomManager {
public:
    void addRoom(Room room); // 增加机房
    void deleteRoom(int id); // 删除机房
    void queryRoom(int id); // 查询机房
    void modifyRoom(int id); // 修改机房
    void printRooms(); // 打印机房信息
    void printRoomsByType(string type); // 按分类打印机房信息
private:
    vector<Room> rooms_; // 机房数组
    string filename_ = "rooms.txt"; // 机房信息文件名
    void readRooms(); // 从文件中读取机房信息
    void writeRooms(); // 将机房信息写入文件
};

void RoomManager::addRoom(Room room) {
    readRooms();
    if (rooms_.empty() || room.getId() > rooms_.back().getId()) {
        rooms_.push_back(room);
    } else {
        for (auto it = rooms_.begin(); it != rooms_.end(); it++) {
            if (it->getId() == room.getId()) {
                cout << "机房编号已存在,添加失败!" << endl;
                return;
            } else if (it->getId() > room.getId()) {
                rooms_.insert(it, room);
                break;
            }
        }
    }
    writeRooms();
}

void RoomManager::deleteRoom(int id) {
    readRooms();
    for (auto it = rooms_.begin(); it != rooms_.end(); it++) {
        if (it->getId() == id) {
            if (it->isUsed()) {
                cout << "机房正在使用中,删除失败!" << endl;
            } else {
                rooms_.erase(it);
                writeRooms();
                cout << "机房删除成功!" << endl;
            }
            return;
        }
    }
    cout << "未找到机房,删除失败!" << endl;
}

void RoomManager::queryRoom(int id) {
    readRooms();
    for (auto it = rooms_.begin(); it != rooms_.end(); it++) {
        if (it->getId() == id) {
            cout << "机房编号:" << it->getId() << endl;
            cout << "机房分类:" << getCategoryName(it->getCategory()) << endl;
            cout << "最大容纳人数:" << it->getCapacity() << endl;
            cout << "机房状态:" << (it->getIsUsed() ? "正在使用" : "未使用") << endl;
            return;
        }
    }
    cout << "未找到编号为" << id << "的机房" << endl;
}

vb啥的倒是有

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^