Write in C++. Create a calculator that reads in commands from an input file call
ID: 3883226 • Letter: W
Question
Write in C++. Create a calculator that reads in commands from an input file called input.txt and performs the actions required by the commands. The program needs to consist of multiple functions. You need your main and at least two additional functions. You can have more than two if desired. You cannot use any global variables in your programs except for const values. All data that is needed by a called function will either have to already be part of that function or it will have to be passed as a parameter. If you need to pass in ifstream to a function you need to pass it as a non-const reference.
The general syntax of a commands are as follows:
action number
action
action text
The input command may contain whitespace before the action value. There must be whitespace separating action from number or from text. The text option is only used for comments (below). Anything after the number before the newline is ignored. If the command consists of only the action any text after action before the newline is ignored.
Here are the commands that must be supported by the calculator:
Command: Meaning
# comment The entire line of input is ignored
= number Assign value of number to the "accumulator"
-> Sore the accumulator value to the calculator’s memory
<- Set the accumulator to the current value in memory
+ number Add number to the value in the accumulator and put the new value in the accumulator
- number Subtract number from the value in the accumulator and save the new value in the accumulator
* number Similar to + and - but with multiplication
/ number Divide the accumulator by number with the result stored back to the accumulator. Output an error message if the number is 0.
<< Display the contents of the accumulator to cout with 5 digits to the right of the decimal point
S or s Take the square root of the accumulator with the result replacing the Accumulator. You must accept both s and S as value for the square root.
p number or P number Take accumulator to the number value, pow(accumulator, number), and update the accumulator with the result. Your program must accept both lower and upper case p/P for the power command.
Note that the actions here are +m +M -m -M etc. Note there is no whitespace between the + or - * or / and the m or M. Either m or M will indicate the calculators one memory variable. All of the memory commands (+m, +M, -m, -M, etc.) update the accumulator with the new result.
Command Meaning
+m or +M Add memory to the accumulator
-m or -M Subtract memory from the accumulator
*m or *M Multiply the accumulator by the memory value
/m or /M Divide the accumulator by the memory value. Output a message if division by zero is attempted.
Explanation / Answer
#include // Needed to perform IO operations using namespace std; int main() { // Program entry point coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.