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;
}