码迷,mamicode.com
首页 > 其他好文 > 详细

Exercise 3.3 Calculate a discounted price

时间:2014-12-17 23:53:57      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   sp   for   on   div   

Exercise 3-3. Write a program that will calculate the price for a quantity entered from
the keyboard, given that the unit price is $5 and there is a discount of 10 percent for
quantities over 30 and a 15 percent discount for quantities over 50.

 1 // Exercise 3.3 Calculate a discounted price
 2 
 3 // I interpreted this exercise as implying that the 10% applies to items 31 to 50
 4 // and the 15% applies to items in excess of 50.
 5 // That is, you don‘t get 15% discount on the whole price when you order 51 items.
 6 
 7 // There is more than one way of doing this so different is not necessarily wrong.
 8 
 9 #include <stdio.h>
10 
11 int main(void)
12 {
13   const int level1 = 30;               // Quantity over this level are at discount1
14   const int level2 = 50;               // Quantity over this level are at discount2
15   const double discount1 = 0.10;       // 10% discount
16   const double discount2 = 0.15;       // 15% discount
17   const double unit_price = 5.0;       // Basic unit price
18   int quantity = 0;
19   int qty_full_price = 0;              // 0 to 30 at full price
20   int qty_level1 = 0;                  // 31 to 50 at level1 price
21   int qty_level2 = 0;                  // Over 50 at level2 price
22   printf("Enter the quantity that you require: ");
23   scanf("%d", &quantity);
24  
25   if(quantity > 50)                     // Quantity over 50
26   {
27     qty_full_price = level1;
28     qty_level1 = level2 - level1;
29     qty_level2 = quantity - level2;
30   }
31   else if(quantity > 30)                // Quantity is from 30 to 50
32   {
33     qty_full_price = level1;
34     qty_level1 = quantity - level1;
35   }
36   else
37     qty_full_price = quantity;
38 
39   printf("The total price for %d items is $%.2lf\n", quantity,
40     unit_price*(qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2));
41   return 0;
42 }

 

Exercise 3.3 Calculate a discounted price

标签:style   blog   ar   io   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/xiaomi5320/p/4170586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!