Hello, I want to be able to modify my InfixToPostfix, so that between two operands, I can use space to separate them. An example of this would be for 34+12, the function returns 34 12+. Could someone help me work through this? I'm not sure how to attack this.
string InfixToPostfix (string infix)
{
// Empty character stack and blank postfix string.
stack<char> opStack;
string postFixString = "";
// Get a pointer to our character array.
constchar *cPtr = infix.c_str();
// Loop through the array (one character at a time) until we reach the end of the string.
while (*cPtr != '\0') {
if (isOperand(*cPtr))
{
// If operand, simply add it to our postfix string.
postFixString += *cPtr;
}
elseif (isOperator(*cPtr)) {
// If it is an operator, pop operators off our stack until it is empty,
// an open parenthesis or an operator with less than or equal precedence.
while (!opStack.empty() && opStack.top() != '(' &&
compareOperators(opStack.top(),*cPtr) <= 0) {
postFixString += opStack.top();
opStack.pop();
}
opStack.push(*cPtr);
}
elseif (*cPtr == '(')
{
// Simply push all open parenthesis onto our stack
opStack.push(*cPtr);
}
elseif (*cPtr == ')') {
// When we reach a closing one, start popping off operators until we run into the opening parenthesis.
while (!opStack.empty()) {
if (opStack.top() == '(') { opStack.pop(); break; }
postFixString += opStack.top();
opStack.pop();
}
}
// Advance our pointer to next character in string.
cPtr++;
}
// After the input expression has been ran through, if there is any remaining operators left on the stack
// pop them off and put them onto the postfix string.
while (!opStack.empty()) {
postFixString += opStack.top();
opStack.pop();
}
// Show the postfix string at the end.
return postFixString;
}