sinat_29373003 于 2015.07.09 20:18 提问

// 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 20:31

`````` 是的，小于等于30的数都走最后一个else分支。也就是qty_full_price = quantity;这条语句。再后面的语句是公共的，不管哪个分支最后都会执行
``````
danielinbiti 回复sinat_29373003: (qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2)所以这个结果也就是5*(20+0+0)=100

danielinbiti 回复sinat_29373003: (1.0 - discount2)*qty_level2，qty_level2等于0，结果也是0

danielinbiti 回复sinat_29373003: 先算括号 (1.0 - discount1)*qty_level1，这里qty_level1等于0，结果是0吧

sinat_29373003 不是应该先算括号内的吗？？？？而且1减0.1和0.85都得算，因为刚开始就申明了。

danielinbiti 回复sinat_29373003: unit_price=5，qty_full_price =20，两个相乘，就是100，后面qty_level1 =0，qty_level2=0，后面两个结果是0，刚好100

danielinbiti 回复sinat_29373003: unit_price*(qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2));

sinat_29373003 但是这样程序就算不对，我用电脑输入20最后结果是100.但是我用笔算怎么也算不出100啊，我算出0了。。。。你算算看

devmiao      2015.07.09 20:58

`````` else if (...) {  }
``````

`````` else
{
if (...) {  }
}
``````

`````` if (xxx)
aaa;
bbb;
ccc;
``````

bbb总是执行。

u012216727      2015.07.09 20:26

Ivan_zstu 是的，你的 #include的后面 丢了库文件，#include<stdio.h>,输入20，输出“The total price for 20 items is \$100.00”，建议你else后分支，用括号括起来。

sinat_29373003 但是这样程序就算不对，我用电脑输入20最后结果是100.但是我用笔算怎么也算不出100啊，我算出0了。。。。你算算看

frank_20080215   2015.07.09 20:36

sinat_29373003 但是这样程序就算不对，我用电脑输入20最后结果是100.但是我用笔算怎么也算不出100啊，我算出0了。。。。你算算看

sinat_29373003   2015.07.09 20:55

frank_20080215   2015.07.09 21:28

qty_full_price = quantity; -- qty_full_price = 数量20吗？

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

lingzhu111   2015.08.07 16:50

C语言风格
if （ xxx）
a;
b;

if （ xxx）
a;
else
b；
c；