infix to postfix

hi everybody, im not sure whats wrong with my program maybe some more expirience eyes will help, im pretty sure i followed the algorithm correctly, it looks like it does it correctly but it outputs an extra character at the end. thanks for any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef stack_h
#define stack_h

const int MAX_ITEMS = 1000;

class stack
{
public:
	stack();
	bool isEmpty();
	bool isFull();
	void push(char item);
	char pop();
	char getTop();

private:
	int top;
	char items[MAX_ITEMS];
};

#endif 



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

#include"stack.h"
#include<iostream>

using namespace std;

stack::stack()
{
	top = -1;
}

bool stack::isEmpty()
{
	if (top == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool stack::isFull()
{
	if (top == MAX_ITEMS - 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void stack::push(char item)
{
	if (isFull())
	{
	}
	else
	{
		top++;
		items[top] = item;
	}
}

char stack::pop()
{
	char popped;

	if (isEmpty())
	{
		return 1;
	}
	else
	{
		popped = items[top];
		top--;
	}

	return popped;
}

char stack::getTop()
{
	if (isEmpty())
	{
		cout << "Stack is empty." << endl;
		return 1;
	}
	else
	{
		return items[top];
	}
}



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

#include"stack.h"
#include<iostream>

using namespace std;

int precedence(char op);
bool operation(char op);

int main()
{
	char temp;
	char pre;
	char post[50];
	int x = 0;

	stack s;

	cout << "Enter a mathematical equation:" << endl;
	cin.get(pre);

	while (pre != '\n')
	{
		while (pre == ' ')
		{
			cin.get(pre);
		}
		if (isdigit(pre) || isalpha(pre))
		{
			while (isdigit(pre) || isalpha(pre))
			{
				post[x] = pre;
				x++;
				cin.get(pre);
			}
		}
		if (pre == '(')
		{
			s.push(pre);
			cin.get(pre);
		}
		if (pre == ')')
		{
			temp = s.pop();

			while (temp != '(')
			{
				post[x] = temp;
				x++;
				temp = s.pop();
			}
			cin.get(pre);
		}
		if (operation(pre))
		{
			if (s.isEmpty())
			{
				s.push(pre);
			}
			else
			{
				temp = s.pop();

				while (precedence(temp) >= precedence(pre))
				{
					post[x] = temp;
					x++;
					temp = s.pop();
				}
				s.push(temp);
				s.push(pre);
			}
			cin.get(pre);
		}
	}

	while (!(s.isEmpty()))
	{
		temp = s.pop();
		post[x] = temp;
		x++;
	}

	cout << "The postfix expression is:" << endl;

	for (int a = 0; a < x; a++)
	{
		cout << post[a];
	}

	cout << endl;

	return 0;
}


int precedence(char op)
{
	int priority;
	priority = 0;

	if (op == '^^')
	{
		priority = 3;
	}
	else
		if (op == '*' || op == '/')
		{
			priority = 2;
		}
		else
			if (op == '+' || op == '-')
			{
				priority = 1;
			}

	return priority;
}

bool operation(char op)
{
	if (op == '+' || op == '-' || op == '*' || op == '/' || op == '^^')
	{
		return true;
	}
	else
	{
		return false;
	}
}
...?
Topic archived. No new replies allowed.