用c语言写一个实验室设备管理系统
时间: 2024-05-16 07:13:49 浏览: 17
好的,以下是一个简单的实验室设备管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_DEVICES 100 // 最大设备数量
struct Device {
int id; // 设备编号
char name[20]; // 设备名称
int count; // 设备数量
};
struct Device devices[MAX_DEVICES]; // 存储设备的数组
int num_devices = 0; // 当前设备数量
// 添加设备
void add_device() {
if (num_devices >= MAX_DEVICES) {
printf("设备数量已达上限,无法添加!\n");
return;
}
struct Device device;
printf("请输入设备编号:");
scanf("%d", &device.id);
printf("请输入设备名称:");
scanf("%s", device.name);
printf("请输入设备数量:");
scanf("%d", &device.count);
devices[num_devices++] = device;
printf("设备添加成功!\n");
}
// 查找设备
void search_device() {
int id;
printf("请输入设备编号:");
scanf("%d", &id);
for (int i = 0; i < num_devices; i++) {
if (devices[i].id == id) {
printf("设备名称:%s,设备数量:%d\n", devices[i].name, devices[i].count);
return;
}
}
printf("没有找到设备!\n");
}
// 显示所有设备
void show_devices() {
printf("设备编号\t设备名称\t设备数量\n");
for (int i = 0; i < num_devices; i++) {
printf("%d\t\t%s\t\t%d\n", devices[i].id, devices[i].name, devices[i].count);
}
}
int main() {
int choice;
do {
printf("\n");
printf("实验室设备管理系统\n");
printf("--------------------\n");
printf("1. 添加设备\n");
printf("2. 查找设备\n");
printf("3. 显示所有设备\n");
printf("0. 退出\n");
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_device();
break;
case 2:
search_device();
break;
case 3:
show_devices();
break;
case 0:
printf("谢谢使用!\n");
break;
default:
printf("无效选项!\n");
break;
}
} while (choice != 0);
return 0;
}
```
这个程序使用了结构体来存储设备的信息,包括设备编号、设备名称和设备数量。在主函数中,使用一个循环来不断显示菜单,根据用户的选择执行相应的操作。用户可以选择添加设备、查找设备、显示所有设备或退出程序。在添加设备时,程序会检查设备数量是否已达到上限,并让用户输入设备信息。在查找设备时,程序会让用户输入设备编号,然后遍历所有设备来查找该设备。在显示所有设备时,程序会逐个打印所有设备的信息。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)