Stack operations

1

I am trying to do operations using stack but at the time of allocating the results in the previous position in the stack it is that I am not succeeding because the result of the operation is not allocated in the desired position:

I'm 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

  • PUSH A
  • PUSH B
  • SUB (Subtraction)
  • PUSH C
  • PUSH D
  • PUSH E
  • MPY (Multiplication)
  • ADD (Add)
  • DEC (Decrement)
  • DIV (Division)
  • 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));
    
    
    
    }
    
        
    asked by anonymous 05.11.2018 / 01:56

    1 answer

    1

    Most of the error is actually something very simple and very distracting, which is 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 is true for all operations that are not push or pop . These 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 complete the logic that showed in the example, the decrement was also incomplete because it only returns the decremented element and does not change it in the stack. In this sense, its dec function 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 it on Ideone

    There are several things you can improve on your code, however, I leave only a few to consider:

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

    • Similarly, you should not allow elements of the queue to be stripped with pop if none exists, that is, if topo is already -1 . As it stands if this situation happens it will have undefined behavior and a potential segmentation fault .

    • In the div operation you should prevent the split being made by 0 , which was actually the error you initially had, since operations were done doubling and removing extra elements from the stack ending in zero operations.

    06.11.2018 / 02:18