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