Infix to Postfix conversion using Stack - Problem in pop function

Hey everybody, following 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
#include <iostream>
#include <string>
using namespace std;
#define MAX 100
class Stack
{
	char arr[MAX];
	int top;
public:
	Stack();
	void push(char);
	char pop();
	bool isFull();
	bool isEmpty();
	char peek();
};
Stack::Stack() { top = -1; }
bool Stack::isFull()
{
	if (top > MAX)
		return true;
	return false;
}
bool Stack::isEmpty()
{
	if (top < 0)
		return true;
	return false;
}
void Stack::push(char a)
{
	if (isFull())
	{
		cout << "Stack is Full" << endl;
		return;
	}
	arr[++top] = a;
}
char Stack::pop()
{
	if (isEmpty())
	{
		cout << "Stack is Empty" << endl;
		return -1;
	}
	return arr[top--];
}
char Stack::peek()
{
	if (isEmpty())
	{
		cout << "No Element in Stack to peek at" << endl;
		return -1;
	}
	return arr[top];
}
int Check_Precedence_Order(char);
int main()
{
	int k = 0;
	char Input[100];
	cout << "Enter Expression: ";
	cin >> Input;
	while (Input[k++] != '\0');
	Input[k + 1] = '\0';
	Input[k--] = ')';
	for (k; k > 0; k--)
		Input[k] = Input[k - 1];
	Input[k] = '(';
	char Output[100];
	int j = 0;
	char c;
	Stack obj;
	for (int i = 0; Input[i] != '\0'; i++)
	{
		if (Input[i] == '(')
		{
			obj.push(Input[i]);
		}
		else if ((Input[i] >= 'a' && Input[i] <= 'z') || (Input[i] >= 'A' && Input[i] <= 'Z'))
		{
			Output[j++] = Input[i];
		}

		else if (Input[i] == '+' || Input[i] == '-' || Input[i] == '*' || Input[i] == '/' || Input[i] == '^')
		{
			while (obj.peek() == '+' || obj.peek() == '-' || obj.peek() == '*' || obj.peek() == '/' || obj.peek() == '^')
			{
				if (Check_Precedence_Order(obj.peek()) >= Check_Precedence_Order(Input[i]))
				{
					Output[j++] = obj.pop();
				}
				else
					break;
			}
			obj.push(Input[i]);
		}
		else if (Input[i] == ')')
		{
			while (c = obj.pop() != '(')
			{
				Output[j++] = c;
			}
		}
	}
	Output[j] = '\0';
	cout << Output << endl << endl;
	system("pause");
	return 0;
}
int Check_Precedence_Order(char p)
{
	switch (p)
	{
	case '^':
		return 3;
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	default:
		return 0;
	}
}



Here, I am trying to convert infix expression into postfix expression (I am also adding a ( at the start and ) at the end to make sure its a fully paranthesised equation)
The program works fine however, the operators when returned turns into a garbage value or something like that. I have failed to find the mistake I made in the program and need somebody's help to please provide it for me...

Here's the output that I am getting.

1
2
3
4
Enter Expression: (a+b)*c
abc

Press any key to continue . . .


However, it should be like this,
1
2
3
4
Enter Expression: (a+b)*c
ab+c*

Press any key to continue . . .
The issue is with the loop on line 100, it should be this:

1
2
3
4
while ((c = obj.pop()), c != '(')
{
	Output[j++] = c;
}


The issue with your original loop is that you're checking if (c = obj.pop()) != '(' - which doesn't make sense. You want to set "c" THEN check if it's equal to something.

Even better, you could do this:

1
2
3
4
5
while (obj.peek() != '(')
{
	c = obj.pop();
	Output[j++] = c;
}


So there's less vagueness


EDIT:

Here's why the loop condition caused this issue:

while (c = obj.pop() != '(')

^That will NOT make c = obj.pop() THEN check if not-equal to '('. This code actually first will do the "!=" check THEN will assign "c" ... to a bool. Once "obj.pop() != '('" is evaluated to true or false, "c" will then become assigned "true" or "false" - outputting the symbol you were getting.
Last edited on
Topic archived. No new replies allowed.