Skip to content

Instantly share code, notes, and snippets.

@anshumanatri
Created March 12, 2009 10:12
Show Gist options
  • Save anshumanatri/77992 to your computer and use it in GitHub Desktop.
Save anshumanatri/77992 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{ static int a[5];
int i,n,top=-1,op1,op2,noperator=0,noperand=0;
char ch[10];
clrscr();
printf("\n Enter the string\n");
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
if ((ch[i]>=48)&&(ch[i]<=57))
{
a[++top]=ch[i]-48;
noperand++;
}
else
switch(ch[i])
{
case '+' : noperator++;
op2=a[top--];
op1=a[top--];
n=op1+op2;
a[++top]=n;
break;
case '-' : noperator++;
op2=a[top--];
op1=a[top--];
n=op1-op2;
a[++top]=n;
break;
case '*' : noperator++;
op2=a[top--];
op1=a[top--];
n=op1*op2;
a[++top]=n;
break;
case '/' : noperator++;
op2=a[top--];
op1=a[top--];
n=op1/op2;
a[++top]=n;
break;
}
}
if(noperator==noperand-1)
printf("value of the given expression is : %d",a[0]);
else
printf("\n Enter a correct postfix expression");
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment