Doubts about C ++ batteries

2

I have a question with a battery exercise, whose code is the one you have below, what I want to ask is why the operands are unstacked , they are popped, before operating with them ? I mean this:

const int r_op = stack_.top();
stack_.pop(); // aqui está mi duda, se le hace un pop antes de operar con él?

And another doubt, it is assumed that to stack an element you have to resort to the push, that is different to doing this? const int r_op = stack_.top(); if you could explain that to me. Thanks

void operate(char operador){ //operando derecha,izquierda, lo mete en la pila, cuando terminamos, con pop sacas lo que hay en la pila, Y QUÍ ES DONDE SE OPERA CON ELLOS, SE GUARDA EL 4 EN R_OP, Que ya de por sí está en el top, luego se le elimina, se le hace un pop, y el 3 pasa a ser el top de la pila, se guarda en left_op, y luego se elimina de la pila con pop

    const int r_op = stack_.top(); //añadido, right es el primer elemento en salir en la pila, recuerda el símil de la caja, operamos de izquierda a derecha
    stack_.pop();                   
    const int l_op = stack_.top();
    stack_.pop();

    int result = 0;
    switch (operador) {
        case '+': result = l_op + r_op; break;
        case '-': result = l_op - r_op; break;
        case '*': result = l_op * r_op; break;
        case '/': result = l_op / r_op; break;
        /* ... */                          
    }
    stack_.push(result);   
}

Thanks

    
asked by AER 20.07.2017 в 19:16
source

2 answers

1

It is necessary to explain the context where this code is used but I also infer that it is part of a compiler or interpreter and the stack is used to solve arithmetic operations. The idea is that when the function / method void operate() is invoked, at the top of the stack are stored the two operands, for example 3 below and 4 above that came from an expression such as for example:

  

3 + 4

Since the idea is to solve the expression, the two operands in the stack must be eliminated and replaced by the result (7 = 3 + 4). I understand that in this stack implementation, the primitive top() returns the value that is at the top of the stack but does not delete it, and for that you need the primitive pop() . Finally push(result) what it does is put the result of the operation at the top of the stack (in replacement of the two operands that were already eliminated), ie the inverse of top() and pop() .

    
answered by 20.07.2017 / 19:43
source
2

Even if you do not believe it, your question is agnostic about the language you are using ..

In your case, notice that this class uses top to see the first item that is in the stack, pop to get it out and push to put an element. Then, as pop in the case that you are showing does not return the item you got, you must first use top to see what is on the tip of the stack. and pop later to remove it (because a stack can not be traversed).

Your second question does not make sense

const int r_op = stack_.top();

Makes you keep what is on the tip of the stack in r_op. with push you insert something in the stack.

Another note : I do not know why you define the variables that you remove from the stack as const. Some expert will surely be able to explain you better, but those are not constant.

    
answered by 20.07.2017 в 19:22