Stack operations

I am trying to do operations using stack but when it comes to allocating the results in the previous position in the stack is that I am not succeeding, since the result of the operation is not allocated in the desired position:

I am trying to store in the previous position using but not the pointer does not pass the value.

      p->topo--;
      p->pElem [p->topo]=calc;

Order of operations

  1. PUSH TO
  2. PUSH B
  3. subtraction
  4. PUSH C
  5. PUSH D
  6. PUSH AND
  7. MPY (multiplication)
  8. ADD
  9. DEC (decrement)
  10. DIV (Division)
  11. POP F

Operations:

A=9 B=3 C=2 D=1 E=1                                                            1
                                                                  1            1
                      3         3                    2            2            2
PUSH A  9 ->  PUSH B  9 -> SUB  9 (9-3) 6 -> PUSH C  6  -> PUSH D 6 -> PUSH E  6 

      1  
      1        1         1        
      2        2         2        3         3       2        2
 MPY  6 (1x1)  6 -> ADD  6  (2+1) 6 -> DEC  6 (3-1) 6 -> DIV 6 (6/2) 3 -> POP F 3    

Code:

#include<stdio.h>
#include<stdlib.h>


struct Pilha {

    int topo;
    int capa;
    float *pElem;

};

void criarpilha( struct Pilha *p, int c ){

   p->topo = -1;
   p->capa = c;
   p->pElem = (float*) malloc (c * sizeof(float));

}


void push ( struct Pilha *p, float v){

    p->topo++;
    p->pElem [p->topo] = v;

}

float sub(struct Pilha *p)
{
  int x,y;
  float calc;
  p->topo--;
  x=p->pElem [p->topo];
  p->topo++;
  y=p->pElem [p->topo];
  calc=x-y;
  p->topo--;
  p->pElem [p->topo]=calc;

  return calc;

}

float mpy(struct Pilha *p)
{
  int x,y;
  float calc;
  p->topo--;
  x=p->pElem [p->topo];
  p->topo++;
  y=p->pElem [p->topo];

  calc=x*y;

  p->topo--;
  p->pElem [p->topo]=calc;

  return calc;
}

float add(struct Pilha *p)
{
  int x,y;
  float calc;
  p->topo--;
  x=p->pElem [p->topo];
  p->topo++;
  y=p->pElem [p->topo];
  calc=x+y;
  p->topo--;
  p->pElem [p->topo]=calc;


  return calc;
}


float Div(struct Pilha *p)
{
  int x,y;
  float calc;
  p->topo--;
  x=p->pElem [p->topo];
  p->topo++;
  y=p->pElem [p->topo];
  calc=x/y;
  p->topo--;
  p->pElem [p->topo]=calc;

  return calc;
}

float dec(struct Pilha *p)
{
  int x;
  x=p->pElem [p->topo];
  x--;
  return x;
}


float  pop ( struct Pilha *p ){

   float aux = p->pElem [p->topo];
   p->topo--;
   return aux;

}

float monstrarpilha ( struct Pilha *p ){

   return p->pElem [p->topo];

}

int main(){

    struct Pilha p;
    int capacidade=4;
    float valor;
    int A=9,B=3,C=2,D=1,E=1;

    criarpilha (&p, capacidade);

    push(&p,A);
    printf("\nPUSH A: %.1f\n",monstrarpilha(&p));

    push(&p,B);
    printf("\nPUSH B: %.1f\n",monstrarpilha(&p));

    sub(&p);
    printf("\nSubtracao: %.1f\n",sub(&p));

    push(&p,C);
    printf("\nPUSH C: %.1f\n",monstrarpilha(&p));

    push(&p,D);
    printf("\nPUSH D: %.1f\n",monstrarpilha(&p));

    push(&p,E);
    printf("\nPUSH E: %.1f\n",monstrarpilha(&p));

    mpy(&p);
    printf("\nmultiplicacao: %.1f\n",mpy(&p));

    add(&p);
    printf("\nadicao: %.1f\n",add(&p));

    dec(&p);
    printf("\ndecrementar: %.1f\n",dec(&p));

    Div(&p);
    printf("\ndivisao: %.1f\n",Div(&p));

    printf("\nPOP F%.1f\n",pop(&p));



}
Author: FZero, 2018-11-05

1 answers

Most of the error is actually something very simple and distracting, which is the fact that you are calling the same function more than once:

mpy(&p); // <-- mpy aqui
printf("\nmultiplicacao: %.1f\n",mpy(&p) /* <-- e aqui*/);

add(&p); // <-- add aqui
printf("\nadicao: %.1f\n",add(&p) /* <-- e aqui */);

And the same happens for all operations that are not push or pop. These your functions already return the value so you just need to keep them in printfs, like this:

printf("\nmultiplicacao: %.1f\n",mpy(&p));
printf("\nadicao: %.1f\n",add(&p));

To conclude the logic that showed in the example, decrement was also incomplete because it only returns the decremented element and does not change it in the stack. In this sense its function dec should become:

float dec(struct Pilha *p) {
    int x;
    p->pElem[p->topo]--;
    return p->pElem[p->topo];
}

With these two changes I mentioned you already get the result you expect.

See Ideone

There are several things that can improve in the code, however I leave only a few to consider:

  • The capa field in Pilha is not being used, and therefore allows you to enter more elements than the indicated capacity. Even the name itself is not very good, once I only realized what it was for when I looked at the function that builds the stack.

  • Similarly, you should not allow elements to be removed from the queue with pop if there is none, i.e. if topo is already -1. The way it is if this situation happens it will have undefined behavior and a potential segmentation fault.

  • In operation div it must prevent division from being done by 0, which was actually the error which it had initially, since operations were done to fold and removed extra elements from the stack ending in operations with zeros.

 2
Author: Isac, 2018-11-06 01:18:50