Stack of characters

1
Good night people, okay? I'm with my program here from RPN Calculator and apparently everything is working normal despite great difficulties to work with char stack. I'm missing implementing the power function on my calculator only, but nothing works, and my result always goes out as 0. Any idea? Recalling that the inputs of the problem are N, which is qtd of operation characters and other N characters of the account just below. An example input is: N = 7;E10E2-C (10-2). The 'E' stacks a 0 initially, and then the numbers after it, C prints the result and reboots the stack. Here is 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;
}
    
asked by anonymous 20.10.2016 / 02:43

1 answer

1

Hello! Well I did not read your code completely so I just went through the eye and go straight to where you said the problem was. You said that it is not solving powers, to use a variable in an operation you need before initializing it with some value, its error is that you are using the variable int aux without before initializing it. As you will use it in multiplication operations you need to initialize it with 1, in case of your code you should do this well before iterating the while(cont != x) loop where the aux variable is used and allow it to be redefined with 1 again for each loop interaction for(i=0;i<N;i++) . It would look like this:

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

Now the answer will no longer be zero. I also point out that you are importing libraries unnecessarily, math.h and string.h are deleting your code. I also checked for redundancy in the int v = (int)op section. You do not have to force the conversion to int of the character written to char op because the assignment itself converts it to the ASCII table integer automatically. It will look like this:

int v = op;

When posting more questions, comment your code more, so it will take less time to respond. Take a look at using libraries. Thanks.

    
21.10.2016 / 13:58