Infix to Postfix calculator, having a little trouble...

Hi all, for a class assignment we are to create a class (infixToPostfix) that will handle functions to store the infix, show the infix, convert the infix to postfix, and determine precedence of two operators. My coding was going smoothly until I tried coding the convertToPostfix function. I got the algorithm from an online source and tried to cater it to my program but I'm getting errors as it pertains to popping operands from the stack.

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
// Specification file for infixToPostfix.h

#ifndef H_infixToPostfix
#define H_infixToPostfix

#include <string>
#include <stack>

using namespace std;

class infixToPostfix
{
public:
	void convertToPostfix();
	bool precedence(char opr1, char opr2);
	void storeInfix(string s);
	void showInfix();
	void showPostfix();
	infixToPostfix(); //Default constructor

private:
	string  ifx; //infix string
	string  pfx; //postfix string

};
//definition of Class Constructor
inline infixToPostfix::infixToPostfix()
{
    ifx = "";
    pfx = "";
}

//definition for precedence function
bool infixToPostfix::precedence(char opr1, char opr2)
{
    int val1, val2;

    if(opr1 == '+' || opr1 == '-')
        val1 = 1;
    if(opr1 == '/' || opr1 == '*')
        val1 = 2;
    if(opr2 == '+' || opr2 == '-')
        val2 = 1;
    if(opr2 == '*' || opr2 == '/')
        val2 = 2;
    return (val1 >= val2);
}

//Definition of storeInfix function
void infixToPostfix::storeInfix(string s)
{
    ifx = s;
}

//Definition of showInfix function
void infixToPostfix::showInfix()
{
    cout << ifx << endl;
}

//Definition of showPostfix function
void infixToPostfix::showPostfix()
{
    cout << pfx << endl;
}

//Definition of converToPostfix function
void infixToPostfix::convertToPostfix()
{
    int len = ifx.length();
    char temp;
    stack<char> stack;

    for(int i = 0; i < len; i++)
    {
        if(ifx[i] == '+' || ifx[i] == '-' || ifx[i] == '*' || ifx[i] == '/')
        {
            while(!stack.empty() && stack.top() != '(')
            {
                if(precedence(stack.top(), ifx[i]))
                {
                   stack.pop(temp);
                   pfx += temp;
                }
                else break;

            }
            stack.push(ifx[i]);
        }
        else if(ifx[i] == '(')
            stack.push('(');
        else if(ifx[i] == ')')
        {
            while(!stack.empty() && stack.top() != '(')
            {
                stack.pop(temp);
                pfx += temp;
            }
            if(!stack.empty())
                stack.pop();
        }

        else if(ifx[i] >= 'A' && ifx[i] <= 'Z')
            pfx += ifx[i];
    }

    while(!stack.empty())
    {
        stack.pop(temp);
        pfx += temp;
    }
}



#endif 


How will I go about creating the convertToPostfix function? Any help is greatly appreciated.
http://en.wikipedia.org/wiki/Shunting-yard_algorithm should do.

EDIT: infixToPostfix::precedence() can and should be simplified.
Last edited on
I get what the algorithm is supposed to do, my issue is that I'm getting errors while popping the temp char.

1
2
3
4
 infixToPostfix.h:89: error: no matching function for call to `std::stack<char, std::deque<char, std::allocator<char> > >::pop(char&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_stack.h:206: note: candidates are: void std::stack<_Tp, _Sequence>::pop() [with _Tp = char, _Sequence = std::deque<char, std::allocator<char> >]
infixToPostfix.h:103: error: no matching function for call to `std::stack<char, std::deque<char, std::allocator<char> > >::pop(char&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_stack.h:206: note: candidates are: void std::stack<_Tp, _Sequence>::pop() [with _Tp = char, _Sequence = std::deque<char, std::allocator<char> >]


Am I defining the temp char properly? I'm getting more confused as I fiddle with this. From the error I'm thinking that stack.pop(temp); is illegal, or am I concatenating it improperly?

It seems you're not calling pop() properly.
http://www.cplusplus.com/reference/stl/stack/pop/
pop() only removed the top element from the stack. If you need to also get this value, you need to first call top():
1
2
3
4
std::stack<int> s;
s.push(20);
int a=s.top();
s.pop(); //no parameters 


Usually, std::vectors are more versatile than std::stacks, while keeping the same functionality with push_back() and pop_back().
You know what, I have no idea what I was thinking, but my function works like a charm by simply adding temp = stack.top(); before the pop call that was having errors. Calling stack.pop() followed by pfx += temp; then works fine. Thanks for the simple yet somewhat overlooked step!
Last edited on
Topic archived. No new replies allowed.