功能:设备管理系统应包含各种设备的全部信息,每台设备为一条记录,包括设备号、
设备名称、领用人、所属部门、数量、购买时间、价格等。
系统要求实现以下功能:
1、搭建人机对话界面
2、建立一个文件,将每条记录信息写入文件中并能显示于屏幕上;
3、能对文件进行补充、修订、删除,能统计所有设备的总价值;
4、进一步要求:完成设备按种类、按所属部门进行统计。
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Device {
int deviceId;
string deviceName;
string user;
string department;
int quantity;
string purchaseDate;
float price;
};
// 存储所有设备信息的容器
vector<Device> devices;
// 添加设备信息到容器
void addDevice() {
Device device;
cout << "请输入设备号:";
cin >> device.deviceId;
cout << "请输入设备名称:";
cin >> device.deviceName;
cout << "请输入领用人:";
cin >> device.user;
cout << "请输入所属部门:";
cin >> device.department;
cout << "请输入数量:";
cin >> device.quantity;
cout << "请输入购买时间:";
cin >> device.purchaseDate;
cout << "请输入价格:";
cin >> device.price;
devices.push_back(device);
cout << "设备信息已添加!" << endl;
}
// 显示所有设备信息
void displayDevices() {
cout << "设备信息如下:" << endl;
for (const auto& device : devices) {
cout << "设备号:" << device.deviceId << endl;
cout << "设备名称:" << device.deviceName << endl;
cout << "领用人:" << device.user << endl;
cout << "所属部门:" << device.department << endl;
cout << "数量:" << device.quantity << endl;
cout << "购买时间:" << device.purchaseDate << endl;
cout << "价格:" << device.price << endl;
cout << "------------------------" << endl;
}
}
// 将设备信息写入文件
void writeToFile() {
ofstream file("devices.txt", ios::trunc);
if (!file) {
cerr << "无法打开文件!" << endl;
return;
}
for (const auto& device : devices) {
file << "设备号:" << device.deviceId << endl;
file << "设备名称:" << device.deviceName << endl;
file << "领用人:" << device.user << endl;
file << "所属部门:" << device.department << endl;
file << "数量:" << device.quantity << endl;
file << "购买时间:" << device.purchaseDate << endl;
file << "价格:" << device.price << endl;
file << "------------------------" << endl;
}
file.close();
cout << "设备信息已写入文件!" << endl;
}
// 从文件中读取设备信息并显示
void readFromFile() {
ifstream file("devices.txt");
if (!file) {
cerr << "无法打开文件!" << endl;
return;
}
string line;
while (getline(file, line)) {
cout << line << endl;
}
file.close();
}
// 根据设备号删除设备信息
void deleteDevice(int deviceId) {
auto it = remove_if(devices.begin(), devices.end(), [deviceId](const Device& device) {
return device.deviceId == deviceId;
});
if (it != devices.end()) {
devices.erase(it, devices.end());
cout << "设备信息已删除!" << endl;
} else {
cout << "找不到设备号为 " << deviceId << " 的设备!" << endl;
}
}
// 统计所有设备的总价值
float calculateTotalValue() {
float totalValue = 0.0;
for (const auto& device : devices) {
totalValue += device.price * device.quantity;
}
return totalValue;
}
// 统计设备按种类进行统计
void countByDeviceName() {
int count = 0;
string deviceName;
cout << "请输入设备名称:";
cin >> deviceName;
for (const auto& device : devices) {
if (device.deviceName == deviceName) {
count++;
}
}
cout << "设备名称为 " << deviceName << " 的设备共有 " << count << " 台。" << endl;
}
// 统计设备按所属部门进行统计
void countByDepartment() {
int count = 0;
string department;
cout << "请输入所属部门:";
cin >> department;
for (const auto& device : devices) {
if (device.department == department) {
count++;
}
}
cout << "所属部门为 " << department << " 的设备共有 " << count << " 台。" << endl;
}
int main() {
while (true) {
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 << "0. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addDevice();
break;
case 2:
displayDevices();
break;
case 3:
writeToFile();
break;
case 4:
readFromFile();
break;
case 5:
int deviceId;
cout << "请输入要删除的设备号:";
cin >> deviceId;
deleteDevice(deviceId);
break;
case 6:
cout << "所有设备的总价值为:" << calculateTotalValue() << endl;
break;
case 7:
countByDeviceName();
break;
case 8:
countByDepartment();
break;
case 0:
cout << "感谢使用,再见!" << endl;
return 0;
default:
cout << "无效选择,请重新输入!" << endl;
}
cout << endl;
}
return 0;
}
平均时间复杂度:O(nlogn)
空间复杂度:O(logn)
最差的情况就是每一次取到的元素就是数组中最小/最大的,退化到冒泡排序,最差的时间复杂度:O(n²),空间复杂度:O(n)
非稳定排序
#include <iostream>
#include <vector>
using namespace std;
void quick_sort(vector<int> &vec,int left,int right)
{
if(left >= right)
return;
int low = left;
int high = right;
int key = vec[low];
while(low < high)
{
while(low < high && vec[high] >= key)
high--;
if(low < high)
vec[low++] = vec[high];
while(low < high && vec[low] < key)
low++;
if(low < high)
vec[high--] = vec[low];
}
vec[high] = key;
quick_sort(vec,left,high - 1);
quick_sort(vec,high + 1,right);
}
int main()
{
int num;
cin >> num;
vector<int> vec;
int temp;
for(int i = 0; i < num; ++i)
{
cin >> temp;
vec.push_back(temp);
}
quick_sort(vec,0,num - 1);
for(vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
return 0;
}