infix to postfix multiple digits spacing issue.

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.

Below is my function for InfixToPostfix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
string InfixToPostfix (string infix)
{
        // Empty character stack and blank postfix string.
        stack<char> opStack;
        string postFixString = "";


        // Get a pointer to our character array.
        const char *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;
                }
                else if (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);
                }
                else if (*cPtr == '(')
                {
                        // Simply push all open parenthesis onto our stack
                        opStack.push(*cPtr);
                }
                else if (*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;
}

Last edited on
Every time you append anything to postFixString, append a space.
Topic archived. No new replies allowed.