Implement program for evaluating infix expressions

If I could get any help, I would appreciate it. Also, can't use namespace std for this.
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

#include<iostream>
#include<stack>
#include<fstream>
#include<iomanip>
#include<queue>
#include<cassert>
#include<string>
#include<cstring>

using std::endl;
using std::cout;
using std::cin;
using std::stack;
using std::string;
using std::ifstream;
using std::queue;
using std::setw;

void readIn();
void testBalanced();
void testEvaluatepostfix();
void testInfixtoPostfix();
double evaluatePostfix(const string& expression);
queue<string> readIn(string fileName);
string infixtoPostfix(const string& expression);
bool isBalanced(const string& expression);

/*
It will read in a infix expression from a text file.check if the parentheses in the input expression are balanced.convert the infix expression into a postfix expression and evaluate the expression.*/

int main()
{
	string expression;
	string postfixExpression;
	double result;

	testBalanced();
	testInfixtoPostfix();
	readIn();
	testEvaluatepostfix();

	queue<string> expressions=readIn("expressions.txt");
	cout<<setw(12)<<"prefix"<<setw(12)<<"postfix"<<setw(12)<<"result"<<endl;
	while(!expressions.empty()){
		expression=expressions.front();
		expressions.pop();
		
		if(isBalanced(expression))
		{
			postfixExpression=infixtoPostfix(expression);
			result=evaluatePostfix(postfixExpression);
			cout << setw(12) << expression << setw(12) << postfixExpression << setw(12) << result << endl;
		}

		else
		{
			cout << setw(12) << expression <<setw(12) << setw(16)<<"Is not balanced"<<endl;
		}
	}
	cout<<"Project end part"<<endl<<endl;

	return 0;
}

bool isBalanced(const string& expression)
{
	stack<char> openPar;
	for (unsigned int i=0; i<expression.length(); i++)
	{
		if(expression[i] == '(')
		{
			openPar.push(expression[i]);
		}

		else(expression[i] == ')')
		{
			if (openPar.empty())
			{
				return false;
			}
			
			else
			{
				openPar.pop();
			}
		}
		
	}

	if(openPar.empty())
	{
		return true;
	}

	else
	{
		return false;
	}
}

void testbalanced()
{
	bool output;
	bool solution;

	cout<<endl;
	cout<<"Test is balanced"<<endl;
	cout<<" output " << setw(11)<<"solution "<<endl;

	output= isBalanced(""); solution = true;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("()"); solution = true;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("(("); solution = false;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("("); solution = false;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced(")"); solution = false;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced(")("); solution = false;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("(())"); solution = true;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("()()"); solution = true;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("()("); solution = false;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("a"); solution = true;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("(a)"); solution = true;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("(a))"); solution = false;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);

	output= isBalanced("(a))("); solution = false;
	cout<< setw(8) << output<< setw(2) << "|"<<setw(8)<<solution<<endl;
	assert(output==solution);


	cout<<"In balanced end part"<<endl;
}

string infixtoPostfix(const string& expression)
{
	stack<int> j; 
	string postfixExpression;
	string prec = "(+*";
	string numbers = "0123456789";
	int checker;
	char top;
	char element;

	for (unsigned int i=0; i<expression.length(); i++)
	{
		element = expression[i];
		checker = numbers.find(element);

		if (checker !=-1)
		{
			postfixExpression += element;
		}

		else if (element == '(')
		{
			j.push(element);
		}

		else if (element == ')')
		{
			top=j.top();
			j.pop();

			//search until there is (
			while (top != '(')
			{
				postfixExpression += top;
				top = j.top();
				j.pop();
			} 
		}
		else
		{
			while (!j.empty())
			{
				postfixExpression += j.top();
				j.pop();
			}

			return postfixExpression;
		}
	}

void testInfixtoPostfix()
{
			cout<<endl;
			cout<<"The infix to Postfix"<<endl;
			cout<<endl;
			string output;
			string solution;

			cout<< " output "<<setw(11)<<"solution"<<endl;

			output = infixtoPostfix("");
			solution = "";
			assert(output ==solution);

			output=infixtoPostfix("1");
			solution="1";
			cout<<setw(8)<<output<<setw(2)<<"|" <<setw(8)<<solution<<endl;
			assert(output==solution);

			output=infixtoPostfix("(1+1)");
			solution = "11+";
			cout<<setw(8)<<output<<setw(2)<<"|"<<setw(8)<<solution<<endl;
			assert(output==solution);

			output=infixtoPostfix("((2+3)*(1+5))");
			solution="23+15+*";
			cout<<setw(8)<<output<<setw(2)<<"|"<<setw(8)<<solution<<endl;
			assert(output==solution);

			output=infixtoPostfix("(1+(2+(3+4)))");
			solution = "1234+++";
			cout<<setw(8)<<output<<setw(2)<<"|"<<setw(8)<<solution<<endl;
			assert(output==solution);

			cout<<"Test ending"<<endl;
}

		double evaluatePostfix(const string& expression)
		{
			string numbers="0123456789";
			string prec="+*";
			stack<int> operands;
			double result;
			int operand;
			int t1; 
			int t2;
			char element;

			for(unsigned int i=0; i<expression.length(); i++)
			{
				element=expresion[i];
				operand numbers.find(element);
				if (operand !=-1)
				{
					operands.push(operand);
				}

				else
				{
					result=0;
					
					t1=operands.top();
					operands.pop();

					t2=operands.pop();

					if(element=='+')
					{
						result += (t1 + t2);
					}

					else
					{
						result +=(t1 * t2);
					}

					operands.push(result);

				}
			}

			return operands.top();
		}

		void testEvaluatePostfix()
		{
			double output;
			double solution;

			cout<<endl;
			cout<<endl;

			cout<<"Test evaluate postfix"<<endl;

			cout<<" output "<<setw(11)<<"solution"<<endl;

			output=evaluatePostfix("11+");
			solution=2;
			cout<<setw(8)<<output<<setw(2)<<"|"<<setw(8)<<solution<<endl;
			assert(output == solution);

			output = evaluatePostfix("12+2*");
			solution=6;
			cout<<setw(8)<<output<<setw(2)<<"|"<<setw(8)<<solution<<endl;
			assert(output == solution);

			cout<<"Ending of evaluate postfix"<<endl;
		}

		queue<string> readIn(string filename)
		{
			string line;
			queue<string> expressions;
			ifstream in_file(filename).c_str()); //Allows the entering of a string

			if(in_file.is_open())
			{
				while(!in_file.eof())
				{
					getline(in_file, line, '\n');

					if(line !="")
					{
						expressions.push(line);
					}
				}

				in_file.close();
			}
			
			else
			{
				cout<<"Unable to open"<<endl;
			}

			return expressions;
		}


		void testreadin()
		{
			string solution;
			queue<string> output;
			cout<<endl;
			cout<<endl;
			cout<<"Tesing read in"<<endl;

			cout<< " output "<<setw(14)<<"solution"<<endl;
			output=readIn("test1.txt");
			solution="(1+2)*(1+1)";
			cout<<setw(12)<<output.front()<<setw(2) << " | "<<setw(8)<<solution<<endl;
			assert(output.front()==solution);
			output.pop();

			ouput=readIn("test2.txt");
			solution="(1+1)";
			cout<<setw(12)<<output.front()<<setw(2)<< " | "<<setw(8)<<solution<<endl;
			assert(ouput.front()==solution);
			output.pop();
			solution="2+2";
			cout<<setw(12)<<output.front()<<setw(2)<<" | "<<setw(8)<<solution<<endl;

			cout<<"End of read in"<<endl;
		}
		}
What is your problem?
What seem to be the problem?
line 214 down seems to have a really big issue. I keep getting errors like:

testInfixtoPostfix : local function definitions are illegal..
Last edited on
Trying doing your testing in a seperate .cpp file
This line end only loop, not the whole function. so next function happens to be inside this one. Add another closing bracket.
Ok I'm going to try. Also, I did the brackets and I am now seeing.

errors for line 330 about the string. I am confused on what I did wrong on that part.

error C2143: syntax error : missing ';' before '.'
So I noticed I was missing a bracket there. So I fixed that. Still errors. But I noticed that mistake.
Which errors? We cannot read your mind and say what wrong.

I see problem on line 76: else(expression[i] == ')') Maybe you meant else if (expression[i] == ')')?
Topic archived. No new replies allowed.