Exercise 3.3 Calculate a discounted price

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 }
时间: 2024-11-10 14:01:22

Exercise 3.3 Calculate a discounted price的相关文章

Exercise 2.3 Calculating volume price of alternative products

// Exercise 2.3 Calculating volume price of alternative products // The only problem here is to devise a way to determine the price // for the product type. Here I used the product type value to do this. #include <stdio.h> int main(void) { double to

C++primer第十五章. 面向对象编程

面向对象编程基于三个基本概念:数据抽象.继承和动态绑定. 15.1. 面向对象编程:概述 面向对象编程的关键思想是多态性(polymorphism). 之所以称通过继承而相关联的类型为多态类型,是因为在许多情况下可以互换地使用派生类型或基类型的“许多形态”.正如我们将看到的,在 C++ 中,多态性仅用于通过继承而相关联的类型的引用或指针. 继承 派生类(derived class)能够继承基类(baseclass)定义的成员,派生类可以无须改变而使用那些与派生类型具体特性不相关的操作,派生类可以

牛津高中英语模块8Reading课文

Unit 1 Appreciating literature What is classic literature? Classics are the antiques of the literary world. They are novels, plays and poems that were written a long time ago and were so well written and well received that people still read them toda

4 THINGS I WISH I WOULD HAVE KNOWN WHEN I STARTED MY SOFTWARE DEVELOPMENT CAREER

4 Things I Wish I Would Have Known When I Started My Software Development Career My software development career began about 15 years ago. But only in about the last 5 years did I really start to see a large boost in my software development career. He

基于 .NET 的开源AOP框架评估

Rating of Open Source AOPFrameworks in .NET 基于 .NET 的开源AOP框架评估 Introduction 引言 In the days where business agility is becoming the definite needof any business / IT infrastructure, quite frequentlywe are ending up with facing scenarios where we need t

C++开源库集合

| Main | Site Index | Download | mimetic A free/GPL C++ MIME Library mimetic is a free/GPL Email library (MIME) written in C++ designed to be easy to use and integrate but yet fast and efficient. It is based on the C++ standard library and heavily us

[MST] Derive Information from Models Using Views

Redundant data or caching data is a constant source of bugs. MST adheres to the philosophy that no data that can be derived should ever get stored. In this lesson, you will learn how to introduce views that declaratively derive data and which cache d

Code Signal_练习题_matrixElementsSum

After they became famous, the CodeBots all decided to move to a new building and live together. The building is represented by a rectangular matrix of rooms. Each cell in the matrix contains an integer that represents the price of the room. Some room

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression

斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression 1  Linear regression with one variable In thispart of this exercise, you will implement linear regression with one variableto predict profits for a food truck. Suppose you are the CEO of a rest