Stack operations

Asked

Viewed 284 times

2

I am trying to do operations using stack but at the time of allocating the results in the previous position in stack is that I am not getting, because the result of 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 A
  2. PUSH B
  3. SUB (Subtraction)
  4. PUSH C
  5. PUSH D
  6. PUSH AND
  7. MPY (Multiplication)
  8. ADD (Addition)
  9. DEC (Decrease)
  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));



}
  • But which operation does not work as desired ?

  • subtraction, multiplication, addition, decrease and division, as I cannot store the value in the previous stack

1 answer

2


Most of the error is actually something very simple and even distracting, which is the fact that it’s 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 goes for all operations that are not push or pop. These functions already return the value so you just need to keep them in printfs, thus:

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

To conclude the logic shown in the example, the decrement is also incomplete because it only returns the decremented element and does not alter 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 amendments I have already mentioned you get the result you expect.

Check it out at Ideone

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

  • The field capa in Pilha is not being used, and therefore allows you to insert 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 should not allow elements to be removed from the queue with pop if there is none, that is to say, if the topo been -1. The way it is if this situation happens will have undefined behavior and a potential Segmentation fault.

  • In the operation div shall prevent division by 0, which in fact was the error that it had initially, since operations were done double and removed extra elements from the pile ending in operations with zeroes.

  • opa came, I was busy these days I was busy and did not give to look more tomorrow I will look your answer carefully =)

  • that right there kkkk, lack of attention from me and not notice a silly mistake like that

Browser other questions tagged

You are not signed in. Login or sign up in order to post.