Reverse Polish Notation

Im having trouble grasping the concept of RPN. I have to read it and evaluate using stack


1
2
3
4
5
6
7
  // im reading it in as a string
         string prefix;
	cout << "Enter: ";
	cin >> prefix;
 // then I convert it to integer
      atoi(prefix.c_str())
 

but then how do i differentiate from a number and an operand?
Any help would be appreciated
Last edited on
so nothing?
lookt at the reference of ctype, like this:

http://www.cplusplus.com/reference/cctype/isdigit/

you need a loop for your string prefix:
1
2
3
4
5
6
7
8
for(size_t i = 0; i < prefix.size(); ++i)
{
  if(isdigit(prefix[i]))
    ...
  else if('+' == prefix[i])
    ...
  else
}
ok thanks for pushing me in the right direction coder777
1
2
3
4
5
6
7
8
9
10
11
12
if(isdigit(prefix[i]))
{
 stack.push(prefix);
}
else if('+' == prefix[i])
{
operandstack.push(prefix);
}
else if('-' == prefix[i])
.
.
.

would it be something like that?
can someone help me convert this to take n amount of numbers.
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
double value1; double value2, value3;

				if('+' == str[i])
					{ 
						value2 = calculate.top();
						calculate.pop();
						value1 = calculate.top();
						calculate.pop();
						value3 = calculate.top();
						calculate.pop();
						calculate.push(value1 + value2 + value3);
						cout << "no" ;
					}
					else if('-' == str[i])
					{
					
						value2 = calculate.top();
						calculate.pop();
						value1 = calculate.top();
						calculate.pop();
						calculate.push(value1- value2);
					}
					else if( '*'== str[i])
					{
						value2 = calculate.top();
						calculate.pop();
						value1 = calculate.top();
						calculate.pop();
						calculate.push(value1*value2);
					}
					else if( '/' == str[i])
					{
						value2 = calculate.top();
						calculate.pop();
						if (value2 == 0)
						{undefined = true;}
					
					else
					
						value1 = calculate.top();
						calculate.pop();
						calculate.push(value1 / value2);
					
					}
Topic archived. No new replies allowed.