给我随便写一个购物商城管理后台
时间: 2023-03-25 20:03:42 浏览: 47
好的,以下是一个简单的购物商城管理后台的代码:
#include <stdio.h>
#include <stdlib.h>
struct product {
int id;
char name[50];
float price;
int quantity;
};
int main() {
struct product products[100];
int num_products = ;
int choice;
do {
printf("请选择操作:\n");
printf("1. 添加商品\n");
printf("2. 修改商品\n");
printf("3. 删除商品\n");
printf("4. 查看所有商品\n");
printf("5. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入商品名称:");
scanf("%s", products[num_products].name);
printf("请输入商品价格:");
scanf("%f", &products[num_products].price);
printf("请输入商品数量:");
scanf("%d", &products[num_products].quantity);
products[num_products].id = num_products + 1;
num_products++;
printf("商品添加成功!\n");
break;
case 2:
printf("请输入要修改的商品编号:");
int id;
scanf("%d", &id);
if (id <= || id > num_products) {
printf("商品编号无效!\n");
} else {
printf("请输入新的商品名称:");
scanf("%s", products[id-1].name);
printf("请输入新的商品价格:");
scanf("%f", &products[id-1].price);
printf("请输入新的商品数量:");
scanf("%d", &products[id-1].quantity);
printf("商品修改成功!\n");
}
break;
case 3:
printf("请输入要删除的商品编号:");
int del_id;
scanf("%d", &del_id);
if (del_id <= || del_id > num_products) {
printf("商品编号无效!\n");
} else {
for (int i = del_id - 1; i < num_products - 1; i++) {
products[i] = products[i+1];
}
num_products--;
printf("商品删除成功!\n");
}
break;
case 4:
printf("所有商品如下:\n");
printf("编号\t名称\t价格\t数量\n");
for (int i = ; i < num_products; i++) {
printf("%d\t%s\t%.2f\t%d\n", products[i].id, products[i].name, products[i].price, products[i].quantity);
}
break;
case 5:
printf("谢谢使用!\n");
break;
default:
printf("无效的选择!\n");
break;
}
} while (choice != 5);
return ;
}
相关推荐
![](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)