公司现有以下设备:普通电视机、DVD、带DVD 的电视机,带DVD 的电视机的售价为普通电视机和DVD 单价之和的 80%,要求对所有的库存设备都能按照品名进行显示、查找、增加和删除、保存的功能。用c语言编程 不使用链表和指针
时间: 2024-02-06 14:03:59 浏览: 25
好的,我了解了你的问题。针对你的需求,我可以提供以下代码实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_DEVICES 100 // 最大库存设备数
#define MAX_NAME_LEN 50 // 设备名称最大长度
// 设备结构体
struct Device {
char name[MAX_NAME_LEN];
int quantity;
};
// 库存设备数组
struct Device devices[MAX_DEVICES];
// 当前库存设备数
int num_devices = 0;
// 显示所有设备
void show_devices() {
printf("当前库存设备如下:\n");
for (int i = 0; i < num_devices; i++) {
printf("%s\t%d\n", devices[i].name, devices[i].quantity);
}
printf("\n");
}
// 查找设备
int find_device(const char *name) {
for (int i = 0; i < num_devices; i++) {
if (strcmp(devices[i].name, name) == 0) {
return i;
}
}
return -1;
}
// 增加设备
void add_device(const char *name, int quantity) {
int index = find_device(name);
if (index == -1) {
// 设备不存在,添加新设备
if (num_devices >= MAX_DEVICES) {
printf("库存设备已满,无法添加新设备!\n");
return;
}
strcpy(devices[num_devices].name, name);
devices[num_devices].quantity = quantity;
num_devices++;
} else {
// 设备已存在,增加数量
devices[index].quantity += quantity;
}
printf("设备添加成功!\n");
}
// 删除设备
void delete_device(const char *name) {
int index = find_device(name);
if (index == -1) {
printf("设备不存在,无法删除!\n");
return;
}
// 将最后一个设备覆盖要删除的设备
devices[index] = devices[num_devices-1];
num_devices--;
printf("设备删除成功!\n");
}
// 保存设备信息到文件
void save_devices() {
FILE *fp = fopen("devices.dat", "wb");
if (fp == NULL) {
printf("打开文件失败!\n");
return;
}
fwrite(devices, sizeof(struct Device), num_devices, fp);
fclose(fp);
printf("设备信息已保存到文件!\n");
}
// 从文件中读取设备信息
void load_devices() {
FILE *fp = fopen("devices.dat", "rb");
if (fp == NULL) {
printf("打开文件失败!\n");
return;
}
num_devices = fread(devices, sizeof(struct Device), MAX_DEVICES, fp);
fclose(fp);
printf("设备信息已从文件中读取!\n");
}
int main() {
int choice, quantity;
char name[MAX_NAME_LEN];
load_devices(); // 从文件中读取设备信息
do {
printf("请选择操作:\n");
printf("1. 显示所有设备\n");
printf("2. 查找设备\n");
printf("3. 增加设备\n");
printf("4. 删除设备\n");
printf("5. 保存设备信息到文件\n");
printf("6. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
show_devices();
break;
case 2:
printf("请输入设备名称:");
scanf("%s", name);
int index = find_device(name);
if (index == -1) {
printf("设备不存在!\n");
} else {
printf("设备数量为:%d\n", devices[index].quantity);
}
break;
case 3:
printf("请输入设备名称:");
scanf("%s", name);
printf("请输入设备数量:");
scanf("%d", &quantity);
add_device(name, quantity);
break;
case 4:
printf("请输入设备名称:");
scanf("%s", name);
delete_device(name);
break;
case 5:
save_devices();
break;
case 6:
break;
default:
printf("无效选择,请重新输入!\n");
break;
}
} while (choice != 6);
save_devices(); // 退出程序前保存设备信息到文件
return 0;
}
```
该程序使用结构体数组存储库存设备信息,提供显示所有设备、查找设备、增加设备、删除设备、保存设备信息到文件等功能。在增加设备和删除设备时,会先查找设备是否已存在。在程序启动时,会从文件中读取设备信息;在程序退出前,会将设备信息保存到文件中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)