C++ question. Please give me a complete code. We normally write arithmetical exp
ID: 3775385 • Letter: C
Question
C++ question. Please give me a complete code.
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<double> (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.
Explanation / Answer
#include<iostream>
#include<string>
#include<stack>
using namespace std;
float postfixEval(string expression); // function to evaluate postfix expression
float calculate(char operation, float operand1, float operand2);
bool isOP(char C); // Function to check character is operator symbol or not.
bool isNUM(char C); // Function to check whether a character is numeric digit.
int main()
{
string expr;
cout<<"Enter Your Postfix expression:";
getline(cin,expr);
float result = postfixEval(expr);
cout<<"Output = "<<result<<" ";
}
// Function to evaluate Postfix expression
float postfixEval(string expr)
{
//Stack from STL
stack<float> Stk;
for(float i = 0;i< expr.length();i++) {
if(expr[i] == ' ' || expr[i] == ',') continue;
else if(isOP(expr[i])) {
float operand2 = Stk.top(); Stk.pop();
float operand1 = Stk.top(); Stk.pop();
float result = calculate(expr[i], operand1, operand2);
Stk.push(result);
}
else if(isNUM(expr[i])){
float operand = 0;
while(i<expr.length() && isNUM(expr[i])) {
operand = (operand*10) + (expr[i] - '0');
i++;
}
i--;
Stk.push(operand);
}
}
return Stk.top();
}
// checking numeric or not
bool isNUM(char Ch)
{
if(Ch >= '0' && Ch <= '9') return true;
return false;
}
// checking operator or not.
bool isOP(char Ch)
{
if(Ch == '+' || Ch == '-' || Ch == '*' || Ch == '/')
return true;
return false;
}
float calculate(char operation, float operand1, float operand2)
{
if(operation == '+') return operand1 +operand2;
else if(operation == '-') return operand1 - operand2;
else if(operation == '*') return operand1 * operand2;
else if(operation == '/') return operand1 / operand2;
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.