Open In App

Arithmetic Expression Evaluation

Last Updated : 19 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The stack organization is very effective in evaluating arithmetic expressions. Expressions are usually represented in what is known as Infix notation, in which each operator is written between two operands (i.e., A + B). With this notation, we must distinguish between ( A + B )*C and A + ( B * C ) by using either parentheses or some operator-precedence convention. Thus, the order of operators and operands in an arithmetic expression does not uniquely determine the order in which the operations are to be performed. 

1. Polish notation (prefix notation) – 
It refers to the notation in which the operator is placed before its two operands. Here no parentheses are required, i.e., 

+AB 

2. Reverse Polish notation(postfix notation) – 
It refers to the analogous notation in which the operator is placed after its two operands. Again, no parentheses is required in Reverse Polish notation, i.e., 

AB+ 

Stack-organized computers are better suited for post-fix notation than the traditional infix notation. Thus, the infix notation must be converted to the postfix notation. The conversion from infix notation to postfix notation must take into consideration the operational hierarchy. 

There are 3 levels of precedence for 5 binary operators as given below: 

Highest: Exponentiation (^)
Next highest: Multiplication (*) and division (/)
Lowest: Addition (+) and Subtraction (-) 

For example – 

Infix notation: (A-B)*[C/(D+E)+F]
Post-fix notation: AB- CDE +/F +* 

Here, we first perform the arithmetic inside the parentheses (A-B) and (D+E). The division of C/(D+E) must be done prior to the addition with F. After that multiply the two terms inside the parentheses and bracket. 

Now we need to calculate the value of these arithmetic operations by using a stack. 

The procedure for getting the result is: 

  1. Convert the expression in Reverse Polish notation( post-fix notation). 
  2. Push the operands into the stack in the order they appear. 
  3. When any operator encounters then pop two topmost operands for executing the operation. 
  4. After execution push the result obtained into the stack. 
  5. After the complete execution of expression, the final result remains on the top of the stack. 

For example – 

Infix notation: (2+4) * (4+6)
Post-fix notation: 2 4 + 4 6 + *
Result: 60 

The stack operations for this expression evaluation is shown below: 

 


Previous Article
Next Article

Similar Reads

Expression Evaluation
Evaluate an expression represented by a String. The expression can contain parentheses, you can assume parentheses are well-matched. For simplicity, you can assume only binary operations allowed are +, -, *, and /. Arithmetic Expressions can be written in one of three forms: Infix Notation: Operators are written between the operands they operate on
15 min read
Evaluation of Postfix Expression
Given a postfix expression, the task is to evaluate the postfix expression. Postfix expression: The expression of the form "a b operator" (ab+) i.e., when a pair of operands is followed by an operator. Examples: Input: str = "2 3 1 * + 9 -"Output: -4Explanation: If the expression is converted into an infix expression, it will be 2 + (3 * 1) - 9 = 5
15+ min read
Evaluation of Expression Tree
Given a simple expression tree, consisting of basic binary operators i.e., + , - ,* and / and some integers, evaluate the expression tree. Examples: Input: Root node of the below tree Output:100 Input: Root node of the below tree Output: 110 Recommended PracticeExpression TreeTry It! Approach: The approach to solve this problem is based on followin
9 min read
Building Expression tree from Prefix Expression
Given a character array a[] represents a prefix expression. The task is to build an Expression Tree for the expression and then print the infix and postfix expression of the built tree. Examples: Input: a[] = "*+ab-cd" Output: The Infix expression is: a + b * c - d The Postfix expression is: a b + c d - *Input: a[] = "+ab" Output: The Infix express
8 min read
Convert Infix expression to Postfix expression
Write a program to convert an Infix expression to Postfix form. Infix expression: The expression of the form "a operator b" (a + b) i.e., when an operator is in-between every pair of operands.Postfix expression: The expression of the form "a b operator" (ab+) i.e., When every pair of operands is followed by an operator. Examples: Input: A + B * C +
13 min read
Evaluation of Prefix Expressions
Prefix and Postfix expressions can be evaluated faster than an infix expression. This is because we don't need to process any brackets or follow operator precedence rule. In postfix and prefix expressions which ever operator comes before will be evaluated first, irrespective of its priority. Also, there are no brackets in these expressions. As long
13 min read
Evaluation of Risk in Investments
Given two investment options A and B, we have to find the less risky investment of the two. The two investments A and B are each represented by an array. Each element in the array is a probable investment outcome. Thus each element in the array is a pair of two values. The first value is the amount of money received and the second value is the prob
12 min read
Remainder Evaluation
Given two positive integers Num1 and Num2, the task is to find the remainder when Num1 is divided by Num2. Examples: Input: Num1 = 11, Num2 = 3Output: 2Explanation: 3) 11 (3 - 9 --------- 2 -> Remainder ---------- Input: Num1 = 15, Num2 = 3Output: 0 Approach 1: The problem can be solved by using the modulus operator. Modulus operator returns the
5 min read
Grid Evolution and Query Evaluation
Given a grid of dimensions n rows and m columns, where each element initially contains an integer from 0 to k-1. The grid evolves over time, with each element undergoing the transformation (grid[i][j] + 1) % k after each unit of time. You are also provided with a series of queries, each defined by [t, val, lx, ly, rx, ry], the task is to determine,
13 min read
Horner's Method for Polynomial Evaluation
Given a polynomial of the form cnxn + cn-1xn-1 + cn-2xn-2 + ... + c1x + c0 and a value of x, find the value of polynomial for a given value of x. Here cn, cn-1, .. are integers (may be negative) and n is a positive integer.Input is in the form of an array say poly[] where poly[0] represents coefficient for xn and poly[1] represents coefficient for
6 min read