#include<stdio.h>
#include<stdlib.h>
int top=-1,stacksize=9;
char st[10];
int push(char value)
{
if(top==stacksize-1)
{
printf("\n stack is full");
return 0;
}
else
{
top++;
st[top]=value;
return 1;
}
}
char pop()
{
if(top==-1)
{
printf("stack is empty");
//return 1;
}
else
return st[top--];
}
char peek()
{
if(top==-1)
{
//return -1;
}
else
{
return st[top];
}
}
int main()
{
float value;
char postfix[20],cc,sopr,fopr,digit[20];
int i=0;
printf("enter the positive expression:");
scanf("%s",postfix);
while(postfix[i]!='\0')
{
cc=postfix[i];
if(cc=='+'||cc=='-'||cc=='*'||cc=='/')
{
sopr=pop();
fopr=pop();
switch(cc)
{
case'+':value=fopr+sopr;
break;
case'-':value=fopr-sopr;
break;
case'*':value=fopr*sopr;
break;
case'/':value=fopr/sopr;
}
push(value);
}
else
{
digit[0]=cc;
digit[1]='\0';
value=atoi(digit);
push(value);
}
i++;
}
value=pop();
printf("\nvalue of the expression=%f",value);
}
Comments
Post a Comment