infix to postfix conversion function error

My input is "A+B-C;" but my function will always output "A+B-C" instead of the correct postfix expression "AB+C-". Am I missing something simple?

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
void Expression::convertInfixToPostfix()
{
    /*Define operator stack*/
	string postfix;
	int size = infixExpression.size();
    stackType<char> s(size);
	s.initializeStack();
	
    for (int iter = 0; iter < size; ++iter)
    {
        switch (infixExpression[iter])
        {
			case '+':
			case '-':
			case '/':
			case '*':

				if (s.empty())
					s.push(infixExpression[iter]);
				else
					
					/*if precedence of current operator is higher than
					one on top of the stack, we simply push the current
					operator to the top of the stack*/
					if (precedence(s, infixExpression[iter]))
							s.push(infixExpression[iter]);
					else
					{
						/*Pop from the stack and append it to string stream.
						Repeat it unless we find an operator on top of the stack
						whose precedence is lower or stack is empty.*/
						do
						{
							postfix += s.top();
							s.pop();
						}while (!s.empty() && !precedence(s, infixExpression[iter]) && s.top() != '(');
							/*Now push the current operator to the stack*/
								s.push(infixExpression[iter]);
						}
					
						break;
				
			case '(':

                s.push(infixExpression[iter]);
				break;
			case ')':
			
				while(s.top() != '(')
				{
					postfix += s.top();
					s.pop();
				}
				s.pop();
				break;
			
			default:
            
                if (isOperand(infixExpression[iter]))
					postfix+= infixExpression[iter];
				break;
	
		}
		/*Pop one by one from operator stack and append it to our stream*/
		while(!s.empty())
		{
			postfix += s.top();
			s.pop();
		}
	}
	postfixExpression = postfix;
}

bool Expression::precedence(stackType<char> s, char operator_specified)
{
	char top_operator = s.top();
    if ((top_operator != '(' && (operator_specified == '+') || (operator_specified == '-')))
        return false;
    if ((top_operator == '+') || (top_operator == '-') || (top_operator == '('))
        return true;
    else
        return false;
}

bool Expression::isOperand(char symbol)
{
    switch (symbol) {
    case '+':
    case '-':
    case '*':
    case '/':
	case '(':
	case ')':
	case ';':
        return false;
    default:
        return true;
    }
}
Topic archived. No new replies allowed.