postfix evaluation for more than 1 digit

Hi guys!!

I made a program to convert an expression to postfix and then evaluate it!! But I don't know what modifications i should do to to make it work for more than 2 digit numbers!!
for eg. 2+3
will give 23+ as postfix expression and sum 5
But 23+5 wont work !
PLEASE HELP!!!

My code is as follows:


#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
char stack[50];
int stk[50];
int top=0,top2=0;
void calc();
void postfx(char infix[]);
void push(char symbol);
char pop();
int precedence(char ch);
int isdigit(int a);
int Pop();
void Push2(int symbol);
void Evaluate();
static int pos=0;
char postfix[40];

void main()
{
char infix[25];
printf("Enter the expression:");
gets(infix);
postfx(infix);
getch();
}
void postfx(char infix[])
{
int l;
static int i=0;

char temp,symbol;
l=strlen(infix);

while(i<l)
{
symbol=infix[i];
switch(symbol)
{
case'(':
push(symbol);
break;
case')':
temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case'+':
case'-':
case'*':
case'/':
case'^':
while(precedence(stack[top])>=precedence(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default:postfix[pos++]=symbol;
break;
}
i++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos]='\0';
printf("The postfix expression is:%s",postfix);
Evaluate();
}

void push(char symbol)
{
if(top>=49)
{printf("STACK is full.");
getch();
return;
}
else
{
top=top+1;
stack[top]=symbol;
}
}
char pop()
{
char item;
if(top==0)
{
printf("STACKis empty.");
getch();
return(0);
}
else
{
item=stack[top];
top--;
}
return(item);
}

int precedence(char ch)
{
if(ch=='/' || ch == '*')
{return(3);}
else if(ch=='^')
{return(4);}
else if(ch=='+' || ch=='-')
{return(2);}
else
return(1);
}

void Evaluate()
{
int i=0,a,b,q,z;
postfix[pos]=')';
pos=pos+1;
postfix[pos]='\0';

while(postfix[i]!=')')
{
if ( isdigit ( postfix[i] )==1 )
{
Push2(postfix[i]-48);
}
else if ( postfix[i] == '*' || postfix[i] == '+' || postfix[i] == '/' || postfix[i] == '^' || postfix[i] == '-' )
{
a = Pop ( );
b = Pop ( );
switch( postfix[i] )
{
case '+' :
{q=b+a;
break;}
case '-' :
{q=b-a;
break;}
case '*' :
{q=b*a;
break;}
case '/' :
{q=b/a;
break;}
{case '^' :
q=pow(a,b);
break;}
}
Push2 ( q );
}
i++;
}
z = Pop ( );
printf("\nThe result is:%d",z);
}

int isdigit(int a)
{ a=a-48;
if(a>=0 && a<=9)
{return (1);}
else
{return(2);}}
void Push2(int symbol)
{if(top2>=49)
{printf("STACK is full.");
getch();
return;
}
else
{stk[top2]=symbol;
top2=top2+1;
}}

int Pop()
{
int item;
if(top2==0)
{
printf("STACKis empty.");
getch();
return(0);
}
else
{top2--;
item=stk[top2];
}
return(item);
}






Topic archived. No new replies allowed.