编写一个c语言程序根据键盘输入一个值计算总价单价是5¥数值超过30的折扣10数值超过50的折扣是15。
以下是一个根据键盘输入值计算总价的C语言程序:
#include <stdio.h>
int main() {
int quantity;
float unitPrice = 5.0;
float totalPrice;
printf("请输入购买数量:");
scanf("%d", &quantity);
totalPrice = quantity * unitPrice;
if (totalPrice > 50.0) {
totalPrice *= 0.85; // 打15%折扣
} else if (totalPrice > 30.0) {
totalPrice *= 0.9; // 打10%折扣
}
printf("总价为:%.2f¥\n", totalPrice);
return 0;
}
在上述程序中,我们使用scanf函数从键盘输入购买数量。然后,我们根据购买数量乘以单价来计算总价。接着,我们使用if语句来判断总价是否超过30或50,并相应地应用折扣。最后,我们使用printf函数来输出计算得到的总价。
注意,我们使用.2f来控制输出总价时只保留小数点后两位
原文地址: http://www.cveoy.top/t/topic/hQJK 著作权归作者所有。请勿转载和采集!