Converting InFix to PostFix with Linked Stacks

#include <iostream> // <<, >>, cout, cin
#include <string> // string, ==, find, npos
#include <cassert> // assert()
#include <cctype> // alnum()
using namespace std;

#include "LStack.h"

string PostFixExp(string exp);

int main()
{
string InFixExp;
cout << "Type 'X' to leave the program." << endl;

for (;;)
{
cout << "Enter an expression: ";
getline(cin, InFixExp);

if (InFixExp == "X") break;

cout << "Postfix expression is: " << PostFixExp(InFixExp) << endl;
}
exit(0);
}

string PostFixExp(string exp)
{
char * token,
topToken;
Stack opStack;
string PostFix;
const string BLANK = " ";

for (int i = 0; i < exp.length(); i++)
{
token = &exp[i];
switch (*token)
{
case ' ' : break; //if it's space, continue
case '(':
opStack.push(*token); //if it's left parenthesis, push on the stack.
break;
case ')':
for (;;) //if it's righ parenthesis, pop it.
{
assert(!opStack.empty());
topToken = opStack.top();
opStack.pop();
if (topToken == '(') break;
PostFix.append(BLANK + topToken);
}
break;
case '+': case '-': //check for any operations
case '*': case '/': case'%':
for (;;)
{
if (opStack.empty() ||
opStack.top() == '(' ||
(*token == '*' || *token == '/' || *token == '%') &&
(opStack.top() == '+' || opStack.top() == '-'))
{
opStack.push(*token);
break;
}
else
{
topToken = opStack.top();
opStack.pop();
PostFix.append(BLANK + topToken);
}
}
break;
default: // operand
PostFix.append(BLANK + token);
for (;;)
{
if (!isalnum(exp[i + 1])) break; // end of identifier
i++;
token = &exp[i];
PostFix.append(1, *token);
}
}
// Pop remaining operators on the stack
for (;;)
{
if (opStack.empty()) break;
topToken = opStack.top();
opStack.pop();
if (topToken != '(')
{
PostFix.append(BLANK + topToken);
}
else
{
cout << " *** Error in infix expression ***\n";
break;
}
}
}
return PostFix;
}

What can I fix this order in order to work properly?
1. Please use the code formatting tags to format your code. It's not readable at present.
2. Why do you think there's something wrong with it?
Topic archived. No new replies allowed.