INFIX TO POSTFIX

I am working on a program which convert infix expression to postfix expression.
But program is not working as expected.
I need your help me to figure out the problem.
Here is my code:-

#include <iostream>
#include <string>
#include <stack>
#include <conio.h>

using namespace std;

int main()
{
stack<char> stck;
int len;
string exp;
while(true)
{
cout<<"Enter Infix expression: "; getline(cin,exp); int yL=exp.length();
string ex="("+exp+")";
len=ex.length();
char Y[len]; char ch,a;
for (int i=0; i<len; i++)
{
ch=ex[i];
if(stck.empty())
{
stck.push(ch);
}
if (ch=='(')
{
stck.push(ch); Y[i]=' ';
}
else if((ch>=65 && ch<=90) || (ch>=97 && ch<=122) ||(ch>=48 && ch<=57))
{
Y[i]=ch;
}
else if(ch==')')
{
while(true)
{
if (stck.top()=='(')
{
stck.pop();
break;
}
else
{
a=stck.top();
Y[i]=a;
stck.pop();
}
}
}
else if(ch=='^')
{
stck.push(ch); Y[i]=' ';
}
else if(ch=='*')
{
if (stck.top()=='/' || stck.top()=='*' || stck.top()=='^')
{
a=stck.top();
Y[i]=a;
stck.pop();
}
else
{
Y[i]=' ';
}
stck.push(ch);
}
else if (ch=='/')
{
if (stck.top()=='/' || stck.top()=='*' || stck.top()=='^')
{
a=stck.top();
Y[i]=a;
stck.pop();
}
else
{
Y[i]=' ';
}
stck.push(ch);
}
else if (ch=='+')
{
if (stck.top()=='^' || stck.top()=='*' || stck.top()=='/' || stck.top()=='+' || stck.top()=='-')
{
a=stck.top();
Y[i]=a;
stck.pop();
}
else Y[i]=' ';
stck.push(ch);
}
else if (ch=='-')
{
if (stck.top()=='^' || stck.top()=='*' || stck.top()=='/' || stck.top()=='+' || stck.top()=='-')
{
a=stck.top();
Y[i]=a;
stck.pop();
}
else Y[i]=' ';
stck.push(ch);
}
}
for (int i=0; i<sizeof(Y); i++)
{
if(Y[i]==' ')
{
for (int j=i; j<sizeof(Y); j++)
{
Y[j]=Y[j+1];
}
}
}
cout<<"\nEquivalent postfix expression is: ";
for (int i=0; i<sizeof(Y)-4; i++)
cout<<Y[i];
cout<<endl;
_getch();
}
return 0;
}
Last edited on
Hey i got the answer. Now no need to reply. You can use the code to develop your own program. I myself improved the above code :D
Here is the corrected coding:-

#include <iostream>
#include <string>
#include <stack>
#include <conio.h>

using namespace std;

int main()
{
stack<char> stck;
int len;
string exp;
while(true)
{
cout<<"\nEnter Infix expression: "; getline(cin,exp); int yL=exp.length();
string ex="("+exp+")";
len=ex.length();
stack<char> Y; char ch,a;
for (int i=0; i<len; i++)
{
ch=ex[i];
if(stck.empty())
{
stck.push(ch);
}
if (ch=='(')
{
stck.push(ch);
}
else if((ch>=65 && ch<=90) || (ch>=97 && ch<=122) ||(ch>=48 && ch<=57))
{
Y.push(ch);
}
else if(ch==')')
{
while(true)
{
if (stck.top()=='(')
{
stck.pop();
break;
}
else
{
a=stck.top();
Y.push(a);
stck.pop();
}
}
}
else if(ch=='^')
{
stck.push(ch);
}
else if(ch=='*')
{
if (stck.top()=='/' || stck.top()=='*' || stck.top()=='^')
{
a=stck.top();
Y.push(a);
stck.pop();
if (stck.top()=='/' || stck.top()=='*' || stck.top()=='^')
{
a=stck.top();
Y.push(a);
stck.pop();
}
}
stck.push(ch);
}
else if (ch=='/')
{
if (stck.top()=='/' || stck.top()=='*' || stck.top()=='^')
{
a=stck.top();
Y.push(a);
stck.pop();
if (stck.top()=='/' || stck.top()=='*' || stck.top()=='^')
{
a=stck.top();
Y.push(a);
stck.pop();
}
}
stck.push(ch);
}
else if (ch=='+')
{
if (stck.top()=='^' || stck.top()=='*' || stck.top()=='/' || stck.top()=='+' || stck.top()=='-')
{
a=stck.top();
Y.push(a);
stck.pop();
if (stck.top()=='^' || stck.top()=='*' || stck.top()=='/' || stck.top()=='+' || stck.top()=='-')
{
a=stck.top();
Y.push(a);
stck.pop();
}
}
stck.push(ch);
}
else if (ch=='-')
{
if (stck.top()=='^' || stck.top()=='*' || stck.top()=='/' || stck.top()=='+' || stck.top()=='-')
{
a=stck.top();
Y.push(a);
stck.pop();
if (stck.top()=='^' || stck.top()=='*' || stck.top()=='/' || stck.top()=='+' || stck.top()=='-')
{
a=stck.top();
Y.push(a);
stck.pop();
}
}
stck.push(ch);
}
}
string o;
while(!Y.empty())
{
o.push_back(Y.top());
Y.pop();
}
string output;
for(int i=o.length(); i>=0; i--)
{
output.push_back(o[i]);
}
cout<<"\nEquivalent postfix expression is: "<<output;
cout<<endl;
_getch();
}
return 0;
}
Last edited on
Topic archived. No new replies allowed.