
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Evaluation of Prefix Expressions in C++
In this article, we will discuss the evaluation of prefix Expression.
Prefix Expression
In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.
Example:
* + 6 9 - 3 1
Prefix expressions are evaluated faster than infix expressions. Also, there are no brackets in prefix expressions which make it evaluate quicker.
Algorithm to evaluate Prefix Expression:
The evaluation of prefix expression requires a stack data structure. We will push the operators in the stack and then solve the expression.
We will visit each element of the expression one by one. If the current element is an operand, we will push it to the stack. And if it is an operator, we will pop two operands, perform the operation, operand operator operand and then push the result back to the stack.
Algorithm −
Step 1: Start from the last element of the expression.
Step 2: check the current element.
Step 2.1: if it is an operand, push it to the stack.
Step 2.2: If it is an operator, pop two operands from the stack. Perform the operation and push the elements back to the stack.
Step 3: Do this till all the elements of the expression are traversed and return the top of stack which will be the result of the operation.
Lets see the working of our algorithm to solve an prefix expression,
Prefix Expression :
* + 6 9 - 3 1
Iteration : 1
Element scanned => 1
Operation => push to stack
Stack => 1
Iteration : 2
Element scanned => 3
Operation => push to stack
Stack => 3 , 1
Iteration : 3
Element scanned => -
Operation => pop two from stack, perform operation and push back the result.
3 - 1 = 2
Stack => 2
Iteration : 4
Element scanned => 9
Operation => push to stack
Stack => 9 , 2
Iteration : 5
Element scanned => 6
Operation => push to stack
Stack => 6, 9 , 2
Iteration : 6
Element scanned => +
Operation => pop two from stack, perform operation and push back the result.
6 + 9 = 15
Stack => 15 , 2
Iteration : 7
Element scanned => *
Operation => pop two from stack, perform operation and push back the result.
15 * 2 = 30
Stack => 30
End => return top of stack, result = 30.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; double evaluatePrefix(string prefixExp) { stack<double> operendStack; int size = prefixExp.size() - 1; for (int i = size; i >= 0; i--) { if (isdigit(prefixExp[i])) operendStack.push(prefixExp[i] - '0'); else { double o1 = operendStack.top(); operendStack.pop(); double o2 = operendStack.top(); operendStack.pop(); if( prefixExp[i] == '+') operendStack.push(o1 + o2); else if( prefixExp[i] == '-') operendStack.push(o1 - o2); else if( prefixExp[i] == '*') operendStack.push(o1 * o2); else if( prefixExp[i] == '/') operendStack.push(o1 / o2); else{ cout<<"Invalid Expression"; return -1; } } } return operendStack.top(); } int main() { string prefixExp = "*+69-31"; cout<<"The result of evaluation of expression "<<prefixExp<<" is "<<evaluatePrefix(prefixExp); return 0; }
Output −
The result of evaluation of expression *+69-31 is 30
- Related Articles
- Explain the evaluation of expressions of stacks in C language
- Prefix and Postfix Expressions in Data Structure
- Evaluation of Boolean expression
- Evaluation of Expression Tree in C++
- Evaluation order of operands in C++
- Evaluation of Risk in Investments in C++
- Order of evaluation in C++ function parameters
- Short-circuit evaluation in JavaScript
- ICT in Assessment and Evaluation
- Alternative Evaluation In Buying Decisions
- What is evaluation order of function parameters in C?
- What is the evaluation of Association Patterns?
- Psychodiagnostics Evaluation and Assessment
- K Prefix in Python
- What are the Stages of Evaluation in Credit Analysis Process?
