每一条设备信息:设备号,设备名称,领用人,所属部门,数量,购买时间,价格
1、初步完成总体设计,搭好框架,确定人机对话界面,确定函数个数;
2、建立一个文件,将每条记录信息写入文件中并能显示于屏幕上;
3、能对文件进行补充、修订、删除,能统计所有设备的总价值;
4、进一步要求:完成设备按种类、按所属部门进行统计。
人机对话界面:
1.添加设备信息
2.显示设备信息
3.修改设备信息
4.删除设备信息
5.计算设备总金额
6.按照设备种类进行分类
7.按照设备所属部门进行分类
要求:适配于devc++环境,采用c++语言风格进行设计,采用类
c++风格 采用类是吧
#include<bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Device {
public:
string deviceNumber;
string deviceName;
string user;
string department;
int quantity;
string purchaseDate;
double price;
Device() {} // 默认构造函数
// 构造函数
Device(string number, string name, string u, string dept, int qty, string date, double p)
: deviceNumber(number), deviceName(name), user(u), department(dept), quantity(qty), purchaseDate(date), price(p) {}
// 显示设备信息
void display() {
cout << "设备号: " << deviceNumber << endl;
cout << "设备名称: " << deviceName << endl;
cout << "领用人: " << user << endl;
cout << "所属部门: " << department << endl;
cout << "数量: " << quantity << endl;
cout << "购买时间: " << purchaseDate << endl;
cout << "价格: " << price << endl;
}
// 获取设备价格
double getPrice() {
return price;
}
// 获取设备所属部门
string getDepartment() {
return department;
}
};
class DeviceManagementSystem
{
vector<Device> devices;
public:
// 添加设备信息
void addDevice(Device device) {
devices.push_back(device);
}
// 显示所有设备信息
void displayDevices() {
for(int i=0;i<devices.size();i++)
{
devices[i].display();
cout<<endl;
}
}
// 修改设备信息
void modifyDevice(string number, string newName, string newUser, string newDept, int newQty, string newDate, double newPrice) {
for(int i=0;i<devices.size();i++)
{
if (devices[i].deviceNumber == number) {
devices[i].deviceName = newName;
devices[i].user = newUser;
devices[i].department = newDept;
devices[i].quantity = newQty;
devices[i].purchaseDate = newDate;
devices[i].price = newPrice;
break;
}
}
}
// 删除设备信息
void deleteDevice(string number) {
for(int i=0;i<devices.size();i++)
{
if(devices[i].deviceNumber==number)
{
devices.erase(devices.begin()+i+1);
}
}
}
// 计算设备总金额
double calculateTotalValue() {
double totalValue = 0.0;
for(int i=0;i<devices.size();i++)
{
totalValue += devices[i].getPrice() * devices[i].quantity;
}
return totalValue;
}
// 按设备种类进行分类统计
void groupByDeviceName() {
// TODO: 实现按设备种类进行分类统计
cout << "按设备种类进行分类" << endl;
}
// 按所属部门进行分类统计
void groupByDepartment() {
// TODO: 实现按所属部门进行分类统计
cout << "按所属部门进行分类" << endl;
}
// 将设备信息保存到文件
void saveToFile(string filename) {
ofstream outfile("devices.txt");
if (outfile.is_open()) {
for(int i=0;i<devices.size();i++)
{
outfile << devices[i].deviceNumber << "," << devices[i].deviceName << "," << devices[i].user << ","
<< devices[i].department << "," << devices[i].quantity << "," << devices[i].purchaseDate << ","
<< devices[i].price << endl;
}
outfile.close();
}
else {
cout << "无法打开文件" << filename << "进行保存" << endl;
}
}
// 从文件中加载设备信息
void loadFromFile(string filename) {
ifstream file("devices.txt");
if (file.is_open()) {
devices.clear();
string line;
while (getline(file, line)) {
Device device;
size_t pos = 0;
string token;
// 按逗号分隔读取设备信息
while ((pos = line.find(',')) != string::npos) {
token = line.substr(0, pos);
line.erase(0, pos + 1);
// 根据数据顺序填充设备对象
if (device.deviceNumber.empty()) {
device.deviceNumber = token;
}
else if (device.deviceName.empty()) {
device.deviceName = token;
}
else if (device.user.empty()) {
device.user = token;
}
else if (device.department.empty()) {
device.department = token;
}
else if (device.quantity == 0) {
stringstream ss(token);
ss>>device.quantity;
}
else if (device.purchaseDate.empty()) {
device.purchaseDate = token;
}
else if (device.price == 0.0) {
stringstream ss(token);
ss>>device.price;
}
}
devices.push_back(device);
}
file.close();
cout << "设备信息已从文件加载。" << endl;
}
else {
cout << "无法打开文件。" << endl;
}
}
};
int main() {
DeviceManagementSystem dms;
dms.loadFromFile("devices.txt"); // 从文件中加载设备信息
int choice;
do {
cout << "人机对话界面:" << endl;
cout << "请选择操作:" << endl;
cout << "1. 添加设备信息" << endl;
cout << "2. 显示设备信息" << endl;
cout << "3. 修改设备信息" << endl;
cout << "4. 删除设备信息" << endl;
cout << "5. 计算设备总金额" << endl;
cout << "6. 按照设备种类进行分类" << endl;
cout << "7. 按照设备所属部门进行分类" << endl;
cout << "0. 退出系统" << endl;
cout << "请输入选项:";
cin >> choice;
switch (choice) {
case 1: {
string number, name, user, dept, date;
int quantity;
double price;
cout << "请输入设备号:";
cin >> number;
cout << "请输入设备名称:";
cin >> name;
cout << "请输入领用人:";
cin >> user;
cout << "请输入所属部门:";
cin >> dept;
cout << "请输入数量:";
cin >> quantity;
cout << "请输入购买时间:";
cin >> date;
cout << "请输入价格:";
cin >> price;
dms.addDevice(Device(number, name, user, dept, quantity, date, price));
cout << "设备信息已添加" << endl;
break;
}
case 2:
dms.displayDevices();
break;
case 3: {
string number, newName, newUser, newDept, newDate;
int newQty;
double newPrice;
cout << "请输入要修改的设备号:";
cin >> number;
cout << "请输入新的设备名称:";
cin >> newName;
cout << "请输入新的领用人:";
cin >> newUser;
cout << "请输入新的所属部门:";
cin >> newDept;
cout << "请输入新的数量:";
cin >> newQty;
cout << "请输入新的购买时间:";
cin >> newDate;
cout << "请输入新的价格:";
cin >> newPrice;
dms.modifyDevice(number, newName, newUser, newDept, newQty, newDate, newPrice);
cout << "设备信息已修改" << endl;
break;
}
case 4: {
string number;
cout << "请输入要删除的设备号:";
cin >> number;
dms.deleteDevice(number);
cout << "设备信息已删除" << endl;
break;
}
case 5:
cout << "设备总金额为:" << dms.calculateTotalValue() << endl;
break;
case 6:
dms.groupByDeviceName();
break;
case 7:
dms.groupByDepartment();
break;
case 0:
dms.saveToFile("devices.txt"); // 将设备信息保存到文件
cout << "谢谢使用,再见!" << endl;
break;
default:
cout << "无效选项,请重新输入" << endl;
break;
}
cout << endl;
} while (choice != 0);
return 0;
}
昨天刚好写了一篇,可以来看看:https://peakchen.blog.csdn.net/article/details/131400419?spm=1001.2014.3001.5502
有现成的,可以借鉴下
使用C/C++制作信息管理系统(demo)