(switch语句) 某超市进行果汁促销活动,每瓶果汁的单价为20元,买5瓶以人上(包括5瓶)10瓶以下打9折: 10瓶以上(包括10瓶)20瓶以下打85折;20瓶以上(包括20瓶)30瓶以下打8折;30瓶以上打75折。 编写桯序,根据顾客的购买数量计算所要付款的数额(保留2位小数)
时间: 2023-05-23 22:01:51 浏览: 51
以下是该超市果汁促销活动的计价程序,您可以运行自己的程序来测试它:
```
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double PRICE = 20.0; // 每瓶果汁单价
int quantity; // 购买的数量
double total; // 总价
cout << "请输入购买的数量: ";
cin >> quantity;
switch (quantity)
{
case 1:
case 2:
case 3:
case 4:
case 5:
total = PRICE * quantity * 0.9;
break;
case 6:
case 7:
case 8:
case 9:
case 10:
total = PRICE * quantity * 0.85;
break;
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
total = PRICE * quantity * 0.8;
break;
default:
total = PRICE * quantity * 0.75;
break;
}
cout << "应付金额为: " << fixed << setprecision(2) << total << "元" << endl;
return 0;
}
```
该程序采用 switch 语句判断购买的数量,根据数量不同应用不同的折扣率计算总价,最后输出结果。运行程序,就可以得到正确的结果了。
相关推荐
![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_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)