用c写某购物中心根据顾客购买商品的总金额进行打折促销,输入顾客购买商品的总金额x(设x是大于0的整数),计算并输出顾客应付总金额y(要求使用选择结构实现)。
时间: 2023-04-04 11:03:39 浏览: 211
好的,这是一个关于编程的问题,我可以回答。以下是用 C 语言实现的代码:
#include <stdio.h>
int main() {
int x, y;
printf("请输入顾客购买商品的总金额:");
scanf("%d", &x);
if (x >= 1000) {
y = x * 0.8;
} else if (x >= 500) {
y = x * 0.9;
} else {
y = x;
}
printf("顾客应付总金额为:%d\n", y);
return 0;
}
以上代码中,根据顾客购买商品的总金额 x,使用选择结构判断是否满足打折条件,然后计算顾客应付总金额 y,并输出结果。
相关问题
某购物中心根据顾客购买商品的总金额(单位:元)进行打 折促销,输入顾客购买商品的总金额x(设x是大于0的整 数),计算并输出顾客应付总金额y(要求使用选择结构实 现) x
好的,根据你的要求,我来给你写一个使用选择结构实现的顾客打折的 C 代码:
```c
#include <stdio.h>
int main() {
float x, y;
printf("请输入顾客购买商品的总金额:");
scanf_s("%f", &x);
if (x <= 0) {
printf("输入金额错误!\n");
return 0;
} else if (x < 100) {
y = x;
} else if (x < 200) {
y = x * 0.9;
} else if (x < 300) {
y = x * 0.8;
} else {
y = x * 0.7;
}
printf("顾客应付总金额为:%.2f元\n", y);
return 0;
}
```
该程序首先使用 scanf_s 函数获取用户输入的总金额 x,然后使用 if-else 语句根据总金额的大小判断打折的程度,最后使用相应的折扣系数计算打折后的金额 y,并使用 printf 函数输出顾客应付总金额。注意,程序中对输入金额的合法性进行了判断,如果输入金额小于等于 0,程序会输出错误提示信息并退出。
由键盘输入商品的价格和购买的数量,编程计算购买x件商品应付总金额。要求用switch语句编程实现。c语言
以下是用switch语句编写的C语言代码,实现计算购买x件商品应付总金额的功能:
```c
#include <stdio.h>
int main() {
float price, total_price;
int quantity;
printf("请输入商品价格:");
scanf("%f", &price);
printf("请输入购买数量:");
scanf("%d", &quantity);
switch(quantity) {
case 1:
total_price = price;
break;
case 2:
total_price = price * 2 * 0.95;
break;
case 3:
total_price = price * 3 * 0.9;
break;
case 4:
total_price = price * 4 * 0.85;
break;
default:
total_price = price * quantity * 0.8;
break;
}
printf("购买%d件商品应付总金额为%.2f元\n", quantity, total_price);
return 0;
}
```
在这个程序中,我们首先通过键盘输入获取商品价格和购买数量。然后,使用switch语句根据购买数量计算应付总金额。如果购买数量为1,总金额就是商品价格;如果购买数量为2,总金额就是商品价格乘以2再打95折,以此类推。最后,用printf函数输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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_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)