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

So I have a program that I need to work on, but I\'m a bit confused on the Rever

ID: 3785477 • Letter: S

Question

So I have a program that I need to work on, but I'm a bit confused on the Reverse Polish Notation so this program is a bit hard for me, can someone help me write the program with explanations on what each method does so I can figure out how it works.. Thank you so much in advance
____________________________________________________________________________________________________________________________________________________________

For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user.

You must use a linked list to maintain the stack for this program (array implementations of the stack will not receive full credit).

You must handle the following situations (errors):
Too many operators (+ - / *)
Too many operands (doubles)
Division by zero

The program will take in a Polish expression that separates the operators and operands by a single space, and terminates the expression with an equals sign.

The program will continue to take and evaluate expressions until the user enters a zero (0) on a line by itself followed by a new line.

Your sample output should show the handling of all the error conditions as well as make use of all of the operators.

Sample IO: (note: formatting of output isn’t a critical issue)
Input Output
10 15 + = 25
10 15 - = -5
2.5 3.5 + = 6 (or 6.0)
10 0 / = Error: Division by zero
10 20 * / = Error: Too many operators
12 20 30 / = Error: Too many operands
-10 -30 - = 20
100 10 50 25 / * - -2 / = -40

Explanation / Answer

#include<iostream>
#include<iomanip>
#include<string>
#include<sstream>

using namespace std;

class Node
{
double data;
Node *top;
Node *ptr;
public:
Node()
{
top = NULL; //top pointer will point top element
ptr = NULL;
}

bool isEmpty()
{
return top == 0;
}

void pushValue(double val)
{
Node *next = new Node;
next->data = val;
next->ptr = top;
top = next;
}

double popVal()
{
if (isEmpty())
{
cout << "Error: Too many operators" << endl;
}
else
{
Node *next = top->ptr;
double ret = top->data;
delete top;
top = next;
return ret;
}

}
//now it Displays the answer of the equation
void print()
{
cout << "= " << top->data << endl;
}
};

bool isOperator(const string& input)
{
string ops[] = { "+", "-", "*", "/" };
for (int i = 0; i < 4; i++)
{
if (input == ops[i])
{
return true;
}
}
return false;
}
//here This function will tells the operators what to do with the values.
void performOp(const string& input, Node& stack)
{
double Val1, Val2;
int errorCheck = 0;

Val1 = stack.popVal();
Val2 = stack.popVal();

if (input == "+")
{
stack.pushValue(Val1 + Val2);
}
else if (input == "-")
{
stack.pushValue(Val1 - Val2);
}
else if (input == "*")
{
stack.pushValue(Val1 * Val2);
}
else if (input == "/" && Val2 != 0)
{
stack.pushValue(Val1 / Val2);
}

if (input == "/" && Val2 == 0)
{
cout << "Error: Division by zero" << endl;
errorCheck = 1;
}

if (errorCheck == 0)
{
stack.print();
}
}

int main()
{
cout << "Reverse Polish Notation Calculator!" << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << "Enter your values followed by your operators(Enter 0 to exit)" << endl;

string input;
Node stack;
//it Checks the user's input to see which function to use.
while (true)
{
cin >> input;
double num;

if (stringstream(input) >> num)
{
stack.pushValue(num);
}
else if (isOperator(input))
{
performOp(input, stack);
}
else if (input == "0")
{
return 0;
}
}
}

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