Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. //Define MathOperator as a function that requires 2 doubles to be passed.
  8. //This will allow me to store all my 2 operand functions into an array to avoid
  9. //unneeded switch/if statements
  10. typedef double (*MathOperator) (double a, double b);
  11.  
  12. //Declare all my functions
  13. double getAdditionResult(double a, double b);
  14. double getMultiplicationResult(double a, double b);
  15. double getSubtractionResult(double a, double b);
  16. double getDivisionResult(double a, double b);
  17. double getExponentiationResult(double a, double b);
  18. double getSquareRoot(double a);
  19.  
  20. //Store my operator list into a char array for display purposes later in the program
  21. char operatorList[] = {
  22.     '+', '-', '*', '/', '^'
  23. };
  24.  
  25. //Constants for the teaching mode and calculator mode
  26. //Yes I know calculator mode is not used in the program,
  27. //but a defined constant should be here regardless.
  28. const int MODE_TEACHING = 1;
  29. const int MODE_CALCULATOR = 2;
  30.  
  31. /**
  32. *   Psuedocode:
  33. *   Initialize variables that will be used throughout the main method
  34. *       -Create an array of 2 operand functions that can be used later on
  35. *   Print out the menu of possible operations for the user
  36. *   Request that the user select an operation
  37. *       -Require the user to input a number between 1 and 6 for the operations
  38. *   Display the possible modes for the user to select
  39. *       -1 for teaching, 2 for calculator
  40. *       -Require the user to select 1 or 2
  41. *
  42. *   If the selected operation is the square root operation:
  43. *       Ask the user to input the number to be square rooted
  44. *           If the mode is in teaching mode ask the user to input what they think is the answer
  45. *               -If they are right, print out congratulations and terminate the program
  46. *               -If they are wrong, tell them they are wrong. The program will print out the answer later
  47. *           Print out the solution and terminate the program
  48. *
  49. *   Otherwise,
  50. *   Tell the user they are performing a 2 operator function in the format A + B, A - B, ect.
  51. *   Require the user to input the A and B operands
  52. *   Calculate the result
  53. *   If the mode is in teaching
  54. *       Ask the user to input what they think is the right answer
  55. *           -If they are right display a congratulations message and terminate the program
  56. *           -If they are wrong then tell them they are wrong. The program will print out the answer later
  57. *   Print out the operation being performed. Ex. 8 + 4
  58. *   Print out the result of the answer and terminate the program.
  59. * */
  60. int main()
  61. {
  62.     //Initialization of variables that will be used throughout the program
  63.     int selectedOperation;
  64.     int selectedMode;
  65.     double leftOperand;
  66.     double rightOperand;
  67.     double inputtedGuess;
  68.     double result;
  69.  
  70.     //Store all my 2 operand functions into an array of "MathOperator" that was typedef'd above
  71.     MathOperator operators[] = {
  72.         getAdditionResult,
  73.         getSubtractionResult,
  74.         getMultiplicationResult,
  75.         getDivisionResult,
  76.         getExponentiationResult,
  77.     };
  78.  
  79.     //Print out the operations list that the user can select
  80.     cout << "Operations List\n";
  81.     cout << "\t1. (+) Addition\n";
  82.     cout << "\t2. (-) Subtraction\n";
  83.     cout << "\t3. (*) Multiplication\n";
  84.     cout << "\t4. (/) Division\n";
  85.     cout << "\t5. (a^b) Exponentiation\n";
  86.     cout << "\t6. (sqrt(a)) Square Root\n";
  87.  
  88.     //Request that the user select an operation and require them to pick a number between 1 and 6
  89.     cout << "Please select an operation to use(1-6): ";
  90.     cin >> selectedOperation;
  91.  
  92.     while (selectedOperation < 1 || selectedOperation > 6) {
  93.         cout << "Please select an appropriate operation between 1 and 6: ";
  94.         cin >> selectedOperation;
  95.     }
  96.  
  97.     //Print out the possible modes the user can select.
  98.     cout << "Modes\n";
  99.     cout << "\t1. Teaching Mode\n";
  100.     cout << "\t2. Calculator Mode\n";
  101.  
  102.     //Request that the user selects a mode and require them to select either 1 or 2.
  103.     cout << "Please select a mode to use: ";
  104.     cin >> selectedMode;
  105.     while (selectedMode < 1 || selectedMode > 2) {
  106.         cout << "Please select either 1 or 2 for your teaching mode: ";
  107.         cin >> selectedMode;
  108.     }
  109.  
  110.     /**********
  111.     *   Single operand function
  112.     ***********/
  113.  
  114.     //Sqrt is the only operator that requires 1 variable
  115.     //Manually perform this operation since it is the only 1 operand function
  116.     if (selectedOperation == 6) {
  117.         //Request the user to enter a number to square root
  118.         cout << "Enter a number to square root: ";
  119.         cin >> leftOperand;
  120.  
  121.         //If this number is less than 0, the result is an imaginary number. We are not calculating this.
  122.         //Alert the user and terminate the program.
  123.         if (leftOperand < 0) {
  124.             cout << "Cannot calculate the square root of a negative number.\n";
  125.             return 0;
  126.         }
  127.  
  128.         //Store the result into 'result'
  129.         result = getSquareRoot(leftOperand);
  130.  
  131.         //If the selected mode is teaching
  132.         if (selectedMode == MODE_TEACHING) {
  133.  
  134.             //Ask the user what they think the answer is and store it into inputted guess
  135.             cout << "What do you think the answer is?: ";
  136.             cin >> inputtedGuess;
  137.  
  138.             //If they are right the display the congrats message and terminate the program
  139.             //Otherwise tell the user they are wrong.
  140.             if (inputtedGuess == result) {
  141.                 cout << "Congrats! You got the answer right!\n";
  142.                 return 0;
  143.             } else {
  144.                 cout << "Sorry, that was not the correct answer.\n";
  145.             }
  146.         }
  147.  
  148.         //Display the result and terminate the program after flushing cout.
  149.         cout << "The result is: " << result << endl;
  150.         return 0;
  151.     }
  152.  
  153.     /**********
  154.     *   Double operand function
  155.     ***********/
  156.  
  157.     //Tell the user they are performing a 2 operand function
  158.     cout << "You are performing a 2 operand function. This is the format\n";
  159.  
  160.     //Display the format. Ex. A + B, A - B, ect based off what they selected earlier
  161.     cout << "\tA " << operatorList[selectedOperation - 1] << " B\n";
  162.  
  163.     //Request the user to input A and B
  164.     cout << "Please input the values of A and B.\n";
  165.     cout << "A: ";
  166.     cin >> leftOperand;
  167.     cout << "B: ";
  168.     cin >> rightOperand;
  169.  
  170.     //Using the array of functions created earlier, I can call a specific function
  171.     //based off what they selected and pass my 2 operands into the function
  172.     //and store the result into 'result'.
  173.     result = operators[selectedOperation-1](leftOperand, rightOperand);
  174.  
  175.     //If the selected mode is teaching
  176.     if (selectedMode == MODE_TEACHING) {
  177.         //Request the user to input their guess
  178.         cout << "What do you think the result is?: ";
  179.         cin >> inputtedGuess;
  180.  
  181.         //If they are right the display the congrats message and terminate the program
  182.         //Otherwise tell the user they are wrong.
  183.         if (inputtedGuess == result) {
  184.             cout << "Congrats! You got the answer right!\n";
  185.             return 0;
  186.         } else {
  187.             cout << "Sorry, that was not the correct answer.\n";
  188.         }
  189.     }
  190.  
  191.     //Print out the operation the user is doing. Ex. 9 - 4
  192.     //Then print out the result and flush cout.
  193.     cout << "Operation: " << leftOperand << " " << operatorList[selectedOperation - 1] << " " << rightOperand << "\n";
  194.     cout << "The result is: " << result << endl;
  195.  
  196.     return 0;
  197. }
  198.  
  199. /**
  200. *   Get the result of 2 added operands
  201. *
  202. *   @parameter left operand
  203. *   @parameter right operand
  204. *   @result LeftOperand + RightOperand
  205. */
  206. double getAdditionResult(double a, double b) {
  207.     return a + b;
  208. }
  209.  
  210. /**
  211. *   Get the result of 2 subtracted operands
  212. *
  213. *   @parameter left operand
  214. *   @parameter right operand
  215. *   @result LeftOperand - RightOperand
  216. */
  217. double getSubtractionResult(double a, double b) {
  218.     return a - b;
  219. }
  220.  
  221. /**
  222. *   Get the result of 2 divided operands
  223. *
  224. *   @parameter left operand
  225. *   @parameter right operand
  226. *   @result LeftOperand / RightOperand
  227. */
  228. double getDivisionResult(double a, double b) {
  229.     return a / b;
  230. }
  231.  
  232. /**
  233. *   Get the result of 2 multiplied operands
  234. *
  235. *   @parameter left operand
  236. *   @parameter right operand
  237. *   @result LeftOperand * RightOperand
  238. */
  239. double getMultiplicationResult(double a, double b) {
  240.     return a * b;
  241. }
  242.  
  243. /**
  244. *   Get the result of an exponentiated number
  245. *
  246. *   @parameter left operand
  247. *   @parameter right operand
  248. *   @result LeftOperand ^ RightOperand
  249. */
  250. double getExponentiationResult(double a, double b) {
  251.     return pow(a, b);
  252. }
  253.  
  254. /**
  255. *   Square root a number
  256. *
  257. *   @parameter operand
  258. *   @result The square root of the operand
  259. */
  260. double getSquareRoot(double a) {
  261.     return sqrt(a);
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement