用c语言编写一个超市食品管理系统代码,功能包括食品录入,食品查询,食品修改,食品删除,食品排序,食品分类,库存统计,销售统计,食品信息保存,食品信息读取,不要用指针函数
时间: 2024-06-15 14:04:06 浏览: 15
下面是一个用C语言编写的超市食品管理系统的代码示例,实现了食品录入、查询、修改、删除、排序、分类、库存统计、销售统计、信息保存和读取的功能。该示例没有使用指针函数。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FOODS 100
typedef struct {
char name[50];
int quantity;
float price;
} Food;
Food foods[MAX_FOODS];
int foodCount = 0;
void addFood() {
if (foodCount >= MAX_FOODS) {
printf("超过最大食品数量限制!\n");
return;
}
printf("请输入食品名称:");
scanf("%s", foods[foodCount].name);
printf("请输入食品数量:");
scanf("%d", &foods[foodCount].quantity);
printf("请输入食品价格:");
scanf("%f", &foods[foodCount].price);
foodCount++;
printf("食品添加成功!\n");
}
void searchFood() {
char name[50];
printf("请输入要查询的食品名称:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < foodCount; i++) {
if (strcmp(foods[i].name, name) == 0) {
printf("食品名称:%s\n", foods[i].name);
printf("食品数量:%d\n", foods[i].quantity);
printf("食品价格:%.2f\n", foods[i].price);
found = 1;
break;
}
}
if (!found) {
printf("未找到该食品!\n");
}
}
void updateFood() {
char name[50];
printf("请输入要修改的食品名称:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < foodCount; i++) {
if (strcmp(foods[i].name, name) == 0) {
printf("请输入新的食品数量:");
scanf("%d", &foods[i].quantity);
printf("请输入新的食品价格:");
scanf("%f", &foods[i].price);
found = 1;
printf("食品修改成功!\n");
break;
}
}
if (!found) {
printf("未找到该食品!\n");
}
}
void deleteFood() {
char name[50];
printf("请输入要删除的食品名称:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < foodCount; i++) {
if (strcmp(foods[i].name, name) == 0) {
for (int j = i; j < foodCount - 1; j++) {
foods[j] = foods[j + 1];
}
foodCount--;
found = 1;
printf("食品删除成功!\n");
break;
}
}
if (!found) {
printf("未找到该食品!\n");
}
}
void sortFoods() {
for (int i = 0; i < foodCount - 1; i++) {
for (int j = 0; j < foodCount - i - 1; j++) {
if (foods[j].price > foods[j + 1].price) {
Food temp = foods[j];
foods[j] = foods[j + 1];
foods[j + 1] = temp;
}
}
}
printf("食品排序成功!\n");
}
void classifyFoods() {
// TODO: 实现食品分类功能
printf("食品分类功能暂未实现!\n");
}
void stockStatistics() {
int totalQuantity = 0;
float totalPrice = 0;
for (int i = 0; i < foodCount; i++) {
totalQuantity += foods[i].quantity;
totalPrice += foods[i].price * foods[i].quantity;
}
printf("库存总数量:%d\n", totalQuantity);
printf("库存总价值:%.2f\n", totalPrice);
}
void salesStatistics() {
// TODO: 实现销售统计功能
printf("销售统计功能暂未实现!\n");
}
void saveFoods() {
FILE *file = fopen("foods.txt", "w");
if (file == NULL) {
printf("保存失败!\n");
return;
}
for (int i = 0; i < foodCount; i++) {
fprintf(file, "%s %d %.2f\n", foods[i].name, foods[i].quantity, foods[i].price);
}
fclose(file);
printf("保存成功!\n");
}
void loadFoods() {
FILE *file = fopen("foods.txt", "r");
if (file == NULL) {
printf("读取失败!\n");
return;
}
foodCount = 0;
while (fscanf(file, "%s %d %f", foods[foodCount].name, &foods[foodCount].quantity, &foods[foodCount].price) != EOF) {
foodCount++;
}
fclose(file);
printf("读取成功!\n");
}
int main() {
int choice;
while (1) {
printf("\n超市食品管理系统\n");
printf("1. 食品录入\n");
printf("2. 食品查询\n");
printf("3. 食品修改\n");
printf("4. 食品删除\n");
printf("5. 食品排序\n");
printf("6. 食品分类\n");
printf("7. 库存统计\n");
printf("8. 销售统计\n");
printf("9. 保存食品信息\n");
printf("10. 读取食品信息\n");
printf("0. 退出\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
addFood();
break;
case 2:
searchFood();
break;
case 3:
updateFood();
break;
case 4:
deleteFood();
break;
case 5:
sortFoods();
break;
case 6:
classifyFoods();
break;
case 7:
stockStatistics();
break;
case 8:
salesStatistics();
break;
case 9:
saveFoods();
break;
case 10:
loadFoods();
break;
case 0:
printf("谢谢使用!\n");
exit(0);
default:
printf("无效的操作编号!\n");
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)