I did a RPN, but doesn't show the result and the steps, before was appearing, I don't understand what is happening and there are no errors thrown by compiler either.
I do always this example:
3.2 1.8 - 10 / 2 + .
4 steps = 2.14
I really don't know what's wrong ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
// quando dá enter ele fecha
// não está imprimindo o resultado O__O
typedef double pilha_t;
#define PILHA_VAZIA INT_MIN
#define N 100
#define TRUE 1
#define FALSE 0
struct Pilha {
pilha_t pilha[N];
int tamanho;
};
void inicializar(struct Pilha *p) {
p->tamanho = 0;
}
int cheia(struct Pilha *p) {
if (p->tamanho == N)
return TRUE;
return FALSE;
}
int vazia(struct Pilha *p) {
if (p->tamanho == 0)
return TRUE;
return FALSE;
}
void empilhar(struct Pilha *p, pilha_t num) {
if (cheia(p))
printf("Atencao: Pilha cheia!\n");
else {
p->pilha[p->tamanho] = num;
p->tamanho++;
}
}
pilha_t desempilhar(struct Pilha *p) {
if (vazia(p)) {
printf("Atencao: Pilha vazia!\n");
return PILHA_VAZIA;
}
else {
p->tamanho--;
return p->pilha[p->tamanho];
}
}
void imprimir(struct Pilha *p) {
int i;
printf("Pilha");
for(i=0; i < p->tamanho; i++)
printf(" %lf", p->pilha[i]);
printf("\n");
}
int main(int argc, char **argv)
{
struct Pilha p;
char str[30];// nao esta sendo usada,msg amarela
inicializar (&p);
char ch1[30];
printf("Digite a expressao: ");
fgets(ch1, 30, stdin);
int i, linha;
char s[30];
int k;
for(k=0;k<30;k++)
s[k]=' ';
for (i=0; i<30; i++) {
char C = ch1[i];
//printf(" %c \n", C);
if (isdigit(C) || C == '.'){
s[linha++] = C;
} else if (s[0] == '.') {
double total;
total = desempilhar(&p);
printf( "Total = %.2lf\n", total);
break;
}
else if (C == ' ') {
if (atof(s) != 0)
empilhar(&p, atof(s));
linha = 0;
int k;
for(k=0;k<30;k++)
s[k]=' ';
}
else if ((C == '+') || (C == '-') || (C == '*') || (C == '/')) {
double n1, n2, total;
n2 = desempilhar(&p);
n1 = desempilhar(&p);
switch (ch1[i]) {
case '+': total = n1 + n2; break;
case '-': total = n1 - n2; break;
case '*': total = n1 * n2; break;
case '/': total = n1 / n2; break;
default : printf( "erro no operador\n");
exit( EXIT_FAILURE);
}
empilhar(&p, total);
}
else
break;
}
return (0);
}
The line
int i, linha;
should be :EDIT :
As you never use
imprimir
in your code, steps could not appear. I make some slight modifications in your code apart from the fix above :#define DEBUG 1
near the beginning of program to control trace prints#define DEBUG 1
with a#ifndef
block to allow to define it at compilation timeimprimir
andempilhar
to allow the use ofimprimir
in the body ofempilhar
imprimir
inempilhar
Here is the resulting code :
Here is an example of use :