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

Use the C++ Standard Template Library’s stack class to write a program for proce

ID: 3594797 • Letter: U

Question

Use the C++ Standard Template Library’s stack class to write a program for processing a file of postfix expressions.

Recall that in a postfix expression, a binary operator comes after its two operands. Thus the infix expression (w + x) * (yz) becomes w x + y z – * in postfix.

A postfix expression can be efficiently evaluated using a stack as follows.

Read the expression from the left to the right.

When an operand is encountered, push it on the stack.

When an operator is encountered, pop the top two elements from the stack, perform the operation and then push the result.

There should be exactly one element on the stack at the end and this element is the value of the expression.

For example, if the expression 20 5 – 3 * is entered, the evaluation should proceed as follows.

Symbol read

20

5

3

*

Actions taken

Push 20

Push 5

Pop 5, Pop 20, Push 20 – 5

Push 3

Pop 3, Pop 15, Push 15 * 3

The result of 45 should then be output.

Your program should ask the user for the name of an input file. The input file will contain postfix expressions one per line. The program should open the file and evaluate the postfix expressions in the file. Your program should give the user the option of writing the results to an output file or to the screen.

Your program should also check the entered postfix expression for validity. A postfix expression is invalid if there is more than one element on the stack at the end of the evaluation or if ever there are not enough operands on the stack when an operation is performed.

E.g. If the input file contained:

20 5 – 3 *

100 50 60 + +

3 4 5 +

3 4 5 +

3 4 + +

3 4 ?

The output would be:

45

210

Too few operators.

Too few operators.

Too many operators.

Illegal operation

Limit your postfix expression to the arithmetic operations of +, –, *, and / on integer values.

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <sstream>
#include <stack>

using namespace std;


bool is_digits(const string &str)
{
return str.find_first_not_of("0123456789") == string::npos;
}

int main()
{
cout << "Enter input file: ";
string filename;
cin >> filename;
ifstream infile(filename.c_str());

string line;
while (getline(infile, line)) {
if (line.empty()) {
continue;
}
double result;
istringstream iss(line);
string s;
stack<double> op;
bool isFailed = false;
while(iss >> s) {
if (s == "*" || s =="/" || s == "+" || s == "-") {
  
double a, b;
if (op.empty()) {
isFailed = true;
cout << "Too many operators." << endl;
break;
}
a = op.top();
op.pop();
if (op.empty()) {
isFailed = true;
cout << "Too many operators." << endl;
break;
}
b = op.top();
op.pop();
  
if (s == "*") {
op.push(b*a);
} else if (s == "/") {
if (b == 0) {
isFailed = true;
cout << "invalid expression. Can not divide by zero" << endl;
break;
}
op.push(b/a);
} else if (s == "+") {
op.push(b + a);
} else if (s == "-") {
op.push(b-a);
}
}
else if (is_digits(s)) {
double a = atof(s.c_str());
op.push(a);
}
else {
isFailed = true;
cout << "Illegal operation" << endl;
}
}
if (!isFailed) {
if (op.empty() || op.size() > 1) {
cout << "Too few operators." << endl;
}
else {
cout << op.top() << endl;
op.pop();
}
}
}

return 0;
}

Sample run

Enter input file: postfix_exp.txt
45
210
Too few operators.
Too few operators.
Too many operators.
Illegal operation