0% found this document useful (0 votes)
28 views10 pages

DS Experiment No 03

This document discusses evaluating postfix expressions using a stack. It defines postfix notation as having operators after operands. To evaluate a postfix expression, each character is scanned from left to right - if it's an operand, it is pushed onto the stack, and if it's an operator, the top two stack values are popped and the operation is performed and result pushed back on. The document provides pseudocode of an algorithm to evaluate a postfix expression using a stack, and includes a C program code example to demonstrate evaluating a given postfix expression.

Uploaded by

jadhavkalpesh881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views10 pages

DS Experiment No 03

This document discusses evaluating postfix expressions using a stack. It defines postfix notation as having operators after operands. To evaluate a postfix expression, each character is scanned from left to right - if it's an operand, it is pushed onto the stack, and if it's an operator, the top two stack values are popped and the operation is performed and result pushed back on. The document provides pseudocode of an algorithm to evaluate a postfix expression using a stack, and includes a C program code example to demonstrate evaluating a given postfix expression.

Uploaded by

jadhavkalpesh881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Aim :

Evaluate Postfix Expression using Stack ADT.

INPUT SPECIFICATION :
Post expression is been inserted .

OUTPUT SPECIFICATION :
Evaluate the result of postfix expression .

THEORY :
Postfix Expression :
If we move the operators after the operands then it is known as a postfix
expression. In other words, postfix expression can be defined as an
expression in which all the operators are present after the operands

Evaluation of a Postfix Expression :


The ease of evaluation acts as the driving force for computers to translate an
infix notation into a postfix notation. That is, given an algebraic expression
written in infix notation, the computer first converts the expression into the
equivalent postfix notation and then evaluates the postfix expression. Both
these tasks—converting the infix notation into postfix notation and evaluating
the postfix expression—make extensive use of stacks as the primary tool. Using
stacks, any postfix expression can be evaluated very easily. Every character of
the postfix expression is scanned from left to right. If the character
encountered is an operand, it is pushed on to the stack. However, if an
operator is encountered, then the top two values are popped from the stack
and the operator is applied on these values. The result is then pushed on to the
stack.
Algorithm:
Evaluation of Postfix expression using Stack
STEP 1. Declare a int stack & character postfix expression.
STEP 2. Read the postfix string expression till not NULL
2.1 If the read character is digit, then push it into the stack.
2.2 Else, if the character is a operator
Pop two element from stack, perform operation and again push new result
into stack.
STEP 3. Return top of stack Or display top of stack as final result.

Pogram code :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define ms 5
struct stackopd
{
int top1;
int arr1[ms];
};

struct stackopr
{
int top2;
char arr2[ms];
};
typedef struct stackopd STKD;
typedef struct stackopr STKR;

void initialize1(STKD *s)


{
s->top1 = -1;
}

int isfulls1(STKD s)
{
if(s.top1 == ms-1)
return 1;
else
return 0;
}

int isemptys1(STKD s)
{
if(s.top1 == -1)
return 1;
else
return 0;
}

void push1(STKD *s,int x)


{
if(isfulls1(*s))
printf("Stack is overflow.....\n");
else
{
s->top1++;
s->arr1[s->top1] = x;
}
}

int pop1(STKD *s)


{
int x = -1;
if(isemptys1(*s))
printf("Stack is underflow.....\n");
else
{
x = s->arr1[s->top1];
s->top1--;
}
return x;
}

void initialize2(STKR *s)


{
s->top2 = -1;
}

int isfulls2(STKR s)
{
if(s.top2 == ms-1)
return 1;
else
return 0;
}

int isemptys2(STKR s)
{
if(s.top2 == -1)
return 1;
else
return 0;
}

void push2(STKR *s,char c)


{
if(isfulls2(*s))
printf("Stack is overflow.....\n");
else
{
s->top2++;
s->arr2[s->top2] = c;
}
}

char pop2(STKR *s)


{
char c = s->arr2[s->top2--];
return c;
}

int priority(char ch)


{
switch(ch)
{
case '^' : return 5;
case '*' :
case '/' :
case '%' : return 4;
case '+' :
case '-' : return 3;
}
}

char getopr(STKR s)
{
return s.arr2[s.top2];
}
int evaluate(char opr,int x, int y)
{
int i,p=1;
switch(opr)
{
case '+' : return(x+y);
case '-' : return(x-y);
case '*' : return(x*y);
case '/' : return(x/y);
case '%' : return(x%y);
case '^' : for(i=1;i<=y;i++)
p *= x;
return p;
}
}

void main()
{
STKD s1;
STKR s2;
int x,i,len,cnt,a,b,c;
char expr[25],postexpr[25],ch;
clrscr();
printf("Enter the infix expression : ");
scanf("%s",expr);
len = strlen(expr);
cnt=0;
initialize2(&s2);
for(i=0;i<len;i++)
{
ch = expr[i];
if((ch>=48)&&(ch<=57))
postexpr[cnt++] = ch;
else
{
if(isemptys2(s2))
push2(&s2,ch);
else
if(priority(ch) > priority(getopr(s2)))
push2(&s2,ch);
else
if(priority(ch) == priority(getopr(s2)))
{
postexpr[cnt++] = pop2(&s2);
push2(&s2,ch);
}
else
{
postexpr[cnt++] = pop2(&s2);
if(!isemptys2(s2))
{
if(priority(ch) == priority(getopr(s2)))
{
postexpr[cnt++] = pop2(&s2);
push2(&s2,ch);
}
}
else
push2(&s2,ch);
}
}
}
do
{
postexpr[cnt++] = pop2(&s2);
}while(!isemptys2(s2));
postexpr[cnt]='\0';

printf("\nPOSTFIX expression is %s \n",postexpr);

initialize1(&s1);
for(i=0;i<strlen(postexpr);i++)
{
ch = postexpr[i];
if((ch>=48)&&(ch<=57))
push1(&s1,ch-48);
else
{
b = pop1(&s1);
a = pop1(&s1);
c = evaluate(ch,a,b);
push1(&s1,c);
}
}
printf("\nResult of the expression = %d",pop1(&s1));
getch();

/*
OUTPUT OF PROGRAM :
Enter the infix expression : 2*3+8-9/3%2*5+4^2
POSTFIX expression is 23*8+93/2%5*-42^+
Result of the expression = 25
*/

CONCLUSION :
The postfix expression is been evaluated .

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy