Reply
Thu 2 Jul, 2009 05:02 pm
i have written a program to convert infix expression to postfix expression.
There are no compilation errors but on run-time it shows the message : "Program Termination". Please help me by making me know where the error lies, here is the program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 30
int topS=-1,topE=-1;
char stack1[MAX],stack2[MAX];
char pop()
{
if(topS==-1)
return '#';
return stack1[topS--];
}
void pushS(char s)
{
stack1[++topS]=s;
}
void pushE(char s)
{
stack2[++topE]=s;
}
void show()
{
int j;
for(j=0;j<=topE;j++)
printf("%c",stack2[j]);
}
void call()
{
while(stack1[topS]!='(')
pushE(pop());
pop();
}
void main()
{
clrscr();
char pe[MAX],len,i=0;
printf("\n Enter the Infix Expression");
gets(pe);
len=strlen(pe);
while(i<len)
{
if(pe=='(')
pushS(pe);
else if((pe>=65 && pe<=90) || (pe>=97 && pe<=122))
pushE(pe);
else if(pe==')')
call();
else
{
if(pe=='+')
{
while(stack1[topS]!='(')
pushE(pop());
pushS('+');
}
else if(pe=='-')
{
while(stack1[topS]!='(')
pushE(pop());
pushS('-');
}
else if(pe=='^')
{
pushS('^');
}
else if(pe=='*')
{
if((stack1[topS]=='+') || (stack1[topS]=='-') || (stack1[topS]=='('))
{
pushS('*');
}
else if((stack1[topS]=='^') || (stack1[topS]=='/'))
{
while((stack1[topS]!='+') || (stack1[topS]!='-') || (stack1[topS]!='('))
pushE(pop());
pushS('*');
}
}
else if(pe=='/')
{
if((stack1[topS]=='+') || (stack1[topS]=='-') || (stack1[topS]=='('))
{
pushS('/');
}
else if((stack1[topS]=='^') || (stack1[topS]=='*'))
{
while((stack1[topS]!='+') || (stack1[topS]!='-') || (stack1[topS]!='('))
pushE(pop());
pushS('/');
}
}
}
i++;
}
printf("\n The Conversion has been done");
printf("\n The resulting Postfix Expression is:\n");
show();
getch();
}
@Prateekr10,
how much is it worth to you?
@Ragman,
That means you dont know the answer......
@Prateekr10,
'ya think?
If you become a programmer, will you still ask questions here on A2K to do your work for you?
@Ragman,
and what did you do a**h***,
if you are that brilliant and genius
why are you even on this site.............