Infix to Postfix

closed account (jEb91hU5)
I'm having trouble dealing with the parentheses in my code. The operators themselves are working like they are supposed to. I commented a couple places where I have questions within the code itself. Any advice would be appreciated. The input and output is also attached at the bottom.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int maxstack = 50;
const char initstack = ' ';

void createstack(char stack[], int& top)
{
	int i;
	top = -1;
	for (i = 0; i < maxstack; i++) stack[i] = initstack;
}

bool emptystack(int top)
{
	return top < 0;
}

bool fullstack(int top)
{
	return top >= maxstack - 1;
}

void push(char stack[], int& top, char data)
{
	if (!fullstack(top))
	{
		top++;
		stack[top] = data;
	}
	else
		cout << "The stack is full! Can't push value\n";
}

char pop(char stack[], int& top)
{
	char ans = initstack;
	if (!emptystack(top))
	{
		ans = stack[top];
		stack[top] = initstack;
		top--;
	}
	return ans;
}

int priority(char ch)
{
	switch (ch)
	{
	case '+':
	case '-':
		return 1;
		break;
	case '*':
	case '/':
		return 2;
		break;
	default: cout << "Invalid Operator" << endl; // Outputs 9 times, not sure why
	}
	return 0;
}

void readem(char stack[], int& top)
{
	int i, len;
	string instring, outstring;
	char ch;
	ifstream inf;
	ofstream outf;
	inf.open("prog3.dat");
	outf.open("prog3.ot");

	while (!inf.eof())
	{
		inf >> instring;
		outstring.clear();
		len = instring.length();
		for (i = 0; i < len; i++)
		{
			ch = instring[i];
			if (ch >= 'A' && ch <= 'Z')
			{
				outstring += ch;
			}
			else
			{
				if (emptystack(top)) push(stack, top, ch);
				else if (ch == '(') push(stack, top, ch); // Having trouble dealing with parentheses in output
				else if (ch == ')')
				{
					while (!emptystack(top) && stack[top] != '(')
					{
						outstring += stack[top];
						pop(stack, top);
					}
				}
				else if (priority(ch) > priority(stack[top])) push(stack, top, ch);
				else
				{
					while (!emptystack(top) && priority(ch) <= priority(stack[top])) outstring += pop(stack, top);
					push(stack, top, ch);
				}
			}
		}
		while (!emptystack(top)) outstring += pop(stack, top);

		outf << instring << "     " << outstring << endl;
	}
}

void main()
{
	char stack[maxstack];
	int top;
	createstack(stack, top);
	readem(stack, top);
}


Input:

A+B+C
A+B*C
W/X-Y/Z
W+X*Y-Z
A*B*C
A-B/C
A*B-C/D
K/G/Z
Q/S*D
P*D/E*K
A+B*C/D-E
A*B-C+D/E
A+(B*(C-D)/E)
A*(B+C)
A-(B-C)
A*((B*C)-D)
A+(B*C)-D


Output:

A+B+C     AB+C+
A+B*C     ABC*+
W/X-Y/Z     WX/YZ/-
W+X*Y-Z     WXY*+Z-
A*B*C     AB*C*
A-B/C     ABC/-
A*B-C/D     AB*CD/-
K/G/Z     KG/Z/
Q/S*D     QS/D*
P*D/E*K     PD*E/K*
A+B*C/D-E     ABC*D/+E-
A*B-C+D/E     AB*C-DE/+
A+(B*(C-D)/E)     ABCD-E/(*(+
A*(B+C)     ABC+(*
A-(B-C)     ABC-(-
A*((B*C)-D)     ABC*D-((*
A+(B*C)-D     ABC*D-(+

Watch out for case '*' it doesn’t have a break, the default probably outputs nine times because you have it in a while loop.
closed account (jEb91hU5)
That makes sense. Now I'm just having trouble with dealing with the parentheses. Is stack[top] the wrong thing to call when dealing with the top of the stack. Or is there something else that I'm missing in this portion of the code:

1
2
3
4
5
6
7
8
9
10
else if (ch == '(') push(stack, top, ch); // Having trouble dealing with parentheses in output	

else if (ch == ')')
{
	while (!emptystack(top) && stack[top] != '(')
	{
		outstring += stack[top];
		pop(stack, top);
	}
}
Last edited on
Trace through it to see what is wrong.

BTW, if everyone turns in the same code with only minor variations, if I were your professor I’d fail every one. These are all very clearly copy-pasted.
Topic archived. No new replies allowed.