Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <iomanip>
- using namespace std;
- //Define MathOperator as a function that requires 2 doubles to be passed.
- //This will allow me to store all my 2 operand functions into an array to avoid
- //unneeded switch/if statements
- typedef double (*MathOperator) (double a, double b);
- //Declare all my functions
- double getAdditionResult(double a, double b);
- double getMultiplicationResult(double a, double b);
- double getSubtractionResult(double a, double b);
- double getDivisionResult(double a, double b);
- double getExponentiationResult(double a, double b);
- double getSquareRoot(double a);
- //Store my operator list into a char array for display purposes later in the program
- char operatorList[] = {
- '+', '-', '*', '/', '^'
- };
- //Constants for the teaching mode and calculator mode
- //Yes I know calculator mode is not used in the program,
- //but a defined constant should be here regardless.
- const int MODE_TEACHING = 1;
- const int MODE_CALCULATOR = 2;
- /**
- * Psuedocode:
- * Initialize variables that will be used throughout the main method
- * -Create an array of 2 operand functions that can be used later on
- * Print out the menu of possible operations for the user
- * Request that the user select an operation
- * -Require the user to input a number between 1 and 6 for the operations
- * Display the possible modes for the user to select
- * -1 for teaching, 2 for calculator
- * -Require the user to select 1 or 2
- *
- * If the selected operation is the square root operation:
- * Ask the user to input the number to be square rooted
- * If the mode is in teaching mode ask the user to input what they think is the answer
- * -If they are right, print out congratulations and terminate the program
- * -If they are wrong, tell them they are wrong. The program will print out the answer later
- * Print out the solution and terminate the program
- *
- * Otherwise,
- * Tell the user they are performing a 2 operator function in the format A + B, A - B, ect.
- * Require the user to input the A and B operands
- * Calculate the result
- * If the mode is in teaching
- * Ask the user to input what they think is the right answer
- * -If they are right display a congratulations message and terminate the program
- * -If they are wrong then tell them they are wrong. The program will print out the answer later
- * Print out the operation being performed. Ex. 8 + 4
- * Print out the result of the answer and terminate the program.
- * */
- int main()
- {
- //Initialization of variables that will be used throughout the program
- int selectedOperation;
- int selectedMode;
- double leftOperand;
- double rightOperand;
- double inputtedGuess;
- double result;
- //Store all my 2 operand functions into an array of "MathOperator" that was typedef'd above
- MathOperator operators[] = {
- getAdditionResult,
- getSubtractionResult,
- getMultiplicationResult,
- getDivisionResult,
- getExponentiationResult,
- };
- //Print out the operations list that the user can select
- cout << "Operations List\n";
- cout << "\t1. (+) Addition\n";
- cout << "\t2. (-) Subtraction\n";
- cout << "\t3. (*) Multiplication\n";
- cout << "\t4. (/) Division\n";
- cout << "\t5. (a^b) Exponentiation\n";
- cout << "\t6. (sqrt(a)) Square Root\n";
- //Request that the user select an operation and require them to pick a number between 1 and 6
- cout << "Please select an operation to use(1-6): ";
- cin >> selectedOperation;
- while (selectedOperation < 1 || selectedOperation > 6) {
- cout << "Please select an appropriate operation between 1 and 6: ";
- cin >> selectedOperation;
- }
- //Print out the possible modes the user can select.
- cout << "Modes\n";
- cout << "\t1. Teaching Mode\n";
- cout << "\t2. Calculator Mode\n";
- //Request that the user selects a mode and require them to select either 1 or 2.
- cout << "Please select a mode to use: ";
- cin >> selectedMode;
- while (selectedMode < 1 || selectedMode > 2) {
- cout << "Please select either 1 or 2 for your teaching mode: ";
- cin >> selectedMode;
- }
- /**********
- * Single operand function
- ***********/
- //Sqrt is the only operator that requires 1 variable
- //Manually perform this operation since it is the only 1 operand function
- if (selectedOperation == 6) {
- //Request the user to enter a number to square root
- cout << "Enter a number to square root: ";
- cin >> leftOperand;
- //If this number is less than 0, the result is an imaginary number. We are not calculating this.
- //Alert the user and terminate the program.
- if (leftOperand < 0) {
- cout << "Cannot calculate the square root of a negative number.\n";
- return 0;
- }
- //Store the result into 'result'
- result = getSquareRoot(leftOperand);
- //If the selected mode is teaching
- if (selectedMode == MODE_TEACHING) {
- //Ask the user what they think the answer is and store it into inputted guess
- cout << "What do you think the answer is?: ";
- cin >> inputtedGuess;
- //If they are right the display the congrats message and terminate the program
- //Otherwise tell the user they are wrong.
- if (inputtedGuess == result) {
- cout << "Congrats! You got the answer right!\n";
- return 0;
- } else {
- cout << "Sorry, that was not the correct answer.\n";
- }
- }
- //Display the result and terminate the program after flushing cout.
- cout << "The result is: " << result << endl;
- return 0;
- }
- /**********
- * Double operand function
- ***********/
- //Tell the user they are performing a 2 operand function
- cout << "You are performing a 2 operand function. This is the format\n";
- //Display the format. Ex. A + B, A - B, ect based off what they selected earlier
- cout << "\tA " << operatorList[selectedOperation - 1] << " B\n";
- //Request the user to input A and B
- cout << "Please input the values of A and B.\n";
- cout << "A: ";
- cin >> leftOperand;
- cout << "B: ";
- cin >> rightOperand;
- //Using the array of functions created earlier, I can call a specific function
- //based off what they selected and pass my 2 operands into the function
- //and store the result into 'result'.
- result = operators[selectedOperation-1](leftOperand, rightOperand);
- //If the selected mode is teaching
- if (selectedMode == MODE_TEACHING) {
- //Request the user to input their guess
- cout << "What do you think the result is?: ";
- cin >> inputtedGuess;
- //If they are right the display the congrats message and terminate the program
- //Otherwise tell the user they are wrong.
- if (inputtedGuess == result) {
- cout << "Congrats! You got the answer right!\n";
- return 0;
- } else {
- cout << "Sorry, that was not the correct answer.\n";
- }
- }
- //Print out the operation the user is doing. Ex. 9 - 4
- //Then print out the result and flush cout.
- cout << "Operation: " << leftOperand << " " << operatorList[selectedOperation - 1] << " " << rightOperand << "\n";
- cout << "The result is: " << result << endl;
- return 0;
- }
- /**
- * Get the result of 2 added operands
- *
- * @parameter left operand
- * @parameter right operand
- * @result LeftOperand + RightOperand
- */
- double getAdditionResult(double a, double b) {
- return a + b;
- }
- /**
- * Get the result of 2 subtracted operands
- *
- * @parameter left operand
- * @parameter right operand
- * @result LeftOperand - RightOperand
- */
- double getSubtractionResult(double a, double b) {
- return a - b;
- }
- /**
- * Get the result of 2 divided operands
- *
- * @parameter left operand
- * @parameter right operand
- * @result LeftOperand / RightOperand
- */
- double getDivisionResult(double a, double b) {
- return a / b;
- }
- /**
- * Get the result of 2 multiplied operands
- *
- * @parameter left operand
- * @parameter right operand
- * @result LeftOperand * RightOperand
- */
- double getMultiplicationResult(double a, double b) {
- return a * b;
- }
- /**
- * Get the result of an exponentiated number
- *
- * @parameter left operand
- * @parameter right operand
- * @result LeftOperand ^ RightOperand
- */
- double getExponentiationResult(double a, double b) {
- return pow(a, b);
- }
- /**
- * Square root a number
- *
- * @parameter operand
- * @result The square root of the operand
- */
- double getSquareRoot(double a) {
- return sqrt(a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement