Infix to Postfix Please Help!!

My infix is A * B + C * D * E + F and the postfix that is outputting is A B C D E F + * * + * which is incorrect. Please help me get correct output. Here is my code:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  #include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;

struct node{
    char value;
    struct node *link;
};

node *top;

class stack{
private:
    int counter;
public:
    node *pushInto(node*, char);
    node *popOut(node*);
    void traverseStack(node*);
    int isStackEmpty();
    stack()
    {
        top = nullptr;
        counter = 0;
    }
};

node *stack::pushInto(node *top, char a)
{
    node *temp;
    temp = new (struct node);
    temp -> value = a;
    temp -> link = top;
    top = temp;
    counter++;
    return top;
}

node *stack::popOut(node *top)
{
    node *temp;
    char item;
    if (top == nullptr)
    {
        cout << "Stack is empty";
    }
    else
    {
        temp = top;
        item = temp -> value;
        cout << item;
        top = top -> link;
        delete temp;
        counter--;
    }
    return top;
}

void stack::traverseStack(node *top)
{
    node *temp;
    temp = top;
    if (top == nullptr)
    {
        cout << "Stack is empty.";
    }
    else
    {
        while (temp != nullptr)
        {
            cout << temp -> value << endl;
            temp = temp -> link;
        }
    }
}

int stack::isStackEmpty()
{
    if (top == nullptr)
        return 0;
    return -1;
}

class infixToPostfix:public stack{
public:
    bool isOperand(char);
    bool isOperator(char);
    int weightOfOperator(char);
    char precedence(char, char);
    void output(char[]);
};

bool infixToPostfix::isOperand(char a)
{
    if (a >= 'A' && a <= 'Z')
        return true;
    return false;
}

bool infixToPostfix::isOperator(char a)
{
    if (a == '*' || a == '/' || a == '+' || a == '-')
        return true;
    return false;
}

int infixToPostfix::weightOfOperator(char a)
{
    int weight;
    
    if (a == '*' || a == '/')
        weight = 2;
    else if (a == '+' || a == '-')
        weight = 1;
    else
        weight = -1;
    return weight;
}

char infixToPostfix::precedence(char a, char b)
{
    if (weightOfOperator(a) > weightOfOperator(b))
    {
        return a;
    }
    return b;
}

void infixToPostfix::output(char a[])
{
    const int length = 11;
//    const int numberOperators = 5;
    char output[length];
    for (int i = 0; i < length; i++)
    {
        if (isOperand(a[i]))
        {
            output[i] = a[i];
        }
        else
        {
            top = pushInto(top, a[i]);
        }
        cout << output[i];
    }

    for (int i = 1; i < length; i+=2)
    {
        if (precedence(a[i], a[i + 1]))
        {
            top = popOut(top);
        }
    }
}

int main()
{
    infixToPostfix result;
    const int length = 11;
    char infix1[length] = {'A', '*', 'B', '+', 'C', '*', 'D', '*', 'E', '+', 'F'};  //Initalize and declare first infix pattern
  //  char infix2[length] = {'A', '+', 'B', '*', 'C', '+', 'D', '+', 'E', '*', 'F'};
    
    result.output(infix1);
    cout << "\n";
//    result.output(infix2);
    
    return 0;
}
bump, anyone???
Hi, hockey34.
There’s at least an error in logic.
You should not pop out all the operators after you have pushed all them into the stack. It should be more a ‘push into, get out of’ game, so to say.

For example, in your string "A * B + C * D * E + F" , when you get into the first ‘+’, the ‘*’ into the stack should be rescued, because it has got a higher precedence.

I’ve changed your code to make the ‘output’ routine describe every single step, and I think it makes the things clearer:
MyStack.h
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
#ifndef MYSTACK_H
#define MYSTACK_H

#include <iostream>

struct Node{
    char value;
    Node *link;
};

class MyStack
{
public:
    MyStack() = default;
    Node *pushNodeInto(Node*, char);
    Node *popNodeOut(Node*);
    void traverseStack(Node*);
    bool isEmpty();

protected:
    Node *top {nullptr};
    int counter {0};
};

#endif // MYSTACK_H 


MyStack.cpp
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
56
57
58
59
#include "MyStack.h"

Node* MyStack::pushNodeInto(Node *top, char tobepushed)
{
    Node *temp = new (Node);
    temp->value = tobepushed;
    temp->link = top;
    top = temp;
    counter++;

    return top;
}

Node* MyStack::popNodeOut(Node *top)
{
    Node *temp;
    char item;
    if (top == nullptr)
    {
        std::cout << "Stack is empty\n";
    }
    else
    {
        temp = top;
        item = temp->value;
        std::cout << item;
        top = top->link;
        delete temp;
        counter--;
    }

    return top;
}

void MyStack::traverseStack(Node *top)
{
    Node *temp;
    temp = top;
    if (top == nullptr)
    {
        std::cout << "Stack is empty.\n";
    }
    else
    {
        while (temp != nullptr)
        {
            std::cout << temp->value << std::endl;
            temp = temp->link;
        }
    }
}

bool MyStack::isEmpty()
{
    if (top == nullptr)
        return true;

    return false;
}


InfixToPostfix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef INFIXTOPOSTFIX_H
#define INFIXTOPOSTFIX_H

#include <string>
#include "MyStack.h"

class InfixToPostfix : public MyStack
{
public:
    InfixToPostfix();
    bool isOperand(char);
    bool isOperator(char);
    int weightOfOperator(char);
    char precedence(char, char);
    void print(std::string);
};

#endif // INFIXTOPOSTFIX_H 


InfixToPostfix.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include "InfixToPostfix.h"

InfixToPostfix::InfixToPostfix()
{}

bool InfixToPostfix::isOperand(char tobetested)
{
    if ('A' <= tobetested && tobetested <= 'Z')
        return true;

    return false;
}

bool InfixToPostfix::isOperator(char tobetested)
{
    switch (tobetested) {
    case '*':
    case '/':
    case '+':
    case '-':
        return true;
        break;
    default:
        break;
    }

    return false;
}

int InfixToPostfix::weightOfOperator(char oper)
{
    switch (oper) {
    case '*':
    case '/':
        return 2;
        break;
    case '+':
    case '-':
        return 1;
        break;
    default:
        break;
    }

    return -1; // ?? error code?
}

char InfixToPostfix::precedence(char a, char b)
{
    if (weightOfOperator(a) > weightOfOperator(b))
    {
        return a;
    }

    return b;
}

void InfixToPostfix::print(std::string str)
{
    std::cout << "\nPassed infix: " << str << '\n';
    std::string output;
    for (size_t i = 0; i < str.length(); i++)
    {
        std::cout << "str.at(i) = \'" << str.at(i) << "\' --> ";
        if (isOperand(str.at(i)) || str.at(i) == ' ')
        {
            output += str.at(i);
            std::cout << "adding \'" << str.at(i)
                      << "\' to unused \"output\"\n";
        }
        else
        {
            top = pushNodeInto(top, str.at(i));
            std::cout << "pushing \'" << str.at(i)
                      << "\' into MyStack\n";
        }
    }

    for (size_t i = 1; i < str.length(); i+=2)
    {
        if (precedence(str.at(i), str.at(i+1)))
        {
            top = popNodeOut(top);
            std::cout << "Popping \'" << top->value
                      << "\' out of MyStack\n";
        }
    }
    std::cout << '\n';
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include "InfixToPostfix.h"

int main()
{
    InfixToPostfix result;

      //Initalize and declare first infix pattern
    std::string infix1 = "A * B + C * D * E + F";
    result.print(infix1);

    return 0;
}


Output:

Passed infix: A * B + C * D * E + F
str.at(i) = 'A' --> adding 'A' to unused "output"
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = '*' --> pushing '*' into MyStack
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = 'B' --> adding 'B' to unused "output"
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = '+' --> pushing '+' into MyStack
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = 'C' --> adding 'C' to unused "output"
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = '*' --> pushing '*' into MyStack
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = 'D' --> adding 'D' to unused "output"
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = '*' --> pushing '*' into MyStack
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = 'E' --> adding 'E' to unused "output"
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = '+' --> pushing '+' into MyStack
str.at(i) = ' ' --> adding ' ' to unused "output"
str.at(i) = 'F' --> adding 'F' to unused "output"
+Popping '*' out of MyStack
*Popping '*' out of MyStack
*Popping '+' out of MyStack
+Popping '*' out of MyStack

Last edited on
Topic archived. No new replies allowed.