sinat_29373003 2015-07-09 12:18 采纳率: 80%
浏览 1514
已采纳

这个c程序如果输入20的话,是不是只执行最后一个else后面的语句???

// Exercise 3.3 Calculate a discounted price

// I interpreted this exercise as implying that the 10% applies to items 31 to 50
// and the 15% applies to items in excess of 50.
// That is, you don't get 15% discount on the whole price when you order 51 items.

// There is more than one way of doing this so different is not necessarily wrong.

#include

int main(void)
{
const int level1 = 30; // Quantity over this level are at discount1
const int level2 = 50; // Quantity over this level are at discount2
const double discount1 = 0.10; // 10% discount
const double discount2 = 0.15; // 15% discount
const double unit_price = 5.0; // Basic unit price
int quantity = 0;
int qty_full_price = 0; // 0 to 30 at full price
int qty_level1 = 0; // 31 to 50 at level1 price
int qty_level2 = 0; // Over 50 at level2 price
printf("Enter the quantity that you require: ");
scanf("%d", &quantity);

if(quantity > 50) // Quantity over 50
{
qty_full_price = level1;
qty_level1 = level2 - level1;
qty_level2 = quantity - level2;
}
else if(quantity > 30) // Quantity is from 30 to 50
{
qty_full_price = level1;
qty_level1 = quantity - level1;
}
else
qty_full_price = quantity;

printf("The total price for %d items is $%.2lf\n", quantity,
unit_price*(qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2));
return 0;
}

  • 写回答

7条回答

  • danielinbiti 2015-07-09 12:31
    关注
     是的,小于等于30的数都走最后一个else分支。也就是qty_full_price = quantity;这条语句。再后面的语句是公共的,不管哪个分支最后都会执行
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题