stack class query


The program below is from a book I'm using to learn programming. While I understand almost every aspect of the code. I have a question.

The program below is meant to implement a Reverse Polish Notation. They use Stack classes to implement the same. The part where I'm confused is ..

In the program you see the name of the stack as "nums"

Now lets say I add another stack class. Although I have commented the second stack class, if I do not comment the second stack class and use the second stack class "numstest" as well, how does the program know the "input_str" is related to the "nums" stack and not the "numstest" stack?

As far as i see, the input string, "input_str" is assigned to the pointer *p.
But I can't see where and how the stack class "nums" is linked to the pointer or the input string. Could anyone open my eyes and show me what I'm missing please?



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
#include <iostream>
#include <cstring>
#include <stack>
#include <cmath>

using namespace std;

#define MAX_CHARS 100

int main(int argc, const char * argv[]) {
    
    stack<double> nums;
//    stack<double> numstest;
 
    char input_str[MAX_CHARS+1], *p;
    int c;
    double a, b, d;
    
    cout << "Enter the RPN string ";
    cin.getline(input_str, MAX_CHARS);
    
    p = strtok(input_str, " ");
    
    while (p) {
        c = p[0];
    
        if ( c == '+' || c == '-' || c == '/' || c == '*') {
            
            if (nums.size()<2) {
                cout << "There are not enough operators " << endl;
                return 0;
            }
            
            a = nums.top();nums.pop();
            b = nums.top();nums.pop();

            switch (c) {
                case '+':
                    d = a + b;
                    break;
                case '-':
                    d = a - b;
                    break;
                case '*':
                    d = a * b;
                    break;
                case '/':
                    d = a / b;
                    break;
            }
            nums.push(d);
        } else {
            nums.push(atof(p));
        }
        p  = strtok(NULL, " ");
    
    }
    cout << nums.top() << endl;
    return 0;
}
I'll do my best.

You can't truly know what is in the stack but you can guess that what you put into it if all else is held constant you will get out of it.

To be clearer, a stack is like an array that can't be indexed into. You are only allow to see the last thing you put into it by the method .peak().

You can assume that what you told it to do, it will do without fail, but what do i know, I'm just a hobbyist.

Here's a url:
http://www.cplusplus.com/reference/stack/stack/?kw=stack

~ Hirokachi
Thank you Hirokachi, but if I have two stacks, how do I know which stack is responsible for holding the input data ? That is the part that confuses me, Because I don't see any line in the code where the input data/string (input_str) is assigned to a stack.
Topic archived. No new replies allowed.