Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. Program name: Lab10
  2. *Author: Michael G!
  3. *Course Cosc 1435- Introduction to Problem Solving I
  4. *Date written: November 12, 2008
  5. *Purpose:The program will help a student learn addition subtraction
  6. *multiplication and division by giving a random problem and checking
  7. *if the users answer is right
  8. *Input:The user will imput if they want to use add subtract multiply
  9. *or divide. The user will also enter what they got for the problem
  10. *Output: After the two random numbers are added, the program will
  11. *display the results and say if they got the problem right or not.
  12. *The program will aos display error messages if invalid data is entered
  13. ***********************************************************************/
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cmath>
  17. using namespace std;
  18.  
  19. char getMenuChoice () ;
  20. void ProcessChoice (char choice);
  21. void showResults (bool answer) ;
  22.  
  23. int doAddition (int num1, int num2) ;
  24. int doSubtraction (int num1, int num2) ;
  25. int doMultiply (int num1, int num2) ;
  26.  
  27. float doDivision (int num1, int num2) ;
  28.  
  29. bool getIntAnswer (int answer) ;
  30. bool getFloatAnswer (float answer) ;
  31.  
  32.  
  33.  
  34.  
  35. int main()
  36. {
  37. char choice;
  38.  
  39. choice = getMenuChoice();
  40. ProcessChoice(choice);
  41. return 0 ;
  42. }
  43.  
  44.  
  45. char getMenuChoice()
  46. {
  47. char choice;
  48.  
  49. cout << "Choose a number\n";
  50. cout << "1. add\n"
  51. << "2. subtract\n"
  52. << "3. multiply\n"
  53. << "4. divide\n"
  54. << "5. quit" ;
  55. cin >> choice;
  56.  
  57. while (choice < '1' || choice >'5')
  58. {
  59. cout << "Re-enter choice: ";
  60. cin >> choice;
  61. }
  62. return choice;
  63. }
  64.  
  65. void ProcessChoice (char choice)
  66. {
  67. int num1,
  68. num2,
  69. intAns;
  70. float floatAns;
  71. bool right;
  72.  
  73. srand((unsigned)time(NULL));
  74.  
  75. num1 = rand() % 150 + 1;
  76. num2 = rand() % 150 + 1;
  77.  
  78. switch (choice)
  79. {
  80. case '1': intAns = doAddition(num1, num2);
  81. right = getIntAnswer(intAns);
  82. showResults(right);
  83. break;
  84. case '2': intAns = doSubtraction(num1, num2) ;
  85. right = getIntAnswer (intAns) ;
  86. showResults(right);
  87. break;
  88. case '3': intAns = doMultiply(num1, num2) ;
  89. right = getIntAnswer (intAns);
  90. break;
  91. case '4': floatAns = doDivision(num1, num2) ;
  92. floatAns = getFloatAnswer (floatAns);
  93. break;
  94. case '5': "Thank you for your time" ;
  95.  
  96.  
  97. default: ;
  98. }
  99. }
  100.  
  101. void showResults (bool right)
  102. {
  103.  
  104. if (right==true)
  105. {
  106. cout << "Way to go \n" ;
  107. }
  108. else
  109. {
  110. cout << "Try again \n";
  111.  
  112. }
  113. }
  114.  
  115. int doAddition (int num1, int num2)
  116. { int add;
  117.  
  118. if (num1 >= num2)
  119. {
  120. add = (num1 + num2);
  121. }
  122. else if (num2 >= num1)
  123. {
  124. add = (num2 + num1);
  125. }
  126. cout << setw(5) << num1 << '\n'
  127. << setw(2) << "+"
  128. << setw(3) << num2 << '\n'
  129. << setw(5) << "-----" << '\n';
  130. return add ;
  131. }
  132.  
  133. int doSubtraction (int num1, int num2)
  134. { int subtract;
  135. if (num1 >= num2)
  136. {
  137. subtract = (num1 - num2);
  138. cout << setw(5) << num1 << '\n'
  139. << setw(2) << "-"
  140. << setw(3) << num2 << '\n'
  141. << setw(5) << "-----" << '\n';
  142. }
  143. else if (num2 >= num1)
  144. {
  145. subtract = (num2 - num1);
  146. cout << setw(5) << num2 << '\n'
  147. << setw(2) << "-"
  148. << setw(3) << num1 << '\n'
  149. << setw(5) << "-----" << '\n';
  150. }
  151.  
  152. return subtract ;
  153. }
  154.  
  155. int doMultiply (int num1, int num2)
  156. { int multiply;
  157.  
  158. if (num1 >= num2)
  159. {
  160. multiply = (num1 * num2);
  161. }
  162. else if (num2 >= num1)
  163. {
  164. multiply = (num2 * num1);
  165. }
  166. cout << setw(5) << num1 << '\n'
  167. << setw(2) << "*"
  168. << setw(3) << num2 << '\n'
  169. << setw(5) << "-----" << '\n';
  170.  
  171. return multiply ;
  172. }
  173.  
  174. float doDivision (int num1, int num2)
  175. { float division;
  176. if (num1 >= num2)
  177. {
  178. division = (num1 / static_cast<float>(num2));
  179. cout << num1 << "/" << num2 ; '\n' ;
  180.  
  181. }
  182. else if (num2 >= num1)
  183. {
  184. division = (num2 / static_cast<float>(num1));
  185. cout << num2 << "/" << num1 ; '\n' ;
  186.  
  187. }
  188.  
  189. return division ;
  190. }
  191.  
  192. bool getIntAnswer (int answer)
  193. {
  194. int uAnswer;
  195. cout << "Enter your answer \n";
  196. cin >> uAnswer;
  197.  
  198. if(answer == uAnswer)
  199. return true;
  200. else
  201. return false;
  202. }
  203.  
  204. bool getFloatAnswer (float answer)
  205. {
  206. float uAnswer;
  207. cout << "Enter your answer \n";
  208. cin >> uAnswer;
  209.  
  210. if(answer == uAnswer)
  211. return true;
  212. else
  213. return false;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement