功能:设备管理系统应包含各种设备的全部信息,每台设备为一条记录,包括设备号、
设备名称、领用人、所属部门、数量、购买时间、价格等。
系统要求实现以下功能:
1、初步完成总体设计,搭好框架,确定人机对话界面,确定函数个数;
2、建立一个文件,将每条记录信息写入文件中并能显示于屏幕上;
3、能对文件进行补充、修订、删除,能统计所有设备的总价值;
4、进一步要求:完成设备按种类、按所属部门进行统计。
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Device {
int deviceNumber;
string deviceName;
string user;
string department;
int quantity;
string purchaseDate;
double price;
};
void displayMenu();
void addDevice(vector<Device>& devices);
void displayDevices(const vector<Device>& devices);
void updateDevice(vector<Device>& devices);
void deleteDevice(vector<Device>& devices);
double calculateTotalValue(const vector<Device>& devices);
void calculateStatisticsByCategory(const vector<Device>& devices);
void calculateStatisticsByDepartment(const vector<Device>& devices);
int main() {
vector<Device> devices;
int choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addDevice(devices);
break;
case 2:
displayDevices(devices);
break;
case 3:
updateDevice(devices);
break;
case 4:
deleteDevice(devices);
break;
case 5:
cout << "Total value of all devices: $" << calculateTotalValue(devices) << endl;
break;
case 6:
calculateStatisticsByCategory(devices);
break;
case 7:
calculateStatisticsByDepartment(devices);
break;
case 8:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
cout << endl;
} while (choice != 8);
return 0;
}
void displayMenu() {
cout << "========== Device Management System ==========" << endl;
cout << "1. Add device" << endl;
cout << "2. Display devices" << endl;
cout << "3. Update device" << endl;
cout << "4. Delete device" << endl;
cout << "5. Calculate total value of all devices" << endl;
cout << "6. Calculate statistics by category" << endl;
cout << "7. Calculate statistics by department" << endl;
cout << "8. Exit" << endl;
}
void addDevice(vector<Device>& devices) {
Device newDevice;
cout << "Enter device number: ";
cin >> newDevice.deviceNumber;
cin.ignore(); // Ignore the newline character
cout << "Enter device name: ";
getline(cin, newDevice.deviceName);
cout << "Enter user: ";
getline(cin, newDevice.user);
cout << "Enter department: ";
getline(cin, newDevice.department);
cout << "Enter quantity: ";
cin >> newDevice.quantity;
cout << "Enter purchase date: ";
cin >> newDevice.purchaseDate;
cout << "Enter price: ";
cin >> newDevice.price;
devices.push_back(newDevice);
cout << "Device added successfully." << endl;
}
void displayDevices(const vector<Device>& devices) {
if (devices.empty()) {
cout << "No devices found." << endl;
return;
}
cout << "========== Devices ==========" << endl;
for (const auto& device : devices) {
cout << "Device Number: " << device.deviceNumber << endl;
cout << "Device Name: " << device.deviceName << endl;
cout << "User: " << device.user << endl;
cout << "Department: " << device.department << endl;
cout << "Quantity: " << device.quantity << endl;
cout << "Purchase Date: " << device.purchaseDate << endl;
cout << "Price: $" << device.price << endl;
cout << "-----------------------------" << endl;
}
}
void updateDevice(vector<Device>& devices) {
int deviceNumber;
cout << "Enter device number to update: ";
cin >> deviceNumber;
auto device = find_if(devices.begin(), devices.end(), [deviceNumber](const Device& d) {
return d.deviceNumber == deviceNumber;
});
if (device == devices.end()) {
cout << "Device not found with the given device number." << endl;
return;
}
cout << "Enter updated device name: ";
cin.ignore(); // Ignore the newline character
getline(cin, device->deviceName);
cout << "Enter updated user: ";
getline(cin, device->user);
cout << "Enter updated department: ";
getline(cin, device->department);
cout << "Enter updated quantity: ";
cin >> device->quantity;
cout << "Enter updated purchase date: ";
cin >> device->purchaseDate;
cout << "Enter updated price: ";
cin >> device->price;
cout << "Device updated successfully." << endl;
}
void deleteDevice(vector<Device>& devices) {
int deviceNumber;
cout << "Enter device number to delete: ";
cin >> deviceNumber;
auto device = find_if(devices.begin(), devices.end(), [deviceNumber](const Device& d) {
return d.deviceNumber == deviceNumber;
});
if (device == devices.end()) {
cout << "Device not found with the given device number." << endl;
return;
}
devices.erase(device);
cout << "Device deleted successfully." << endl;
}
double calculateTotalValue(const vector<Device>& devices) {
double totalValue = 0.0;
for (const auto& device : devices) {
totalValue += device.quantity * device.price;
}
return totalValue;
}
void calculateStatisticsByCategory(const vector<Device>& devices) {
// Implement your logic to calculate statistics by category
cout << "Statistics by category" << endl;
}
void calculateStatisticsByDepartment(const vector<Device>& devices) {
// Implement your logic to calculate statistics by department
cout << "Statistics by department" << endl;
}
桌面应用吗?这个主要的应该是数据库设计,把基本对象定义好,SQL语句处理自己的各种数据,再反馈到界面上。
#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;
}