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[i]=='(')
pushS(pe[i]);
else if((pe[i]>=65 && pe[i]<=90) || (pe[i]>=97 && pe[i]<=122))
pushE(pe[i]);
else if(pe[i]==')')
call();
else
{
if(pe[i]=='+')
{
while(stack1[topS]!='(')
pushE(pop());
pushS('+');
}
else if(pe[i]=='-')
{
while(stack1[topS]!='(')
pushE(pop());
pushS('-');
}
else if(pe[i]=='^')
{
pushS('^');
}
else if(pe[i]=='*')
{
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[i]=='/')
{
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();
}
*
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---