用C++写一个设备管理系统增删改查
时间: 2023-12-29 08:24:39 浏览: 29
好的,以下是一个简单的设备管理系统的增删改查功能的C++代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 设备类
class Device {
public:
Device(int id, string name, string type) : id(id), name(name), type(type) {}
int getId() const { return id; }
string getName() const { return name; }
string getType() const { return type; }
private:
int id;
string name;
string type;
};
// 设备管理系统类
class DeviceManager {
public:
// 添加设备
void addDevice() {
cout << "请输入设备ID:";
int id;
cin >> id;
cout << "请输入设备名称:";
string name;
cin >> name;
cout << "请输入设备类型:";
string type;
cin >> type;
Device device(id, name, type);
devices.push_back(device);
cout << "添加设备成功!" << endl;
}
// 删除设备
void deleteDevice() {
cout << "请输入要删除的设备ID:";
int id;
cin >> id;
for (auto it = devices.begin(); it != devices.end(); ++it) {
if (it->getId() == id) {
devices.erase(it);
cout << "删除设备成功!" << endl;
return;
}
}
cout << "未找到设备ID为" << id << "的设备!" << endl;
}
// 修改设备
void updateDevice() {
cout << "请输入要修改的设备ID:";
int id;
cin >> id;
for (auto it = devices.begin(); it != devices.end(); ++it) {
if (it->getId() == id) {
cout << "请输入新的设备名称:";
string name;
cin >> name;
cout << "请输入新的设备类型:";
string type;
cin >> type;
it->name = name;
it->type = type;
cout << "修改设备成功!" << endl;
return;
}
}
cout << "未找到设备ID为" << id << "的设备!" << endl;
}
// 查询设备
void searchDevice() {
cout << "请输入要查询的设备ID:";
int id;
cin >> id;
for (auto it = devices.begin(); it != devices.end(); ++it) {
if (it->getId() == id) {
cout << "设备ID:" << it->getId() << endl;
cout << "设备名称:" << it->getName() << endl;
cout << "设备类型:" << it->getType() << endl;
return;
}
}
cout << "未找到设备ID为" << id << "的设备!" << endl;
}
private:
vector<Device> devices; // 设备列表
};
int main() {
DeviceManager manager;
while (true) {
cout << "请选择操作:1.添加设备 2.删除设备 3.修改设备 4.查询设备 5.退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
manager.addDevice();
break;
case 2:
manager.deleteDevice();
break;
case 3:
manager.updateDevice();
break;
case 4:
manager.searchDevice();
break;
case 5:
return 0;
default:
cout << "无效的选择!" << endl;
}
}
}
```
该示例代码实现了一个简单的设备管理系统,包括添加设备、删除设备、修改设备和查询设备等功能。用户可以根据提示在控制台输入相应的操作,然后进行相应的操作。