c语言编写一个商店销售系统的程序
时间: 2024-03-16 19:48:01 浏览: 22
这是一个比较大的项目,需要设计数据库、用户界面、交互逻辑等多个方面,我可以给你提供一个简单的C语言程序示例,作为参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20]; // 商品名称
int price; // 商品单价
int count; // 商品数量
} Item;
typedef struct {
Item items[100]; // 商品列表
int count; // 商品个数
} Cart;
void show_menu() {
printf("======= 欢迎使用商店销售系统 =======\n");
printf("1. 添加商品\n");
printf("2. 查看购物车\n");
printf("3. 结算\n");
printf("4. 退出\n");
}
void add_item(Cart* cart) {
Item item;
printf("请输入商品名称:");
scanf("%s", item.name);
printf("请输入商品单价:");
scanf("%d", &item.price);
printf("请输入商品数量:");
scanf("%d", &item.count);
cart->items[cart->count++] = item;
printf("添加成功!\n");
}
void show_cart(Cart* cart) {
printf("======= 购物车清单 =======\n");
printf("商品名称\t单价\t数量\n");
for (int i = 0; i < cart->count; i++) {
Item item = cart->items[i];
printf("%s\t\t%d\t%d\n", item.name, item.price, item.count);
}
}
void checkout(Cart* cart) {
int total = 0;
for (int i = 0; i < cart->count; i++) {
Item item = cart->items[i];
total += item.price * item.count;
}
printf("您的购物车总价为:%d元\n", total);
}
int main() {
Cart cart = {0};
int choice = 0;
while (choice != 4) {
show_menu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item(&cart);
break;
case 2:
show_cart(&cart);
break;
case 3:
checkout(&cart);
break;
case 4:
printf("谢谢使用!\n");
break;
default:
printf("无效的选择,请重新输入。\n");
break;
}
}
return 0;
}
```
这个程序实现了一个简单的商店销售系统,用户可以添加商品、查看购物车和结算。你可以根据这个示例,结合具体的需求和实际情况,进行进一步的开发和优化。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)