某购物中心根据顾客购买商品的总金额(单位:元)进行打 折促销,输入顾客购买商品的总金额x(设x是大于0的整 数),计算并输出顾客应付总金额y(要求使用选择结构实 现) case语句
时间: 2024-01-10 10:04:16 浏览: 161
好的,以下是使用 `switch-case` 语句实现的顾客打折的 C 代码:
```c
#include <stdio.h>
int main() {
float x, y;
printf("请输入顾客购买商品的总金额:");
scanf_s("%f", &x);
if (x <= 0) {
printf("输入金额错误!\n");
return 0;
}
int discount = 0;
if (x < 100) {
discount = 0;
} else if (x < 200) {
discount = 1;
} else if (x < 300) {
discount = 2;
} else {
discount = 3;
}
switch(discount) {
case 0:
y = x;
break;
case 1:
y = x * 0.9;
break;
case 2:
y = x * 0.8;
break;
case 3:
y = x * 0.7;
break;
}
printf("顾客应付总金额为:%.2f元\n", y);
return 0;
}
```
该程序首先使用 `scanf_s` 函数获取用户输入的总金额 `x`,然后使用 `if-else` 语句判断需要使用哪个打折方案,得到对应的打折方案编号 `discount`,最后使用 `switch-case` 语句根据 `discount` 的值计算打折后的金额 `y`,并使用 `printf` 函数输出顾客应付总金额。同样地,程序中对输入金额的合法性进行了判断,如果输入金额小于等于 0,程序会输出错误提示信息并退出。
阅读全文
相关推荐














