(C++)Please implement the function evaluatePostfix based upon the algorithm give
ID: 3691642 • Letter: #
Question
(C++)Please implement the function evaluatePostfix based upon the algorithm given in the code...
int evaluatePostfix (string postFix)
{
/* algorithm:
Declare a stack of int to store the operands, and also the result of the subexpression
scan the expression from left to right, char by char
i) if the char is a digit, push the digit's value into the stack...furt
hermore, loop to read digits until you reach space or operator, and meanwhile finds out the values of the operand...
ii) if the char is an operand, pop two elements from the stack
and apply the operation on the two elements,
push the result onto the stack
when reaching the end of the expression, the value in the stack is the result */
}
Explanation / Answer
/* Evaluation Of postfix Expression in C++ Input Postfix expression must be in a desired format. Operands must be integers and there should be space in between two operands. Only '+' , '-' , '*' and '/' operators are expected. */ #include #include #include using namespace std; // Function to evaluate Postfix expression and return output int EvaluatePostfix(string expression); // Function to perform an operation and return output. int PerformOperation(char operation, int operand1, int operand2); // Function to verify whether a character is operator symbol or not. bool IsOperator(char C); // Function to verify whether a character is numeric digit. bool IsNumericDigit(char C); int main() { string expression; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.