EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

There are 3 types of Expressions

  • Infix Expression
  • Postfix Expression
  • Prefix Expression

INFIX


The arithmetic operator appears between the two operands to which it is being applied.

POSTFIX

The arithmetic operator appears directly after the two operands to which it applies. Also called reverse polish notation

PREFIX


The arithmetic operator is placed before the two operands to which it applies. Also called polish notation.

Evaluating Arithmetic Expressions

  1. Convert the given infix expression to Postfix expression
  2. Evaluate the postfix expression using stack.

Algorithm to convert Infix Expression to Postfix Expression:

Read the infix expression one character at a time until it encounters the delimiter “#”

Step 1: If the character is an operand, place it on the output.

Step 2: If the character is an operator, push it onto the stack. If the stack operator has a higher or equal priority than input operator then pop that operator from the stack and place it onto the output.

Step 3:If the character is left parenthesis, push it onto the stack

Step 4:If the character is a right parenthesis, pop all the operators from the stack till it encounters left parenthesis, discard both the parenthesis in the output.

E.g. Consider the following Infix expression: – A*B+(C-D/E)#

Read charStackOutput
    A       A
    *          *      A
      B        + *          AB
      +          +      AB*
        (          ( +          AB*
    / – ( +  

Read char

C

D

/

E

)

Stack

 
 
(
+
Stack
 
(
+
Stack
 
 
 
(
+
Stack
 
 
/
(
+
Stack
 
 
/
(
+

Output
 

AB*C


AB*C


AB*CD

AB*CD

AB*CDE

AB*CDE/-

Read charStackOutput
#       AB*CDE/-+
Output: Postfix expression:- AB*CDE/-+ Evaluating the Postfix Expression
Algorithm to evaluate the obtained Postfix Expression
Read the postfix expression one character at a time until it encounters the delimiter „#‟ Step 1: If the character is an operand, push its associated value onto the stack.
Step 2: If the character is an operator, POP two values from the stack, apply the operator to them and push the result onto the stack.
E.g consider the obtained Postfix expression:- AB*CDE/-+
 
Operand Value
A 2
B 3
C 4
D 4
E 2
 
 
Char Read Stack

A 2
 
B 3 2

Char Read Stack
* 6
C 4 6
 
D 4 4 6 
E
 
/ 2 4 6
 
2 6
+ 8



 
OUTPUT = 8
Example 2: Infix expression:- (a+b)*c/d+e/f#
 
Read char Stack Output
 
( (
a ( a
+ +( a
 
b +( ab
( ab+
* ab+
ab+c*
#
Postfix expression:- ab+c*d/ef/+

Evaluating the Postfix Expression

Operand Value
a 1
b 2
c 4
d 2
e 6
f 3
Output = 8
Infix to Postfix ConversionOutput
#define SIZE 50           
/* Size of Stack */
#include <ctype.h> char s[SIZE];
int top=-1;       /* Global declarations */
void push(char elem)
{
s[++top]=elem;
}

char pop()
{
return(s[top–]);
}
int pr(char elem)
{                 /* Function for precedence */ switch(elem)
{
case ‘#’: return 0;
case ‘(‘: return 1; case ‘+’:
case ‘-‘: return 2;
case ‘*’: case ‘/’: return 3;
}
return 0;
}

Void main()
{                         /* Main Program */
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
printf(“\nRead the Infix Expression ? “); scanf(“%s”,infx);
push(‘#’);
while( (ch=infx[i++]) != ‘\0‘)
{
if( ch == ‘(‘) push(ch);
else
if(isalnum(ch)) pofx[k++]=ch;
else
if( ch == ‘)’)
{
while( s[top] != ‘(‘)
pofx[k++]=pop();
elem=pop();    /* Remove */
}
else
{       /* Operator */
while( pr(s[top]) >= pr(ch) ) pofx[k++]=pop();
push(ch);
}
}
while( s[top] != ‘#’)     /* Pop from stack till empty
pofx[k++]=pop();
pofx[k]=’\0‘;          /* Make pofx as valid string */
printf(“\n\nGiven Infix Expn: %s Postfix Expn: %s\n“,infx,pofx);
}
              Read the Infix Expression ?      










  (a+b)-(c-d)        


Given Infix Expn: (a+b)*(c-d)













Postfix Expn: ab+cd-*
Other courses

NoSQL

Cloud computing

power BI

Evaluating the Arithmetic Expression in Stack

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer