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 typeC{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– rodorgas
Unfortunately it has to be in this format :/ Just to make things more difficult
– Jessy