new programer, help this question! thank you!
For this problem, you are going to construct a very simple calculator program in a file called Calculator.cpp that can just add, subtract, multiply, and divide. You will allow the user to enter sets of three "inputs". The first and third input will be numbers, and the middle input will be one of the mathematical operators +, -, *, or / . Depending upon what the user inputs, you will perform the appropriate mathematical operation, using a function. You will write five functions: add, subtract, multiply, divide, and print.
The input lines will look like the following (entered one at a time as shown on the right):
4.2 + 88
321.6 - 95.1
18 * 9
10 / 100
Note: for the last example above, return the value that you would expect from a calculator, which is NOT the result of integer division in C++!
Each of the add, subtract, multiply, and divide functions should take two doubles as their parameters, and should return the result of the calculation as a double. The print function should return nothing, and should take the original two operands as doubles, the operator as a char, and the result as a double, and should print the following (corresponding to the examples above):
4.2 + 88 = 92.2
321.6 - 95.1 = 226.5
18 * 9 = 162
10 / 100 = 0.1
If you don't write the functions, you get no points (the goal of this problem is to learn how to write functions...). You must write function prototypes before the main function, and then place the function implementations after main. Not writing prototypes will cost you 10 points. You must write file and function comments as specified in the class coding standards. Not doing that correctly will cost you up to 10 points (if you do no commenting at all). You must have a loop in your main program to allow the user to keep calculating until they want to quit. See the example to the right. Do not call print from within the math functions. Call it from main after returning the math result back from each function. Do not print from within the math functions.
Note: The division function needs to be special, since you can get into trouble in one special case. See the example session. Return zero from the function in this case. That isn't really right, but it would be complicated to do otherwise right now...
You don't have to worry about trapping errors where the user doesn't enter X op Y expressions, where X and Y are numbers, and op is a single character. You also don't have to trap errors where they enter something other than an integer to continue the program. You may change the prompt or method for continuing the program if you like. But make it obvious and explicit to the user what you want.
--------------------------------------------------------------------------------
Problem 2: Calculator Version 2 - real math (60 pts)
For this problem make sure that you've got Problem 1 done, since you will start with it for this problem. You're going to add a nicer menu to it, and add some functionality that a calculator really should have...
Copy the solution from Problem 1 into a file called Calculator2.cpp. First, you will add a menu of choices for the user. Really, once we learn more about reading, parsing, and manipulating strings, it would be nicer just to allow the user to enter expressions and have the program figure out what to do, but for now, we need to have the user tell us up-front what they are going to enter. So the menu will have three options: 1) enter two argument expression, 2) Enter one argument expression, and 3) Quit program. The user will enter 1, 2, or 3 to choose. See the example to the right. You will write a function to process the menu and user choice.
If the user chooses to enter a two-argument expression, then you should read from the console just as in problem 1, and perform exactly the same calculations. However, you will add one more:
5 ^ 3
For which you should print:
5 ^ 3 = 125
So the ^ operator will represent the "power" operator (so the example means "5 to the 3rd power"). You should support any two double operands. A math library function will probably help you here...
If the user asks for choice 2 (one argument expression), you should support the following operator-value combinations:
S 2
Should result in: The square root of 2 = 1.414
E 5
Should result in: The exponential of 5 = 148.413
and
L 2
Should result in: The natural logarithm of 2 is 0.693147
So the operators are the upper case letters S, E, and L, and the arguments follow, and can be appropriate double values. Note that you can't take the square root of a negative number, and you can't take the natural logarithm of any non-positive numbers (including zero). Make sure you protect your program from these conditions. See the figure to the right for a session of my program. You must meet the following architectural requirements:
You must have a menu function that presents the menu and get's the user's choice. You should make sure that it will only return valid choices.
The main program should ask the user for either the two-operand or one-operand data, and then call one of two processArgs functions (see below). Don't do much processing in main. It should then call one of the two overloaded print functions (see below).
You must write two overloaded processArgs functions. One should take the operator and two calculation arguments, and return the result of the calculation through its return parameter. The other should take the operator and one operator argument, and return the result of the calculation through its return parameter. The first version should process two-argument operators, and the second version should process one-argument operators.
You must write two overloaded print functions. One version, you already have from problem 1. The second must also be named print, but should take one operator, the operator argument, and the result value, and should print the square root, exponential, or natural logarithm lines, depending on what the operator is.
To calculate the powers, square roots, exponential function, and logarithm, you need to call C++ math library functions (look them up). Make sure to check arguments when necessary to ensure that they are valid (i.e., don't allow taking the square root of a negative number). Print appropriate error messages. Never quit the program until the user asks to quit.
You must comment all functions correctly. Document purpose, parameters, and return values (if any).
Make sure your program input/output is at least as well formatted as in the example session. Sloppy input/output will lose points.
|