0% found this document useful (0 votes)
9 views2 pages

Evaluation

This document contains a C program that evaluates postfix expressions using a stack. It defines functions for pushing and popping elements from the stack, as well as evaluating the postfix expression based on operators. The program prompts the user for a postfix expression and outputs the evaluation result.

Uploaded by

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

Evaluation

This document contains a C program that evaluates postfix expressions using a stack. It defines functions for pushing and popping elements from the stack, as well as evaluating the postfix expression based on operators. The program prompts the user for a postfix expression and outputs the evaluation result.

Uploaded by

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

#include<string.

h>
#include<math.h>
char s[20],postfix[20];
int top=-1;

void push(char symb)


{
top=top+1;
s[top]=symb;
}

char pop()
{
if(top==-1)
{
printf("stack is empty");
exit(1);
}
return s[top--];

int postfixevaluate( char postfix[])


{
int i,op1,op2,R,value;
for(i=0;i<strlen(postfix);i++)
{ if(postfix[i]>'0' && postfix[i]<='9')
{
push(postfix[i]-'0');
}
else
{
op2=pop();
op1=pop();
switch(postfix[i])
{
case '+':
R=op1+op2;
break;
case '-':
R=op1-op2;
break;
case '*':
R=op1*op2;
break;
case '/':
R=op1/op2;
break;
case '^':
R=pow(op1,op2);
break;
case '$':
R=pow(op1,op2);
break;
default:
printf("Invalid");
}//switch
push(R);
}
}
value=pop();
return value;
}//closing the function

void main()
{
int result;
printf("enter the postfix expression");
scanf("%s",postfix);
result=postfixevaluate(postfix) ;
printf("Result of postfix expression is %d",result);
}

You might also like