功能:设备管理系统应包含各种设备的全部信息,每台设备为一条记录,包括设备号、
设备名称、领用人、所属部门、数量、购买时间、价格等。
系统要求实现以下功能:
1、初步完成总体设计,搭好框架,确定人机对话界面,确定函数个数;
2、建立一个文件,将每条记录信息写入文件中并能显示于屏幕上;
3、能对文件进行补充、修订、删除,能统计所有设备的总价值;
4、进一步要求:完成设备按种类、按所属部门进行统计。
使用while循环就可以,然后创建结构体 Device,再进行编写函数
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 设备结构体
struct Device {
string id; // 设备号
string name; // 设备名称
string user; // 领用人
string department; // 所属部门
int quantity; // 数量
string purchaseTime; // 购买时间
double price; // 价格
};
// 文件名
const string FILE_NAME = "devices.txt";
// 设备列表
vector<Device> devices;
// 显示菜单
void showMenu() {
cout << "设备管理系统" << endl;
cout << "1. 显示设备列表" << endl;
cout << "2. 添加设备" << endl;
cout << "3. 修改设备" << endl;
cout << "4. 删除设备" << endl;
cout << "5. 统计设备总价值" << endl;
cout << "6. 退出" << endl;
cout << "请选择操作:";
}
// 读取设备列表
void readDevices() {
ifstream inFile(FILE_NAME);
if (!inFile) {
cout << "无法打开文件:" << FILE_NAME << endl;
return;
}
devices.clear();
string line;
while (getline(inFile, line)) {
Device device;
device.id = line;
getline(inFile, device.name);
getline(inFile, device.user);
getline(inFile, device.department);
inFile >> device.quantity;
inFile >> device.purchaseTime;
inFile >> device.price;
inFile.ignore();
devices.push_back(device);
}
inFile.close();
}
// 写入设备列表
void writeDevices() {
ofstream outFile(FILE_NAME);
if (!outFile) {
cout << "无法打开文件:" << FILE_NAME << endl;
return;
}
for (const Device& device : devices) {
outFile << device.id << endl;
outFile << device.name << endl;
outFile << device.user << endl;
outFile << device.department << endl;
outFile << device.quantity << endl;
outFile << device.purchaseTime << endl;
outFile << device.price << endl;
}
outFile.close();
}
// 显示设备列表
void showDevices() {
if (devices.empty()) {
cout << "设备列表为空" << endl;
return;
}
cout << "设备列表:" << endl;
for (const Device& device : devices) {
cout << "设备号:" << device.id << endl;
cout << "设备名称:" << device.name << endl;
cout << "领用人:" << device.user << endl;
cout << "所属部门:" << device.department << endl;
cout << "数量:" << device.quantity << endl;
cout << "购买时间:" << device.purchaseTime << endl;
cout << "价格:" << device.price << endl;
cout << endl;
}
}
// 添加设备
void addDevice() {
Device device;
cout << "请输入设备号:";
getline(cin, device.id);
cout << "请输入设备名称:";
getline(cin, device.name);
cout << "请输入领用人:";
getline(cin, device.user);
cout << "请输入所属部门:";
getline(cin, device.department);
cout << "请输入数量:";
cin >> device.quantity;
cout << "请输入购买时间:";
cin >> device.purchaseTime;
cout << "请输入价格:";
cin >> device.price;
cin.ignore();
devices.push_back(device);
writeDevices();
cout << "设备添加成功" << endl;
}
// 修改设备
void modifyDevice() {
if (devices.empty()) {
cout << "设备列表为空" << endl;
return;
}
string id;
cout << "请输入要修改的设备号:";
getline(cin, id);
for (Device& device : devices) {
if (device.id == id) {
cout << "请输入设备名称(原值:" << device.name << "):";
getline(cin, device.name);
cout << "请输入领用人(原值:" << device.user << "):";
getline(cin, device.user);
cout << "请输入所属部门(原值:" << device.department << "):";
getline(cin, device.department);
cout << "请输入数量(原值:" << device.quantity << "):";
cin >> device.quantity;
cout << "请输入购买时间(原值:" << device.purchaseTime << "):";
cin >> device.purchaseTime;
cout << "请输入价格(原值:" << device.price << "):";
cin >> device.price;
cin.ignore();
writeDevices();
cout << "设备修改成功" << endl;
return;
}
}
cout << "设备不存在" << endl;
}
// 删除设备
void deleteDevice() {
if (devices.empty()) {
cout << "设备列表为空" << endl;
return;
}
string id;
cout << "请输入要删除的设备号:";
getline(cin, id);
for (auto it = devices.begin(); it != devices.end(); ++it) {
if (it->id == id) {
devices.erase(it);
writeDevices();
cout << "设备删除成功" << endl;
return;
}
}
cout << "设备不存在" << endl;
}
// 统计设备总价值
void calculateTotalPrice() {
double totalPrice = 0;
for (const Device& device : devices) {
totalPrice += device.price * device.quantity;
}
cout << "设备总价值为:" << totalPrice << endl;
}
int main() {
readDevices();
while (true) {
showMenu();
int choice;
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
showDevices();
break;
case 2:
addDevice();
break;
case 3:
modifyDevice();
break;
case 4:
deleteDevice();
break;
case 5:
calculateTotalPrice();
break;
case 6:
return 0;
default:
cout << "无效的选择" << endl;
break;
}
cout << endl;
}
}
#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
#define ERROR 0
#define OK 1
#define MVNum 100
#define MAX_VERTEX_NUM 100
#define MAXQSIZE 100 //最大队列长度
bool visited[MAX_VERTEX_NUM]; //声明一个辅助指针visited,即数组
typedef int Status;
typedef char VerTexType; //假设顶点的数据类型为字符型
typedef int VerTexTypesqQueue; //队列中元素的类型
/*------图的邻接表存储表示------*/
typedef struct ArcNode //边结点
{
int adjvex; //该边所指向的顶点的位置
struct ArcNode* nextarc; //指向下一条边的指针
}ArcNode;
typedef struct VNode //顶点信息
{
VerTexType data;
ArcNode* firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct
{
AdjList vertices; //邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
//----队列的定义
typedef struct
{
int* base; //初始化的动态分配存储空间,注意队列中存储的是顶点的位置
int front; //头指针,若队列不空,指向队头元素
int rear; //尾指针,若队列不空,指向队尾元素的下一个位置
}sqQueue;
//队列的相关操作
void InitQueue(sqQueue& Q)
{ //构造一个空队列Q
Q.base = new VerTexTypesqQueue[MAXQSIZE];
if (!Q.base) exit(1); //存储分配失败
Q.front = Q.rear = 0;
} //InitQueue
void EnQueue(sqQueue& Q, int e)
{ //插入元素e为Q的新的队尾元素
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
} //EnQueue
bool QueueEmpty(sqQueue Q)
{ //判断是否为空队
if (Q.rear == Q.front)
return true;
return false;
} //QueueEmpty
void DeQueue(sqQueue& Q, int& u)
{ //队头元素出队并置为u
u = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
}
Status LocateVex(ALGraph G, VerTexType v) //求顶点在顶点数组中的位置
{
for (int i = 0;i < G.vexnum;++i)
if (G.vertices[i].data == v) //找到该顶点
return i; //返回顶点结点数组的下标
return -1;
}
Status CreateUDG(ALGraph& G) /*采用邻接表表示法创建有向图*/
{
VerTexType v1, v2; //
ArcNode* p1; //
cout << "请输入总顶点数,总边数,以空格隔开:";
cin >> G.vexnum >> G.arcnum; //输入顶点数和边数
cout << endl;
cout << "输入点的名称,如a" << endl;
for (int i = 0;i < G.vexnum;i++)
{
cout << "请输入第" << i + 1 << "个点的名称:";
cin >> G.vertices[i].data; //输入顶点的值
G.vertices[i].firstarc = NULL; //初始化表头结点的指针域为NULL
}
cout << endl;
cout << "输入边依附的顶点,如a,b" << endl;
for (int k = 0;k < G.arcnum;k++) //输入各边,构造邻接表
{
int i, j;
cout << "请输入第" << k + 1 << "条边依附的顶点:";
cin >> v1 >> v2; //输入一条边依附的两个顶点
i = LocateVex(G, v1); //v1在表结点的位置
j = LocateVex(G, v2); //v2在表结点的位置
p1 = new ArcNode; //生成一个新的边结点*p1
p1->adjvex = j; //邻接点序号为j
p1->nextarc = G.vertices[i].firstarc; //采用头插法插入边结点
G.vertices[i].firstarc = p1; //将新结点*p1插入到顶点vi的边表头部
}
return OK;
}
void DispGraphAdjList(ALGraph G) //邻接表的输出
{
ArcNode* p;
cout << endl;
cout << "有向图的邻接表如下:" << endl;
for (int i = 0;i < G.vexnum;++i) //循环表结点个数次
{
cout << G.vertices[i].data; //输出表结点的顶点信息
for (p = G.vertices[i].firstarc;p != NULL;p = p->nextarc)//从第一个边结点开始,依次输出所有的边结点地址域所对应的顶点信息
{
cout << setw(7) << G.vertices[p->adjvex].data;
}
cout << endl;
}
}
void DFS_AL(ALGraph G, int i) //邻接表存储的有向图的DFS
{
ArcNode* p; //声明一个ArcNode类型的指针p
int j; //记录下标的变量
cout << setw(7) << G.vertices[i].data; //从顶点vi开始,输出下标为i的顶点
visited[i] = 1; //顶点已访问,置为1
p = G.vertices[i].firstarc; //p指向v的边链表的第一个边结点
while (p != NULL)
{
j = p->adjvex; //j是p的邻接点下标
if (visited[j] == 0)
DFS_AL(G, j); //如果j未访问,则递归调用DFS_AL
p = p->nextarc; //p指向下一个边结点
}
}
void BFS_AL(ALGraph G, int v) //邻接表存储的有向图的BFS
{
sqQueue Q;
InitQueue(Q); //初始化队列Q
int u;
ArcNode* p; //声明一个ArcNode结点类型的指针p
for (int i = 0;i < G.vexnum;i++) //将所有的顶点的辅助值置为0
{
visited[i] = 0;
}
cout << setw(7) << G.vertices[v].data; //输出下标为v的表结点顶点信息
visited[v] = 1; //将下标为v的结点标记为已访问
EnQueue(Q, v); //将访问过的结点的下标入队
while (!QueueEmpty(Q)) //如果栈不为空,则进入循环
{
DeQueue(Q, u); //将队首的下标出队
p = G.vertices[u].firstarc; //找到队首下标位置所对应的结点
while (p != NULL)
{
if (visited[p->adjvex] == 0) //如果没有被访问过
{
cout << setw(7) << G.vertices[p->adjvex].data;//访问,输出顶点信息
visited[p->adjvex] = 1; //标记访问标记
EnQueue(Q, p->adjvex); //将访问过的结点的下标入队
}
p = p->nextarc; //指针往后移动,遍历下一个边结点
}
}
}
int main()
{
VerTexType v;
ALGraph G; //声明一个图G
if (CreateUDG(G)) //创建邻接表
cout << "有向图G创建完成!" << endl << endl;
DispGraphAdjList(G); //邻接表的输出
cout << "请输入遍历有向图的起始点:";
cin >> v; //输入遍历的起始点
cout << endl << "深度优先搜索遍历有向图结果:" << endl;
DFS_AL(G, LocateVex(G, v)); //DFS遍历有向图
cout << endl << "广度优先搜索遍历有向图结果:" << endl;
BFS_AL(G, LocateVex(G, v)); //BFS遍历有向图
return 0;
}