Postfix Evaluation

Hi,
I'm supposed to write a program for school that performs postfix evaluation.
It's going pretty good except I've run into a small problem. When I try to print my final value, numStack.top(), I'm just getting garbage. I know this is because it's empty, and it's empty because of line 38 in my second IF statement. I can't seem to come up with a way around this issue.
Any sort of help would be appreciated.

Thanks,
~Aaron

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
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stack>
#include <stdio.h>
#include <math.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string expression = "6+5";
	int i, intValue, answer;
	float x, y;
	bool endOfFile = false, canCalc = false;
	stack<int> numStack;

	//while end of file isn't reached...
	while(endOfFile == false)
	{		
		for(i = 0; i <= expression.length() - 1; i++)
		{
			//if the current character is a digit, convert it to an int and push in into the stack
			if(isdigit(expression[i]))
			{
				stringstream ss(expression[i]);
				ss >> intValue;
				numStack.push(intValue);
			}	
			else
			{	
				if(numStack.top() >= 0)
				{
					x = numStack.top();
						numStack.pop();					
					if(numStack.top() >= 0)
					{
						y = numStack.top();
							numStack.pop();
							if(expression[i] == '+')
							{answer = y + x;}
							if(expression[i] == '-')
							{answer = y - x;}
							if(expression[i] == '/')
							{answer = y / x;}
							if(expression[i] == '*')
							{answer = y * x;}
							if(expression[i] == '^')
							{answer = pow(y, x);}
							numStack.push(answer);
					}				
				}
			}			
		}
	
		//exit loop if end of file is reached
		if(i == expression.size())
		{
			endOfFile = true;
		}
	}

	//print solution
	cout << numStack.top() << endl;
	

	system("PAUSE");
	return 0;
}
I think you have more than a little problem:

As it is, your program only interprets single digit integers, and does not accept any separator (whiespace).You might want to reconsider this.
Moreover, I don't see the use of that while loop.

ps: on the style, most people prefer a switch (...) case... statement rather thant so many ifs
btw, you know that "6+5" is not postfix, right ? So raising an exception should not be a surprise
Aaron Arber wrote:
I can't seem to come up with a way around this issue.
Simply use the function if(!numStack.empty())... of stack to determine whether it's empty.

Last edited on
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
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stack>
#include <stdio.h>
#include <math.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string expression = "6+5";
	int i, intValue, answer;
	float x, y; 
	int num1 , num2 ; 
	bool endOfFile = false, canCalc = false;
	stack<int> numStack;

	//while end of file isn't reached...
	while(endOfFile == false)
	{		
		for(i = 0; i <= expression.length() - 1; i++)
		{
			//if the current character is a digit, convert it to an int and push in into the stack
			if(isdigit(expression[i]))
			{
				stringstream ss(expression[i]);
				ss >> intValue;
				numStack.push(intValue);
			}	
		}
	}
	while(endOfFile == false)
	{		
		for(i = 0; i <= expression.length() - 1; i++)
		{
			//if the current character is a digit, convert it to an int and push in into the stack
			if(!isdigit(expression[i]) &&(numStack.top() >= 0 )
			{
				x = numStack.top();
						numStack.pop();			
					y  =   numStack.top() ;
							numStack.pop();		
                      	if(expression[i] == '+')
							{answer = y + x;}
							if(expression[i] == '-')
							{answer = y - x;}
							if(expression[i] == '/')
							{answer = y / x;}
							if(expression[i] == '*')
							{answer = y * x;}
							if(expression[i] == '^')
							{answer = pow(y, x);}
							numStack.push(answer);
			}	
		}
	}
	
		//exit loop if end of file is reached
		if(i == expression.size())
		{
			endOfFile = true;
		}
	}

	//print solution
	if( numStack.top() > 0 ) 
	cout << numStack.top() << endl;
	

	system("PAUSE");
	return 0;
}
Maybe there's a misunderstanding what top() does?

top() Returns a mutable reference to the element at the top of the stack. Precondition: empty() is false.
otherwise it crashes
may be i dont understand .. sorry for my lack of knowledge ..
How does it works in this above program.
Thank you for the responses everyone.

@johnbob I know about everything it doesn't do. I'm still in the first steps of this program and I haven't taken time to worry about all the details yet. I'm only asking about what I stated specifically.
Last edited on
I'm just wondering. What's the intention of (numStack.top() >= 0)?

@Aaron Arber
Your problem is that after you evaluate the first num you assume that the second value is already on the stack. Which isn't the case

@bluecoder
Your code work for this paricular expression (but doesn't compile due to the missing bracket on line 38)
Hi coder777,
Yes, I know this is the problem. I stated that I knew this was the problem in my post.
Thank you for your response.
thanks @coder777 ..
Hm, I see that inherent logic is not so easy to get. I tweaked it so that it works (and a bit error handling):

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
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stack>
#include <stdio.h>
#include <math.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string expression = "6+5";
	string::size_type i;
	int intValue;
	float x, y;
	float answer;
	stack<float> numStack;

	char op = 0; // remember the operation symbol
	for(i = 0; i < expression.length(); i++)
	{
		//if the current character is a digit, convert it to an int and push in into the stack
		if(isdigit(expression[i]))
		{
			intValue = (expression[i] - '0'); // no need for a stringstream, really
			if(numStack.empty())
				numStack.push(intValue);
			else
			{
				x = intValue;
				y = numStack.top();
				if(op == '+')
					{answer = y + x;}
				else if(op == '-')
					{answer = y - x;}
				else if(op == '/')
					{answer = y / x;}
				else if(op == '*')
					{answer = y * x;}
				else if(op == '^')
					{answer = pow(y, x);}
				else
					break;
				numStack.push(answer);
			}
		}
		else // if(IsValidOp(expression[i]))
			op = expression[i];
		// else
			//  break;
	}
	if(i == expression.length())
	{
		if(numStack.empty())
			cout << "No result" << endl;
		else //print solution
			cout << numStack.top() << endl;
	}
	else
		cout << "Error at position " << i << endl;
	

	system("PAUSE");
	return 0;
}
Topic archived. No new replies allowed.