Unit III - Postfix Evaluation and Balancing Symbols
Unit III - Postfix Evaluation and Balancing Symbols
UNIT-3
postfix notation can easily be done using the stack data structure.
It is used in computers because it is faster than other types of notations (such as infix notation) as
Time and Space complexity for evaluating postfix expression using stack are
Input - A string consisting of numeric values along with operator symbols (*, /, +, -, etc) is
given as the input.
Output - The result obtained by solving the given input is required to be printed.
Example 1 :
Input - 2 3 * 4 5 + * Output - 54
Example 2 :
Input - 5 2 3 * * Output - 30
Algorithm for Evaluating Postfix Expression
+ 49 3 52 52
Program to evaluate postfix expression
#include <stdio.h>
#include<math.h>
#define maxcols 80
#define true 1
#define false 0
Double eval(char[]);
double pop(struct stack*);
Void push(struct stack*, double);
int empty(struct stack*);
int isdigit(char);
double oper(int,double,double);
Void main( ) {
char expr[maxcols];
int position =0;
while((expr[position++]=getchar())!=‘\n’);
expr[--position]=‘\0’;
printf(“\n The original postfix expression is “,expr);
printf(“\n%f”,eval(expr)); }
Program to evaluate postfix expression(Contd…)
else
Struct stack{ {
int top; /* operator */ Opnd2= pop(&opndstk);
double items[maxcols]; Opnd1=pop(&opndstk); Value=oper(c,opnd1,opnd2);
} Push(&opndstk,value);
double eval(char exp[]) }
{ }
int c, position; Return(pop(&opndstk));
double opnd1,opnd2,value; } /* end eval */
struct stack opndstk;
opndstk=-1; int isdigit(char symb)
for(position=0;(c=expr[position])!=‘\0’;position++) {
{ return(symb>=‘0’ && symb<=‘9’);
if(isdigi( c )) }
/* operand – convert the character representation */
/* of the digit into double and push it onto the stack */
Push(&opndstk, (double)(c-’0’));
}
Program to evaluate postfix expression(Contd…)
If the scanned character is a starting bracket ( ‘ ( ‘ or ‘ { ‘ or ‘ [ ‘), then push it to the stack.
If the scanned character is a closing bracket ( ‘ ) ’ or ‘ } ’ or ‘ ] ’ ), then pop from the stack and if the
popped character is the equivalent starting bracket, then proceed. Else, the expression is unbalanced.
After scanning all the characters from the expression, if there is any parenthesis found in the stack or
Now, let us see a program to check balanced parentheses in the given expression.
Let's understand the above algorithm through an example.
Suppose expression is 2 * ( 6 + 5 )
Solution:
First, the x variable is initialized by 0.
The scanning starts from the variable '2', when it encounters '(' then the 'x' variable gets incremented by
1 and when the x reaches to the last symbol of the expression, i.e., ')' then the 'x' variable gets
decremented by 1 and it's final value becomes 0.
We have learnt in the above algorithm that if x is equal to 0 means the expression is balanced;
Therefore, the above expression is a balanced expression.
C Code
#include<stdio.h> void pop()
#include<stdlib.h> {
#include<string.h> if (s.top == - 1)
#define MAX 20 {
struct stack printf ("Stack is Empty\n");
{ }
char stk[MAX]; else
int top; {
}s; s.top = s.top - 1; // Pop the char and decrement top
void push(char item) }}
{
if (s.top == (MAX - 1)) int main(){
printf ("Stack is Full\n"); char exp[MAX];
else int i = 0;
{ s.top = -1;
s.top = s.top + 1; // Push the char and increment top printf("\nINPUT THE EXPRESSION : ");
s.stk[s.top] = item; scanf("%s", exp);
}}
C Code (Contd…)
for(i = 0;i < strlen(exp);i++)
{
if(exp[i] == '(' || exp[i] == '[' || exp[i] == '{')
{
push(exp[i]); // Push the open bracket
continue;
}
else if(exp[i] == ')' || exp[i] == ']' || exp[i] == '}') // If a closed
bracket is encountered
{
if(exp[i] == ')')
{
if(s.stk[s.top] == '(')
{
pop(); // Pop the stack until closed bracket is found
}
else
{
printf("\nUNBALANCED EXPRESSION\n");
break;
}}