0% found this document useful (0 votes)
21 views3 pages

Evaluation of Postfix Expression

The document presents a C program for evaluating postfix expressions using a stack data structure. It defines functions for stack operations such as push, pop, and checks for operands and operators, and performs calculations based on the postfix input. The main function processes the input expression and outputs the final result after evaluation.

Uploaded by

balarammuni17
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)
21 views3 pages

Evaluation of Postfix Expression

The document presents a C program for evaluating postfix expressions using a stack data structure. It defines functions for stack operations such as push, pop, and checks for operands and operators, and performs calculations based on the postfix input. The main function processes the input expression and outputs the final result after evaluation.

Uploaded by

balarammuni17
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/ 3

EVALUATION OF POSTFIX EXPRESSION

#include<stdio.h>
#define max 40

typedef struct stack


{
int list[max];
int top;
}stack;

void init(stack *sptr)


{
sptr->top =-1;
}

int isempty(stack *sptr)


{
if(sptr->top==-1)
return 1;
else
return 0;
}

int isfull(stack *sptr)


{
if(sptr->top==max-1)
return 1;
else
return 0;
}

void push(stack *sptr,int item)


{

if(isfull(sptr))
{
printf("stack is full\n");
}
else{
sptr->top++;
sptr->list[sptr->top]=item;
printf("\n%d is pushed",item);
}
}

int pop(stack *sptr)


{
char temp;

if(isempty(sptr))
{
return -99999;
}
else
{

temp=sptr->list[sptr->top];
sptr->top--;
printf("\n%d is popped",temp);
return temp;
}
}
int isoperand(char op)
{
if(op >='A'&& op <='Z' ||op >='a' && op <='z' )
return 1;
else
return 0;
}
int isoperator(char op)
{
if(op == '+' || op =='-' || op== '*' || op == '/' || op == '%' )
return 1;
else
return 0;
}

int calculate(int op1,char op,int op2)


{
int res=0;
switch (op)
{
case '+' : res=op2+op1;
break;
case '-' : res=op2-op1;
break;
case '*' : res=op2*op1;
break;
case '/' : res=op2/op1;
break;
default : return 0;
}
return res;
}
main ()
{
stack s;
char str1[30],str2[30];
char op;
int result,op1,op2,v;
init(&s);
printf("enter the postfix exp\n");
scanf("%s",str1);
int i=0,j=0;

while(str1[i]!='\0')
{
if(isoperand(str1[i]))
{
printf("enter the value of %c",str1[i]);
scanf("%d",&v);
push(&s,v);
i++;
}
else if(isoperator(str1[i]))
{
op=str1[i];
op1=pop(&s);
op2=pop(&s);
result=calculate(op1,op,op2);printf("result =%d",result);
push(&s,result);
i=i+1;

}
else
return 0;
}
result=pop(&s);
printf("result::%d\n",result);

You might also like