Character stack

Asked

Viewed 1,337 times

1

Good night guys, all right? With my RPN calculator program here and apparently everything is working normal despite great difficulties to work with char stack.Missing implement the potentiation function on my calculator only,but nothing works, and my result always comes out as 0. Any idea? Remember that the problem entries are N, which is Qtd of operation characters and other N characters from the account just below. An example of input is : N = 7;E10E2-C (10-2) . 'E' pushes a 0 initially, and then the numbers after it, C prints the result and reboots the stack. Follow the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define tamanhoMAX 100

typedef struct{
    int topo;
    int elementos[tamanhoMAX];
}Pilha;

void init(Pilha *p);
int Pilha_vazia(Pilha *p);
int Pilha_cheia(Pilha *p);
int push(Pilha *p,char valor);
char pop(Pilha *p);
void show(Pilha *p);

void init(Pilha *p){
    int i;
    for(i=0;i<tamanhoMAX;i++)
        p->elementos[i] = 0;

    p->topo = -1;
}

int Pilha_vazia(Pilha *p){
    return p->topo == -1;
}

int Pilha_cheia(Pilha *p){
    return (p->topo + 1) == tamanhoMAX;
}

int push(Pilha *p,char valor){
    if(Pilha_cheia(p))
        return -1; //aborta a função

    p->topo++;
    p->elementos[p->topo] = valor;
    return 0; //sucesso
}

char pop(Pilha *p){
    char aux;
    if(Pilha_vazia(p) == 1)
        return -1;

    aux = p->elementos[p->topo];
    p->topo--;

    return aux;
}

void show(Pilha *p){
    int i;
    i = p->topo;

    while(i!=-1){
        printf("%d",p->elementos[i]);
        i--;
    }
}

int valor(int c){
   return c-'0';
}

int main(void) {
    int i,j,aux,N;
    Pilha p;
    int x,y,cont=0;
    char op;

    init(&p);

    scanf("%d",&N);
    getchar();

    for(i=0;i<N;i++){
        scanf("%c",&op);
        int v = (int)op;

            switch(v)
            {
            case 67: //correspondente a 'C' na tabela ASCII
                show(&p);
                init(&p);
                break;
            case 69: //correspondente a 'E' na tabela ASCII
                push(&p,0);
                break;
            case 43: //correspondente a '+' na tabela ASCII
                y=pop(&p);
                x=pop(&p);
                push(&p,x+y);
                break;
            case 45: //correspondente a '-' na tabela ASCII
                y=pop(&p);
                x=pop(&p);
                push(&p,x-y);
                break;
            case 42: //correspondente a '*' na tabela ASCII
                y=pop(&p);
                x=pop(&p);
                push(&p,x*y);
                break;
            case 47://correspondente a '/' na tabela ASCII
                y=pop(&p);
                x=pop(&p);
                push(&p,x/y);
                break;
            case '^': //o valor 94 aqui não dava certo tbm :/
                y=pop(&p);
                x=pop(&p);
                while(cont != x){
                    aux = aux*y;
                    cont++;
                }
                push(&p,aux);
                break;

            default:
                x=valor(v);
                y=pop(&p);
                push(&p,y*10+x);
                break;
            }
    }
    return 0;
}
  • The entry must be in the format 7{Enter}E10E2-C{Enter}? Typically, you should press enter instead of type E, and you wouldn’t need to type C{Enter} because the calculator understands that the operation symbol means "calculate the result". It is a little difficult to predict the behavior of the calculator if it leaves the standard without having another model very well defined. For reference, see an online rpn calculator and the wikipedia article

  • Unfortunately it has to be in this format :/ Just to make things more difficult

1 answer

1

Hello! Well, I didn’t read your code completely, so I figured I’d just pass my eye and go right to where you said the problem was. You said that it is not solving potentials, to use a variable in an operation you need before initializing it with some value, your mistake is that you are using the variable int aux without first initializing it. As you will use it in multiplication operations you need to initialize it with 1, in the case of your code you should do this well before loop iteration while(cont != x) where the aux variable is used and allow it to be reset with 1 again to each loop interaction for(i=0;i<N;i++). So it would be:

...
for(i=0;i<N;i++){
 ...
 aux = 1; // <--- 
 while(cont != x){
   aux = aux*y;
   cont++;
 }
...
}

Now the answer won’t be zero anymore. Rebound also that you are importing libraries without need, the math.h and string.h They’re soiling your code. I also checked a redundancy on the stretch int v = (int)op. You do not need to force the conversion to int of the character saved in char op because the assignment itself makes the conversion of it to the whole of the ASCII table automatically. Will stay like this:

int v = op;

When you post more questions comment on your code, it will decrease the amount of time it takes to answer. Do a research on the use of libraries. Hug.

  • Thank you so much for the reply J. Carvalho, but unfortunately my code is still printing 0. I really don’t understand his problem. Anyway, thanks for the library tips and td the most, hugs!

Browser other questions tagged

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