C program to evaluate Postfix Expression using Stack

Simple C program to evaluate Postfix Expression using Stack.

Code:

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<math.h>

int s[50];

int top=-1;

int flag=0;

int pop()

{

return(s[top--]);

}

push(int elem)

{

if(flag==1){

int num;

num=pop();

s[++top]=elem+10*num;

}

else if(flag==0){

s[++top]=elem;

flag=1;

}

}





main(){

char pofx[50],ch;

int i=0,op1,op2;

printf("Enter the Postfix Expression:");

fgets(pofx,100,stdin);

while( (ch=pofx[i++]) != '\n')

{

if(isdigit(ch)) push(ch-'0');

else if(ch==' ')

flag=0;

else

{

flag=0;

op2=pop();

op1=pop();

switch(ch)

{

case '+':push(op1+op2);break;

case '-':push(op1-op2);break;

case '*':push(op1*op2);break;

case '/':push(op1/op2);break;

case ‘^’; push(pow(op1,op2));break;

default:

printf("Input invalid ... give proper input\n");

return 0;

}

}

}

printf("Result: %d\n",s[top]);

getch();

}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Servlet Program to Print Today’s Date and Time using refresh header

Java Program to Implement sorting algorithm using TCP on Server application