RPN with header, Why doesn't it work?

92 views Asked by At

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

There are 1 answers

3
Serge Ballesta On BEST ANSWER

The line int i, linha; should be :

int i, linha = 0;

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 :

  • add a #define DEBUG 1 near the beginning of program to control trace prints
  • protect the #define DEBUG 1 with a #ifndef block to allow to define it at compilation time
  • reverse the order of imprimir and empilhar to allow the use of imprimir in the body of empilhar
  • add a call to imprimir in empilhar

Here is the resulting code :

#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

#ifndef DEBUG
#define DEBUG 1
#endif


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


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

void empilhar(struct Pilha *p, pilha_t num) {
if (cheia(p))
printf("Atencao: Pilha cheia!\n");
else {
p->pilha[p->tamanho] = num;
p->tamanho++;
if (DEBUG) {
    imprimir(p);
}
}
}

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 = 0;
    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);
}

Here is an example of use :

Digite a expressao: 3.2 1.8 - 10 / 2 + .
Pilha 3.200000
Pilha 3.200000 1.800000
Pilha 1.400000
Pilha 1.400000 10.000000
Pilha 0.140000
Pilha 0.140000 2.000000
Pilha 2.140000
Total = 2.14