Advertisement
rebbybear

Lab 4 - re

Nov 17th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. //Rebecca Wong, pikachu1997@gmail.com
  2. //Lab 4
  3. //November 2, 2015
  4.  
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <string>
  8. #include <cstdlib>
  9. #include <cctype>
  10. #include <cmath>
  11. #define PROFIT 0.80
  12. using namespace std;
  13.  
  14. //Function prototypes (declaration)
  15. int calcCopies(int, int);
  16. double calcProfit(int, double, int, char, char);
  17. void output(string, int, double);
  18.  
  19. void signature()
  20. {
  21. string email = "pikachu1997@gmail.com",
  22. lab = "Lab 4",
  23. student = "Rebecca Wong";
  24.  
  25. cout << endl << student << endl;
  26. cout << lab << endl;
  27. cout << email << endl;
  28. }
  29.  
  30. int main()
  31. {
  32. //Declare variables
  33. string ISBN;
  34. double unitPrice, profit;
  35. int expectedClass, copies, finalCopies;
  36. char text, text2;
  37. char requiredText = 'R', suggestedText = 'S';
  38. char newText = 'N', previousText = 'O';
  39.  
  40. //Prompt user for input
  41. cout << "Enter the book's ten digit ISBN number: ";
  42. cin >> ISBN;
  43.  
  44. cout << "Enter the price per book copy: ";
  45. cin >> unitPrice;
  46.  
  47. cout << "Enter the expected class enrollment: ";
  48. cin >> expectedClass;
  49.  
  50. cout << endl << "Is the text required or suggested? "
  51. << endl << "Enter ""R"" for required text, or ""S"" for suggested text: ";
  52. cin >> text;
  53. text = toupper(text);
  54.  
  55. cout << endl << "Is the text new or has it been used in previous quarters? "
  56. << endl << "Enter ""N"" for new text, or ""O"" for a previously used text: ";
  57. cin >> text2;
  58. text2 = toupper(text2);
  59.  
  60. //Calls function that calculates the number of copies needed
  61. finalCopies = calcCopies(copies, expectedClass);
  62.  
  63. //Calls function that calculates the profit
  64. profit = calcProfit(copies, unitPrice, expectedClass, text, text2);
  65.  
  66. //Calls function for output
  67. output(ISBN, finalCopies, profit);
  68.  
  69. signature();
  70. system("pause;");
  71. return 0;
  72. }
  73. int calcCopies(int copies, int enrollment)
  74. {
  75. /*
  76. Pre: copies - number of books to order
  77. enrollment - number of students that are going to buy a book
  78. Purpose : compute the number of copies needed
  79. Post : return finalCopies value
  80. */
  81.  
  82. int finalCopies = copies * enrollment;
  83.  
  84. return finalCopies;
  85. }
  86.  
  87. double calcProfit(int copies, double unitPrice, int classEnrollment, char text, char text2)
  88. {
  89. /*
  90. Pre : copies - number of copies
  91. unitPrice - price of a book for the student
  92. classEnrollment - expected number of students for that class
  93. text - required or suggested book
  94. text 2 - new or old book
  95. Purpose : calculate the profit the bookstore earns
  96. Post : return porfit value
  97. */
  98. double pricePerBook = .20 * unitPrice;
  99. double profit;
  100.  
  101. if (text == 'R' && text2 == 'N')
  102. {
  103. profit = copies * pricePerBook * classEnrollment * 0.90;
  104. }
  105.  
  106. else if (text == 'R' && text2 == 'N')
  107. {
  108. profit = copies * pricePerBook * classEnrollment * 0.65;
  109. }
  110.  
  111. else if (text == 'S' && text2 == 'N')
  112. {
  113. profit = copies * pricePerBook * classEnrollment * 0.40;
  114. }
  115.  
  116. else if (text == 'S' && text2 == 'O')
  117. {
  118. profit = copies * pricePerBook * classEnrollment * 0.20;
  119. }
  120.  
  121. return profit;
  122. }
  123.  
  124. void output(string ISBN, int finalCopies, double profit)
  125. {
  126. /*
  127. Pre : ISBN - ISBN number of the book
  128. finalCopies - number of books to be bought
  129. profit - profit the bookstore gains
  130. Purpose : print results our for bookstore's use
  131. Post : output
  132. */
  133. cout << setprecision(2) << fixed;
  134.  
  135. cout << "ISBN" << ISBN << endl;
  136. cout << "Copies needed: " << finalCopies << endl;
  137. cout << "Profit: " << "$" << profit << endl;
  138.  
  139. return;
  140. }
  141. /*
  142. CALCULATE
  143. Calculate the number of copies needed and the profit if the store pays 80% of list (use
  144. global define for this). For the number of books to order, round up to the nearest whole
  145. book.
  146.  
  147. OUTPUT A sample run might look like this:
  148. Enter book number: 0755798652
  149. Enter price per copy: 34.98
  150. Enter expected class enrollment: 35
  151. Enter 'R' if required or 'S' if suggested: r
  152. Enter 'N' if new or 'O' if not a new text: O
  153. ISBN: 0755798652
  154. Copies Needed: 23
  155. Profit: $ 160.91
  156.  
  157. TEST DATA
  158. 0755798652,34.98,35,R,O
  159. 3453456784,23.95,100,R,N
  160. 5677655673,54.50,40,R,O
  161. 2462462464,5.95,40,S,O
  162. 8953647888, 65.99, 35, N, Y
  163.  
  164. THEME ISSUES
  165. Functions (pass by value), IF Control Structures, character type, rounding
  166.  
  167. Checkpoints
  168. 2) Minimum of three (3) comments in each function (Purpose: Pre: Post:)
  169. 4) To do this use if() and assign uppercase value or you may use
  170. toupper() function (see 10.2 on page 545 of text). Only an error message should
  171. be output and nothing more if data is erroneous. Use exit() function as explained
  172. in 6.15 (p. 360).
  173. 5) A function must be used to calculate the number of books to be ordered. Round to the nearest whole number.
  174. Hint: static_cast<int> (x + .5)
  175. 8) Output must be formatted using dollar sign and 2 decimal place as shown on page 1.
  176.  
  177. Test Data: Execute the program five times and each time copy and paste the
  178. output to the bottom of the program.
  179. Set 1 –0755798652, 34.98, 31, R, O
  180. Set 2 – 3453456784, 23.95, 100, r, N
  181. Set 3 – 5677655673, 54.50, 40, R, O
  182. Set 4 – 2462462464, 5.95, 40,s,O
  183. Set 5 – 8953647888, 65.99, 35, N, Y
  184. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement