C语言实现设备管理系统
1. 设备管理系统需求分析
- 管理员可以添加设备信息
- 管理员可以删除设备信息
- 管理员可以修改设备信息
- 系统可以显示设备信息
2. 设备管理系统设计
2.1 设备信息结构体
typedef struct device {
int id;
char name[20];
char type[20];
int quantity;
float price;
} Device;
2.2 设备信息存储
使用单链表存储所有设备信息。
typedef struct node {
Device data;
struct node *next;
} Node;
使用链表头指针管理链表。
Node *head = NULL;
2.3 设备信息操作函数
实现以下函数,完成具体的设备信息操作。
添加设备信息
void addDevice() {
Device tmp;
printf("Please enter device ID: ");
scanf("%d", &(tmp.id));
printf("Please enter device name: ");
scanf("%s", tmp.name);
printf("Please enter device type: ");
scanf("%s", tmp.type);
printf("Please enter device quantity: ");
scanf("%d", &(tmp.quantity));
printf("Please enter device price: ");
scanf("%f", &(tmp.price));
Node *p = (Node*)malloc(sizeof(Node));
p->data = tmp;
p->next = head;
head = p;
printf("Device added successfully!\n");
}
删除设备信息
void deleteDevice(int id) {
Node *p = head;
Node *q = NULL;
while (p != NULL && p->data.id != id) {
q = p;
p = p->next;
}
if (p == NULL) {
printf("Device not found!\n");
return;
}
if (q == NULL) {
head = p->next;
} else {
q->next = p->next;
}
free(p);
printf("Device deleted successfully!\n");
}
修改设备信息
void modifyDevice(int id) {
Node *p = head;
while (p != NULL && p->data.id != id) {
p = p->next;
}
if (p == NULL) {
printf("Device not found!\n");
return;
}
printf("Please enter new device name (or press enter to skip): ");
char name[20];
scanf("%s", name);
if (strlen(name) != 0) {
strcpy(p->data.name, name);
}
printf("Please enter new device type (or press enter to skip): ");
char type[20];
scanf("%s", type);
if (strlen(type) != 0) {
strcpy(p->data.type, type);
}
printf("Please enter new device quantity (or press -1 to skip): ");
int quantity;
scanf("%d", &quantity);
if (quantity != -1) {
p->data.quantity = quantity;
}
printf("Please enter new device price (or press -1 to skip): ");
float price;
scanf("%f", &price);
if (price != -1) {
p->data.price = price;
}
printf("Device modified successfully!\n");
}
显示设备信息
void showDevice() {
Node *p = head;
while (p != NULL) {
printf("Device ID: %d\nDevice Name: %s\nDevice Type: %s\nDevice Quantity: %d\nDevice Price: %f\n\n", p->data.id, p->data.name, p->data.type, p->data.quantity, p->data.price);
p = p->next;
}
}
3. 设备管理系统示例
3.1 添加设备信息
addDevice();
addDevice();
3.2 显示设备信息
showDevice();
输出:
Device ID: 2
Device Name: Computer
Device Type: Digital
Device Quantity: 2
Device Price: 2000.000000
Device ID: 1
Device Name: Printer
Device Type: Office
Device Quantity: 5
Device Price: 500.000000
3.3 修改设备信息
modifyDevice(2);
输出:
Please enter new device name (or press enter to skip): Laptop
Please enter new device type (or press enter to skip):
Please enter new device quantity (or press -1 to skip):
Please enter new device price (or press -1 to skip): 2500.00
Device modified successfully!
3.4 删除设备信息
deleteDevice(1);
输出:
Device deleted successfully!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言实现设备管理系统 - Python技术站