Having trouble with stacks - crashing

Now, my problem is that once I put "3 4 +", then do "3 +", it adds the 3 to the last result. So I'm guessing I have to free something, but it seems like when I do it, I get 0.00 as result. Also, is there a way to prevent my program from crashing and instead give an error code?

Try this:

calculatrice.c :

include <stdio.h>

#include <stdlib.h>
#include "calculatrice.h"

struct noeud
{
    double chiffre;
    struct noeud *prochain;
};

struct noeud *tete = NULL;

struct noeud *creerNoeud(double v)
{
    struct noeud *n = (struct noeud *) malloc(sizeof(struct noeud));

    n->chiffre = v;
    n->prochain = NULL;

    return n;
}

void push(double v)
{
    struct noeud *tmp = creerNoeud(v);
    tmp->prochain = tete;
    tete = tmp;
}

struct noeud *pop(){

    struct noeud *tmp = tete;
    if(tete != NULL)
    {
        tete = tete->prochain;
        tmp->prochain = NULL;
    }

    return tmp;
}

void detruireNoeud(struct noeud *n)
{
    free(n);
}

void faireOperation(char *s, int i, int j)
{
    double resultat, nbDroite, nbGauche;
    struct noeud *c1 = pop();
    struct noeud *c2 = pop();

    nbDroite = c1->chiffre;
    nbGauche = c2->chiffre;

    switch(s[i]){
    case '+': 
        resultat = nbDroite + nbGauche; 
        break;
    case '-': 
        resultat = nbDroite - nbGauche; 
        break;
    case '*': 
        resultat = nbDroite * nbGauche; 
        break;
    case '/': 
        resultat = nbDroite / nbGauche; 
        break;
    }

    push(resultat);
}

bool estOperateur(char *s, int i , int j)
{
    return ( j-i == 1 && (s[i] == '+' || s[i] == '-' || s[i] == '*'|| s[i] == '/' ));
}

bool estNombre(char *s,int i, int j)
{
    char nombreChar[MAX];
    double nombreDouble;
    int index = 0;

    for(int k = i; k < j; k++, index++)
        nombreChar[index] = s[k];

    nombreDouble = atof(nombreChar);

    if (nombreDouble == 0 && s[i] != '0')
        return false;
    else
        return true;
}

int evaluerExpression(char *expression, int taille, double *resultat){

    int i = 0;
    int j;
    double nombreFinal;

    while(i < taille) {
        while(expression[i] == ' ' && i < taille)
            i++;

        j = i;

        while(expression[j] != ' ' && j < taille)
            j++;

        if (estOperateur(expression, i , j))
            faireOperation(expression, i, j);
        else if (estNombre(expression, i, j))
        {
            char nombreChar[MAX];
            double nombreDouble;
            int index = 0;

            for(int k = i; k < j; k++, index++)
                nombreChar[index] = expression[k];

            nombreDouble = atof(nombreChar);
            push(nombreDouble);
        }
        else 
        {
            return 1;
        }
        i = j;
        *resultat = tete->chiffre;
    }
}

main.c:

#include <stdlib.h>
#include <stdio.h>
#include "calculatrice.h"

int getligne(char *s, int limite)
{
    int c, i;

    i = 0;
    c = getchar();
    while (i < limite - 1 && c != '\n' && c != EOF)
    {
        s[i++] = c;
        c = getchar();
    }
    s[i] = '\0';

    while (c != '\n' && c != EOF)
        c = getchar();

    return i;
}

int main(int argc, char **argv)
{
    char ligne[MAX];
    int t;
    double *resultat;

    printf("Entrez une expression mathematique (Q pour quitter)\n");
    t = getligne(ligne, MAX);
    while ( !(t == 1 && ligne[0] == 'Q') )
    {
        evaluerExpression(ligne, t, resultat);
        printf("resultat = %.2f\n", *resultat);

        printf("Entrez du texte (Q pour quitter)\n");
        t = getligne(ligne, MAX);
    }
}

calculatrice.h:

#define MAX 100

int evaluerExpression(char *expression, int taille, double *resultat);
/r/learnprogramming Thread Parent