Help with infix/postfix

I'm writing a program that reads in a mathematical expression from a user ands stores the expression in a string, then it parses the expression into tokens and stores each token in a queue, then it converts the infix expression to postfix. Everything works except when I have a variable. If i try using a variable it gives me an "Expression: deque iterator not dereferencable" error. Also if I try using a power, for example, 2^2, I get "Expression: deque empty before pop" error.

Any help I can get would be appreciated. Thank you.

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <iostream>
#include <string>
#include <cctype>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;


bool isoperator(char symbol)
{
	if (symbol == '+' ||
		symbol == '-' ||
		symbol == '*' ||
		symbol == '/' ||
		symbol == '^')
		return true;
	
	else
		return false;
}

int priority(char ch)
{
	switch (ch)
	{
		case '(': return 0; 
			break;
		case '+':
		case '-': return 1; 
			break;
		case '*':
		case '/': return 2; 
			break;
		case '^': return 3; 
			break;
		default: return 0; 
			break;
	}
}

void processToInfix(string expression, queue<string>& infix)
{
	int lcount = 0, rcount = 0, i = 0, len = expression.length( ), letterIndex;
	string value;
	char ch;
	string token = "";
	string variable[26];

	for (int j = 0; j < 26; j++)
		variable[j] = "?";

	if(expression == "")
		infix.push("0");

	while(i < len)
	{
		ch = expression[i];
		token = "";

		if(isdigit(ch) || ch == '.')
		{
			while (i < len && (isdigit(expression[i]) || expression[i] == '.'))
			{
				token += ch;
				i++;
				if (i != len)
					ch = expression[i];
			}
				
			infix.push(token);
		}

		else if ((i < len) && (isalpha(ch)))
		{
			ch = toupper(ch);
			letterIndex = ch - 65;

			if (variable[letterIndex] == "?")
			{
				cout << "Enter the value of " << ch << ": ";
				cin >> value;
				infix.push(variable[letterIndex]);
			}
			else
				infix.push(variable[letterIndex]);
			
			i++;
		}

		else if ((i < len) && (isoperator(ch) || ch == '(' || ch == ')'))
		{
			token = ch;
			infix.push(token);
			i++;

			if (ch == '(')
				lcount++;
			else if (ch == ')')
				rcount++;
		}

		else if ((i < len) &&  (ch == ' '))
			i++;

		else
		{
			cout << "Error! Invalid symbol!" << endl;
			system("pause");
			exit(-1);
		}
	}

	if (isoperator(infix.back( )[0]) || isoperator(infix.front( )[0]))
	{
		cout << endl << "Error! Invalid expression!" << endl;
		system ("pause");
		exit(-1);
	}

	if (rcount != lcount)
	{
		cout << endl << "Error! Mismatch parenthesis!" << endl;
		system ("pause");
		exit (-1);
	}
}	


void convertToPostfix(queue<string> infix, queue<string>& postfix)
{
	string token;
	stack<string> operators;


	if (infix.empty( ))
		return;

	do
	{
		token = infix.front( );
		infix.pop( );

		if (token == "(")
			operators.push(token);
	
		else if (isdigit(token[0]))
			postfix.push(token);

		else if (token == ")")
		{
			do
			{
				postfix.push(operators.top( ));
				operators.pop( );
			} 
			
			while(operators.top( ) != "(");
				operators.pop( );
		}

		else if (isoperator(token[0]))
		{
			if (operators.empty( ))
				operators.push(token);

			else if (priority(operators.top( )[0]) < priority(token[0]))
				operators.push(token);
			
			else
			{
				do
				{
					postfix.push(operators.top( ));
					operators.pop( );
				} 
				
				while (!operators.empty( ) && (priority(operators.top( )[0]) > priority(token[0]) || priority(operators.top( )[0]) == priority(token[0])));
					operators.push(token);
			}
		}
	} 
	
	while(!infix.empty( ));

	
	while (!operators.empty( ))
	{
		if (operators.top( ) == "(")
		{
			cout << endl << "Error! Parenthesis mismatch!" << endl;
			exit (-1);
		}
		
		postfix.push(operators.top( ));
		operators.pop( );
	}
}


double evaluate(queue<string>& postfix)
{
	double val1 = 0.0, val2 = 0.0, answer;
	string token;
	stack <double> results;


	if (postfix.empty( ))
		return 0;

	do
	{
		token = postfix.front( );
		postfix.pop( );

		if (isdigit(token[0]))
		{
			results.push(atof(token.c_str( )));
		}

		else if (isoperator(token[0]))
		{
			switch(token[0])
			{
				case('+'):
				val2 = results.top( );
				results.pop( );
				val1 = results.top( );
				results.pop( );
				results.push(val1 + val2);
				break;

				case('-'):
				val2 = results.top( );
				results.pop( );
				val1 = results.top( );
				results.pop( );
				results.push(val1 - val2);
				break;

				case('*'):
				val2 = results.top( );
				results.pop( );
				val1 = results.top( );
				results.pop( );
				results.push(val1 * val2);
				break;

				case('/'):
				val2 = results.top( );
				results.pop( );
				val1 = results.top( );
				results.pop( );
				if (val2 == 0)
				{
					cout << endl << "Error! Division by zero!" << endl;
					exit(-1);
				}
				else
					results.push(val1 / val2);
				break;

				case('^'):
				val2 = results.top( );
				postfix.pop( );
				val1 = results.top( );
				postfix.pop( );
				results.push(pow(val1, val2));
				break;
			}
		}
	} 
	
	while (!postfix.empty( ));
		answer = results.top( );

	results.pop( );

	return answer;
}

int main( )
{
	string expression;
	queue <string> infix;
	queue <string> postfix;

	cout << "Please enter an expression: ";
	getline(cin, expression);

	processToInfix(expression, infix);

	convertToPostfix(infix, postfix);

	cout << endl << "The result is: " << evaluate(postfix) << endl << endl;

	return 0;
}
I get "Expression: deque empty before pop" error.

Lines 265 & 267 - aren't you removing from the wrong container?
Wow I can't believe I did that. Thank you so much. Now the only problem left is with variables.
Line 83 - shouldn't you be pushing "value" onto infix here? I don't see where you use "value" anywhere.


edit: looks like you just need to assign value to variable[letterIndex] before the push onto infix.
Last edited on
Topic archived. No new replies allowed.