Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in C++. Please make sure that the answer is readable. If you already answe

ID: 3884244 • Letter: W

Question

Write in C++. Please make sure that the answer is readable. If you already answered, please don't take on this question again because it's not working. Thank you. 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

Given below is the program and output. Hope it helps. If it did, please don't forget to rate the answer. Thank you very much.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
void processFile(ifstream &infile);
int main()
{
string filename;
cout << "Enter input filename: ";
cin >> filename;
  
ifstream infile(filename.c_str());

if(!infile.is_open())
{
cout << "Error opening input file - " << filename << endl;
return 1;
}
cout << fixed << setprecision(5);
processFile(infile);
cout << "Good Bye!!!" << endl;
  
}

void processFile(ifstream &infile)
{
string command;
double num;
string restOfLine;
double accumulator = 0, memory = 0;
  
while(infile >> command)
{
if(command == "#")
; //do nothing for comments
else if(command == "=") //assign number to acc
{
infile >> num;
accumulator = num;
}
else if(command == "->") //store acc to M
memory = accumulator;
else if(command == "<-")
accumulator = memory;
else if(command == "+") //add num to acc
{
infile >> num;
accumulator += num;
}
else if(command == "-") //sub num to acc
{
infile >> num;
accumulator -= num;
}
else if(command == "*") //mul num to acc
{
infile >> num;
accumulator *= num;
}
else if(command == "/") //div num to acc
{
infile >> num;
if(num == 0)
cout << "ERROR: division by zero attempted" << endl;
else
accumulator /= num;
}
else if(command == "<<")
cout << accumulator << endl;
else if(command == "S" || command == "s")
accumulator = sqrt(accumulator);
else if(command == "P" || command == "p")
{
infile >> num;
accumulator = pow(accumulator, num);
}
else if(command == "+m" || command == "+M") //add M to acc
accumulator += memory;
else if(command == "-m" || command == "-M") //sub M to acc
accumulator -= memory;
else if(command == "*m" || command == "*M") //mul M to acc
accumulator *= memory;
else if(command == "/m" || command == "/M") //div M to acc
{
if(memory == 0)
cout << "ERROR: division by zero attempted" << endl;
else
accumulator /= memory;
}
  

//ignore rest of line after processing command
getline(infile, restOfLine);
}
  
infile.close();
}

input file: calc.txt

<<
# adding 2, 4 ,6
+ 2
+ 4
+ 6
# result should be 12
<<
# store acc to mem
->
= 5
+M
# should show 17
<<
- 8
# should show 9
s
# should show 3
<<
p 3 #raise to power 3, should show 27
<<
/ 5
<<

output

$ g++ calculator.cpp
$ ./a.out
Enter input filename: calc.tx
Error opening input file - calc.tx

$ ./a.out
Enter input filename: calc.txt
0.00000
12.00000
17.00000
3.00000
27.00000
5.40000
Good Bye!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote