Write a C++ program that uses a single stack that will read postfix expressions
ID: 3874847 • Letter: W
Question
Write a C++ program that uses a single stack that will read postfix expressions and prints their values. Each input expression will be on its own line as shown in the postfix.txt file. The output of each expression should be displayed on the console screen in an attractive manner. The program should terminate when the end of the file is reached.
Assume only binary operators and that the expression contain no variables. Note that you will need to use parentheses to indicate the order of application of the operators in the expression. For example:
Use the postfix.txt file to test your program. Be sure to set your program to read from this file.
The contents of postfix.txt:
78
78 6 +
78 6 + 9 2 - /
Input Output 78 78 78 6 + 84 78 6 + 9 2 - / 12 ( 78 + 6 ) / ( 9 - 2 )Explanation / Answer
Algorithms
step 1. Scan the expression from left to right.
step 2. If operand is seen, push stack and If an operator is seen, pop of top two elements an do the operation and push the result onto the stack.
Solution:
#include<iostream>
#include<stack>
#include <fstream>
#include <string>
using namespace std;
bool IsDigit(char data)
{
if(data >= '0' && data <= '9')
{
return true;
}
return false;
}
bool IsOperator(char data)
{
if(data == '+' || data == '-' || data == '*' ||data == '/')
{
return true;
}
return false;
}
int executeOperation(char operation, int operand1, int operand2)
{
if(operation == '+')
{
return operand1 +operand2;
}
else if(operation == '-')
{
return operand1 - operand2;
}
else if(operation == '*')
{
return operand1 * operand2;
}
else if(operation == '/')
{
return operand1 / operand2;
}
else
{
cout<<"Not a operand error ";
}
return -1;
}
// Function to evaluate Postfix expression and return output
int EvaluatePostfix(string expression)
{
stack<int> Sdata;
for(int i = 0;i< expression.length();i++) {
if(expression[i] == ' ' || expression[i] == ',')
{
continue;
}
else if(IsOperator(expression[i])) {
// Pop two operands.
int operand2 = Sdata.top();
Sdata.pop();
int operand1 = Sdata.top();
Sdata.pop();
// execute operation
int result = executeOperation(expression[i], operand1, operand2);
//Push the result on stack.
Sdata.push(result);
}
else if(IsDigit(expression[i])){
int operand = 0;
while(i<expression.length() && IsDigit(expression[i])) {
operand = (operand*10) + (expression[i] - '0');
i++;
}
i--;
// Push operand to stack.
Sdata.push(operand);
}
}
// If expression is in correct format, Stack will finally have one element. This will be the output.
return Sdata.top();
}
int main() {
int value=0 ;
string line;
ifstream myFile;
myFile.open("postfix.txt");
if (myFile.is_open()) {
while (!myFile.eof())
{
getline (myFile,line);
cout<<"Input is "<<line<<endl;
value= EvaluatePostfix(line);
cout<<"Output is "<<value<<endl;
}
cout<<endl;
}
return 0;
}
OUTPUT:
Input is 78
Output is 78
Input is 78 6 +
Output is 84
Input is 78 6 + 9 2 - /
Output is 12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.