Stack, Infix to Postfix

I'm having trouble with my program. This program is supposed to convert an infix expression to a postfix expression. The problem is the program still outputs the infix expression. Here are the codes.

void infix2postfix(char infix[], char postfix[], int s);
int pr_token(char token);

int main ()
{
int s;
char infix[20];
cout<<"Enter an expression in infix: ";
cin.getline(infix, 20);
s = strlen(infix);
char postfix[s];
infix2postfix(infix, postfix, s);
cout<<"Postfix Expression is: "<<postfix;
return 0;
}

void infix2postfix(char infix[], char postfix[], int s)
{
stack<char> a;
int looper = 0, j=0, pr;
char token;
while(looper<s){
token = infix[looper];
if (token == '(')
{
a.push(token);
looper++;
continue;
}
if(token == ')')
{
while(!a.empty() && a.top() != '(')
{
postfix[j++] = a.top();
a.pop();
}
if(!a.empty())
{
a.pop();
}
looper++;
continue;
}
pr = pr_token(token);
if (pr==0)
{
postfix[j++]=token;
}
else
{

if(a.empty())
{
a.push(token);
}
else
{
while(!a.empty() && a.top() != '(' && pr <= pr_token(a.top()))
{
postfix[j++] = a.top();
a.pop();
}
a.push(token);
}
}
looper++;
}
while(!a.empty())
{
postfix[j++] = a.top();
a.pop();
}
postfix[j]=0;
}
int pr_token(char token)
{
if((token == '*') && (token=='/'))
{
return 2;
}
else if ((token == '+') && (token=='-'))
{
return 1;
}
else
return 0;
}
Topic archived. No new replies allowed.