Infix to Postfix

A little lost. Program is not working properly and I'm sure there are a lot of errors. Please help.

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;

char InfixToPostfix(char infix[]);

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

node *top;

class stackAlgo{
private:
    int counter;
public:
    node *pushStack(node*, char);
    node *popStack(node*);
    char copyTop(node*);
    void printStack(node*);
    stackAlgo()
    {
        top = nullptr;
        counter = 0;
    }

};

node *stackAlgo::pushStack(node *top, char item)
{
    node *temp;
    temp = new (struct node);
    temp -> value = item;
    temp -> link = top;
    top = temp;
    counter++;
    return top;
}

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

char stackAlgo::copyTop(node *top)
{
    char item;
    if (top == nullptr)
    {
        cout << "Stack is empty.";
    }
    else
    {
        item = top -> value;
    }
    return item;
}

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

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

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

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

}

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

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

char InfixToPostfix::output(char infix[])
{
    stackAlgo s;
    char result[11];
    int length = 11;
    char blank = NULL;
    for (int i = 0; i < length - 1; i++)
    {
        if (infix[i] == 'A' || infix[i] == 'B' || infix[i] == 'C' || infix[i] == 'D' || infix[i] == 'E')
        {
            result[i] = result[i] + infix[i];
        }
        else
        {
            while (top == nullptr && precedence(s.copyTop(top), infix[i]))
            {
                result[i] = result[i] + s.copyTop(top);
                s.popStack(top);
            }
            s.pushStack(top, infix[i]);
        }
        while (top == nullptr)
        {
            result[i] = result[i] + s.copyTop(top);
            s.popStack(top);
        }
    }
    for (int i = 0; i < 11; i++)
    {
        return result[i];
    }
    return blank;
}


int main()
{
    char infix1[11]; //Initalize and declare first infix pattern
    for (int i = 0; i < 11; i++)
    {
        cout << "Enter a character: ";
        cin >> infix1[i];
    }
    
    class InfixToPostfix object;
    
    char postfix = object.output(infix1);
    
    cout << postfix;

    
    return 0;
}
Topic archived. No new replies allowed.