用C语言编写饮料自动售货机程序,要求根据所选饮料计算应找的钱数,设计算法实现找币及统计所售出的饮料和销售额
时间: 2024-02-27 11:57:08 浏览: 26
好的,这是一个较为复杂的程序,需要考虑多个方面的问题。以下是我设计的算法:
1. 定义饮料种类和价格
- 可以使用结构体数组来存储每种饮料的名称、价格和数量等信息
2. 接收用户输入
- 可以使用 scanf 函数,让用户输入饮料编号和付款金额
3. 判断用户输入是否合法
- 首先判断饮料编号是否存在,如果不存在则提示用户重新输入
- 然后判断付款金额是否足够购买饮料,如果不足则提示用户重新输入
4. 计算找零
- 根据用户付款金额和饮料价格计算找零金额,可以使用简单的减法运算
- 打印找零金额,并且更新所选饮料的数量
5. 统计销售额和饮料销量
- 可以使用全局变量来记录总销售额和每种饮料的销量
- 每次售出饮料时,更新销售额和饮料销量
6. 输出销售统计信息
- 在程序结束时,输出每种饮料的销量和总销售额
下面是示例代码,仅供参考:
```c
#include <stdio.h>
// 饮料结构体
struct Drink {
char name[20];
int price;
int quantity;
};
// 饮料数组
struct Drink drinks[] = {
{"可乐", 3, 10},
{"雪碧", 3, 10},
{"矿泉水", 2, 10}
};
// 销售额和饮料销量
int total_sales = 0;
int drink_sales[] = {0, 0, 0};
int main() {
int drink_id, payment, change;
while (1) {
// 打印饮料信息
printf("饮料编号\t饮料名称\t饮料价格\t饮料数量\n");
for (int i = 0; i < sizeof(drinks) / sizeof(drinks[0]); i++) {
printf("%d\t%s\t%d元\t%d瓶\n", i + 1, drinks[i].name, drinks[i].price, drinks[i].quantity);
}
// 接收用户输入
printf("请选择饮料编号:");
scanf("%d", &drink_id);
printf("请输入付款金额:");
scanf("%d", &payment);
// 判断饮料编号是否合法
if (drink_id < 1 || drink_id > sizeof(drinks) / sizeof(drinks[0])) {
printf("输入的饮料编号不合法,请重新输入!\n");
continue;
}
// 判断付款金额是否足够购买饮料
int drink_price = drinks[drink_id - 1].price;
if (payment < drink_price) {
printf("付款金额不足,请重新输入!\n");
continue;
}
// 计算找零并更新饮料数量
change = payment - drink_price;
printf("找零:%d元\n", change);
drinks[drink_id - 1].quantity--;
// 统计销售额和饮料销量
total_sales += drink_price;
drink_sales[drink_id - 1]++;
// 输出销售统计信息
printf("销售统计:\n");
for (int i = 0; i < sizeof(drinks) / sizeof(drinks[0]); i++) {
printf("%s:%d瓶\n", drinks[i].name, drink_sales[i]);
}
printf("总销售额:%d元\n", total_sales);
}
return 0;
}
```
相关推荐
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)