原始问题:
c语言实验室设备管理系统代码
由于设备管理系统的代码比较复杂,需要考虑到很多细节问题,这里提供一个简单的C语言设备管理系统代码,仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DEVICES 100 // 最大设备数量
// 设备结构体
typedef struct {
char name[20]; // 设备名称
char type[20]; // 设备类型
int quantity; // 设备数量
} Device;
Device devices[MAX_DEVICES]; // 设备数组
int num_devices = 0; // 设备数量
// 添加设备
void add_device() {
if (num_devices >= MAX_DEVICES) {
printf("设备数量已达到上限,无法添加新设备!
");
return;
}
Device new_device;
printf("请输入设备名称:");
scanf("%s", new_device.name);
printf("请输入设备类型:");
scanf("%s", new_device.type);
printf("请输入设备数量:");
scanf("%d", &new_device.quantity);
devices[num_devices++] = new_device;
printf("设备添加成功!
");
}
// 删除设备
void delete_device() {
char name[20];
printf("请输入要删除的设备名称:");
scanf("%s", name);
int i;
for (i = 0; i < num_devices; i++) {
if (strcmp(devices[i].name, name) == 0) {
// 找到要删除的设备
int j;
for (j = i; j < num_devices - 1; j++) {
devices[j] = devices[j + 1];
}
num_devices--;
printf("设备删除成功!
");
return;
}
}
printf("未找到要删除的设备!
");
}
// 修改设备
void modify_device() {
char name[20];
printf("请输入要修改的设备名称:");
scanf("%s", name);
int i;
for (i = 0; i < num_devices; i++) {
if (strcmp(devices[i].name, name) == 0) {
// 找到要修改的设备
printf("请输入新的设备名称:");
scanf("%s", devices[i].name);
printf("请输入新的设备类型:");
scanf("%s", devices[i].type);
printf("请输入新的设备数量:");
scanf("%d", &devices[i].quantity);
printf("设备修改成功!
");
return;
}
}
printf("未找到要修改的设备!
");
}
// 查询设备
void query_device() {
char name[20];
printf("请输入要查询的设备名称:");
scanf("%s", name);
int i;
for (i = 0; i < num_devices; i++) {
if (strcmp(devices[i].name, name) == 0) {
// 找到要查询的设备
printf("设备名称:%s
", devices[i].name);
printf("设备类型:%s
", devices[i].type);
printf("设备数量:%d
", devices[i].quantity);
return;
}
}
printf("未找到要查询的设备!
");
}
// 显示所有设备
void show_all_devices() {
int i;
for (i = 0; i < num_devices; i++) {
printf("设备名称:%s
", devices[i].name);
printf("设备类型:%s
", devices[i].type);
printf("设备数量:%d
", devices[i].quantity);
printf("
");
}
}
// 主函数
int main() {
int choice;
while (1) {
printf("设备管理系统
");
printf("1. 添加设备
");
printf("2. 删除设备
");
printf("3. 修改设备
");
printf("4. 查询设备
");
printf("5. 显示所有设备
");
printf("6. 退出
");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_device();
break;
case 2:
delete_device();
break;
case 3:
modify_device();
break;
case 4:
query_device();
break;
case 5:
show_all_devices();
break;
case 6:
printf("谢谢使用!
");
exit(0);
default:
printf("无效的操作!
");
break;
}
}
return 0;
}这个设备管理系统代码实现了添加设备、删除设备、修改设备、查询设备和显示所有设备等功能。但是这个代码还有很多可以改进的地方,比如可以添加文件读写功能,可以添加设备借还功能等等。
Prev:学前儿童卫生与保育摘要