In: Computer Science
We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in "4 + 5". In postfix notation, the operator appears after its operands, as in "4 5 +". Here is a slightly more complex postfix expression: "25 12 7 - 2 * /". The equivalent infix expression is: "25 / ((12 - 7) * 2)". The result of that expression should be 2.5 (beware integer division). Postfix expressions don't require parentheses.
Write a function named postfixEval that uses a stack (from the Standard Template Library) to evaluate postfix expressions. It should take a C-style string parameter that represents a postfix expression. The only symbols in the string will be +, -, *, /, digits and spaces. '+' and '-' will only appear in the expression string as binary operators - not as unary operators that indicate the sign of a number. The return type should be double. You may find the isdigit() function useful in parsing the expression. You may also use strtok() and atof().
Hint: Read a postfix expression from left to right. When you read a number, push it on the stack. When you read an operand, pop the top two numbers off the stack, apply the operator to them, and push the result on top of the stack. At the end, the result of the expression should be the only number on the stack.
**File must be called: postfixEval.cpp**
This code implements postfix evaluation. It takes an input and provides the calculated result in double. I hope this surely helps you. Do rate me with a thumbs up.
//CODE in C++:-
#include<iostream>
#include<stack>
#include<string>
using namespace std;
// This function evaluates Postfix expression and
returns output
double postfixEval(string expression);
// This function performs an operation on two operands
and returns output.
double PerformOperation(char operation, double operand1, double
operand2);
// This function verifies whether a character is an
operator symbol or not.
bool IsOperator(char C);
// This function verifies whether a character is numeric
digit.
bool IsNumericDigit(char C);
int main()
{
string expression;
cout<<"Enter Postfix Expression \n";
getline(cin,expression);
double result = postfixEval(expression);
cout<<"Output =
"<<result<<"\n";
}
// this function evaluates the given Postfix expression
and returns output
double postfixEval(string expression)
{
// Declaring a Stack from Standard template library in
C++.
stack<double> S;
for(int i = 0;i< expression.length();i++) {
// Scanning each
character from left.
// If character is a delimitter,
simply move on.
if(expression[i] == ' ' ||
expression[i] == ',') continue;
// If character is
operator, pop two elements from stack, perform operation and push
the result back.
else if(IsOperator(expression[i]))
{
// Pop two
operands.
double operand2
= S.top(); S.pop();
double operand1
= S.top(); S.pop();
// Perform
operation
double result =
PerformOperation(expression[i], operand1, operand2);
//Push back
result of operation on stack.
S.push(result);
}
else
if(IsNumericDigit(expression[i])){
// Extract the
numeric operand from the string
// Keep
incrementing i as long as you are getting a numeric digit.
double operand =
0;
while(i<expression.length() &&
IsNumericDigit(expression[i])) {
// For a number with more than one digits, as we
are scanning from left to right.
// Everytime , we get a digit towards right, we
can multiply current total in operand by 10
// and add the new digit.
operand = (operand*10) + (expression[i] -
'0');
i++;
}
// Finally, you
will come out of while loop with i set to a non-numeric character
or end of string
// decrement i
because it will be incremented in increment section of loop once
again.
// We do not
want to skip the non-numeric character by incrementing i
twice.
i--;
//
Push operand on stack.
S.push(operand);
}
}
// If expression is in correct format, Stack will
finally have one element. This will be the output.
return S.top();
}
// Function to verify whether a character is numeric
digit.
bool IsNumericDigit(char C)
{
if(C >= '0' && C <= '9') return
true;
return false;
}
// Function to verify whether a character is operator
symbol or not.
bool IsOperator(char C)
{
if(C == '+' || C == '-' || C == '*' || C == '/')
return true;
return false;
}
// Function to perform an operation and return
output.
double PerformOperation(char operation, double operand1, double
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<<"Unexpected Error
\n";
return -1;
}
// output:-
Enter Postfix Expression 25 12 7 - 2 * / Output = 2.5