/*************************************************************************** Name of program: Tom's Calculator v1.0 Name of file: calc.cpp What the program does: executes 8 different mathematical functions, 4 simple, 4 advanced Creator name: Tom Kralidis Date: October 22, 1998 Miscellaneous: Program begins with a start menu (9 options). After a selection is made, the user is prompted to enter a number (or numbers) to perform the chosen function. Following execution, the screen is cleared and the user is prompted back to the start menu if he/she chooses. The quit option automatically terminates the program, with no return to the main menu. ****************************************************************************/ #include #include #include /*************************************************************************** These are the function prototypes, always placed before main. They are user defined. Can be thought of as supplements to the libraries. ***************************************************************************/ addition(); subtraction(); multiplication(); division(); square(); squareroot(); inverse(); log(); quit(); int main() { int choice = 0; int pick; // This initializes choice to 0 do { cout <<"Welcome to Tom's Calculator v1.0\n\n"; cout <<"Choose your function from the following list\n\n"; cout << "\t1. addition \n"; cout << "\t2. subtraction \n"; cout << "\t3. multiplication \n"; cout << "\t4. division \n"; cout << "\t5. square \n"; cout << "\t6. squareroot \n"; cout << "\t7. natural log \n"; cout << "\t8. inverse \n"; cout << "\t9. quit \n"; cout << "\nWhat is your choice?\n\n"; cin >> choice; while ( choice < 1 || choice > 9 ) { cout << "\nIllegal choice, enter a number from 1 to 9\n\n"; cin >> choice; } /********************************************************************** Switch from variable of choices, the cases call the selected functions, and break to them ***********************************************************************/ switch (choice) { case (1): addition(); break; case (2): subtraction(); break; case (3): multiplication(); break; case (4): division(); break; case (5): square(); break; case (6): squareroot(); break; case (7): log(); break; case (8): inverse(); break; case (9): quit(); break; } if ( choice != 9 ) { cout << "\n\nBack to the Main Menu? \n\t[1] Yes\n\t[2] No\n\t"; cin >> pick; if ( pick == 2 ) { quit(); return 0; } } } while ( choice != 9 ); return 0; } /************************************************************************* The functions below are the eight mathematical functions and the quit function. **************************************************************************/ addition() { float num1, num2, sum; cout << "\nWelcome to Tom's Addition!\n\n"; cout << "Enter first integer:\n\n"; cin >> num1; cout << "\nEnter second integer:\n\n"; cin >> num2; sum = num1 + num2; cout << "\nThe sum is " << sum << "\n" << endl; return 0; } subtraction() { float num1, num2, difference; cout << "\nWelcome to Tom's Subtraction!\n\n"; cout << "Enter first integer:\n\n"; cin >> num1; cout << "\nEnter second integer:\n\n"; cin >> num2; difference = num1 - num2; cout << "\nThe difference is " << difference << "\n" << endl; return 0; } multiplication() { float num1, num2, product; cout << "\nWelcome to Tom's Multiplication!\n\n"; cout << "Enter first integer:\n\n"; cin >> num1; cout << "\nEnter second integer:\n\n"; cin >> num2; product = num1 * num2; cout << "\nThe product is " << product << "\n" << endl; return 0; } division() { float num1, num2, quotient; cout << "\nWelcome to Tom's Division!\n\n"; cout << "Enter first integer:\n\n"; cin >> num1; cout << "\nEnter second integer:\n\n"; cin >> num2; while ( num2 == 0) { cout << "\nCannot divide by zero, choose another number:\n\n"; cin >> num2; } quotient = num1 / num2; cout << "\nThe quotient is " << quotient << "\n" << endl; return 0; } square() { float num1, square; cout << "\nWelcome to Tom's Squares!\n\n"; cout << "Enter an integer:\n\n"; cin >> num1; square = num1 * num1; cout << "\nThe square of " << num1 << " is " << square << "\n" << endl; return 0; } /************************************************************************ This begins the functions that work off the file, declared at the start of this program. The assignment of sqrt(num1) runs off the this header file *************************************************************************/ squareroot() { double num1, sqroot; cout << "\nWelcome to Tom's Square Root!\n\n"; cout << "Enter an integer\n\n"; cin >> num1; sqroot = sqrt(num1); cout << "\nThe square root of " << num1 << " is " << sqroot << "\n" << endl; return 0; } log() { double num1, logg; cout << "\nWelcome to Tom's Log of a Number!\n\n"; cout << "Enter an integer\n\n"; cin >> num1; while ( num1 == 0) { cout << "\nEnter an integer other than zero:\n\n"; cin >> num1; } logg = log(num1); cout << "The answer is " << logg << endl; return 0; } inverse() { double num1, inverse; cout << "\nWelcome to Tom's Inverse of a Number!\n\n"; cout << "Enter an integer\n\n"; cin >> num1; while ( num1 == 0) { cout << "\nEnter an integer other than zero:\n\n"; cin >> num1; } inverse = 1.0/num1; cout << "The answer is " << inverse << endl; return 0; } quit() { cout << "\nThank you for trying Tom's Calculator v1.0!!\n\n"; cout << "Comments, bugs and general info on this software can be sent to:\n\n"; cout << "\tTom Kralidis\n\t(613) 233-4921\n\ttommy@storm.ca\n\n"; return 0; }