题目内容:某超市商品打折促销。假定购买某商品的数量为x件,折扣情况如下: 由键盘输入商品的价格和购买的数量,编程计算购买x件商品应付总金额。要求用switch语句编程实现。
时间: 2023-11-28 16:50:45 浏览: 103
好的,下面是使用switch语句编写的计算购买商品应付总金额的程序:
```c
#include <stdio.h>
int main() {
int price, quantity; // 商品单价和购买数量
float total; // 应付总金额
printf("请输入商品单价和购买数量:");
scanf("%d%d", &price, &quantity);
switch(quantity) {
case 1:
total = price;
break;
case 2:
total = price * quantity * 0.95; // 打95折
break;
case 3:
total = price * quantity * 0.9; // 打9折
break;
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
total = price * quantity * 0.85; // 打85折
break;
default:
total = price * quantity * 0.8; // 打8折
break;
}
printf("应付总金额为:%.2f元\n", total);
return 0;
}
```
程序中,首先从键盘输入商品单价和购买数量,接着使用switch语句根据购买数量计算应付总金额。根据题目要求,当购买数量为1时,不打折;当数量为2时,打95折;当数量为3时,打9折;当数量为4~9时,打85折;当数量大于等于10时,打8折。最后输出应付总金额。注意,程序中使用了浮点数类型的变量来存储应付总金额,以避免由于整数除法的特性而导致计算结果不准确的问题。