/* Julie Kapsa Calculator Program to illustrate top down design File: calculat.cpp Lab 7 */ #include /* Input: Values and operations to be performed on these values. Output: The result of the calculations Purpose: to simulate a 6 function integer calculator Program. */ // function prototypes void print_description(); int get_first_value(); int get_second_value(); char get_operation(); int perform_calculation(int, int, char); int add(int, int); int subtract(int, int); int multiply(int, int); int exponent(int, int); /* Beginning of Main */ void main() { int value1 = 0, // first value value2 = 0, // second value result = 0; // result after operation performed char op = ' '; // operation to perform print_description(); value1 = get_first_value(); op = get_operation(); // Continue processing values until user indicates // ready for result while (op != '!') { value2 = get_second_value(); result = perform_calculation(value1, value2, op); op = get_operation(); value1 = result; } } /**************************************************************/ // Input: None // Output: To the screen // Purpose: Print the directions for the calculator void print_description() { cout << "\n\nThis program simulates a four function calculator."; cout << "\nPlease use only integer values."; cout << "\nValid operations are:"; cout << "\n\t'+' -- addition"; cout << "\n\t'-' -- subtraction"; cout << "\n\t'*' -- multiplication"; cout << "\n\t'^' -- exponentiation"; cout << "\n\t'!' -- exit program\n"; } // end print_description /**************************************************************/ // Input: None // Output: Returns updated value // Purpose: Prompt for the first value to perform the operation on. int get_first_value() { int value; cout << "\nEnter first value: "; cin >> value; return (value); } /**************************************************************/ // Input: Character representing the operation // Output: Updated operation // Purpose: Prompt the user and obtain valid operation char get_operation() { char operation; cout << "Enter operation (+,-,*,^,!): "; cin >> operation; // keep prompting until a valid operation is entered. while ((operation != '+') && (operation != '-') && (operation != '^') && (operation != '*') && (operation != '!')) { cout << "\nInvalid Operation. Please enter '+','-','^','!': "; cin >> operation; } return (operation); } // end get operation /**************************************************************/ // Input: Integer representing the second value // Output: Returns updated value // Purpose: Prompt for a second value to perform the operation on. int get_second_value() { int value; cout << "Enter value: "; cin >> value; return(value); } // end get_second_value /**************************************************************/ // Input: Two integer values and a character operation // Output: Integer result & print result to the screen // Purpose: Perform the given operation on the two values and // return the result. int perform_calculation(int val1, int val2, char operation) { int answer = 0; // local answer -- needs to be returned // perform the appropriate operation switch (operation) { case '+' : answer = add(val1, val2); break; case '-' : answer = subtract(val1, val2); break; case '*' : answer = multiply(val1, val2); break; case '^' : answer = exponent(val1, val2); } cout << "\nResult = " << answer << "\n"; return(answer); } // end perform_calculation /**************************************************************/ // Input: Two integer values // Output: Return result of adding the two numbers // Purpose: Perform addition on two numbers int add(int valu1, int valu2) { return (valu1 + valu2); } // end of add /**************************************************************/ // Input: Two integer values // Output: Return result of subtracting two numbers // Purpose: Subtract two numbers int subtract(int valu1, int valu2) { return(valu1 - valu2); } // end of subtract /**************************************************************/ // Input: Two integer values // Output: Return result of multiplying two numbers // Purpose: Multiply two nubers int multiply (int valu1, int valu2) { return(valu1 * valu2); } // end multiply /**************************************************************/ // Input: Two integer values // Output: Return result of raising 1st value to the // second value. // Purpose: Raise one value to another int exponent (int valu1, int valu2) { int answ = 1; // local answer // continue raising valu1 to the valu2 power // until the power becomes one while (valu2 >= 1) { answ *= valu1; valu2--; } return(answ); } // end of exponent