Bonjour,

Je suis entrain d'essayer de faire une calculatrice en C et j'aimerais utiliser la notation polonaise inversée pour gérer les parenthèses des différents calculs à effectuer. Pour le moment, j'essaye d'afficher sur la sortie la conversion.

Exemple : (4 + 3) * 5 -> 4 3 + 5 *

Voici mon code source :

conversion.c :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
 
#include "conversion.h"
 
int
main(void) {
 
    struct pile operators;
    char string[SIZE];
 
    pile_initialisation(&operators);
    fgets(string, SIZE, stdin);
    conversion(string, &operators);
 
    return 0;
}
 
void
pile_initialisation(struct pile* operators) {
 
    int i = 0;
 
    for (i = 0; i < 100; i = i + 1) {
 
	(*operators).table[i] = '\0';
    }
    (*operators).top = 0;
}
 
/* Si c'est un chiffre, on l'affiche sur la sortie ;
 * Si c'est un operateur ou une parenthèse, on l'ajoute à la pile ;
 * Si c'est une parenthèse droite, on dépile jusqu'à la précédente parenthèse
 * gauche.
 * Sinon on avance de 1 (espaces tout ça...)
 */
 
void
conversion(const char* string, struct pile* operators) {
 
    int i = 0;
 
    while (string[i] != '\0') {
 
	if (isdigit(string[i])) {
 
	    printf("%d ", atoi(&string[i]));
	    i = next_no_digit(&string[i], i);
	}
	else if (isOperator(string[i])) {
 
	    add_operator_to_pile(string[i], operators);
	    i = i + 1;
	}
	else if (string[i] == ')') {
 
	    push_to_previous_parenthesis(operators);
	    i = i + 1;
	}
	else {
 
	    i = i + 1;
	}
    }
    push_pile_to_end(operators);
}
 
/* avance jusqu'au prochain signe different d'un chiffre... */
 
int
next_no_digit(const char* string, int i) {
 
    int j = 0;
 
    for (j = i + 1; !isdigit(string[j]); j = j + 1);
 
    return j;
}
 
int
isOperator(const char character) {
 
    int is = 0;
 
    switch (character) {
 
    case '+' :
	is = 1;
	break;
    case '-' :
	is = 1;
	break;
    case '*' :
	is = 1;
	break;
    case '/' :
	is = 1;
	break;
    case '(' :
	is = 1;
	break;
    default :
	is = 0;
    }
 
    return is;
}
 
void
add_operator_to_pile(const char operator, struct pile* operators) {
 
    (*operators).table[(*operators).top] = operator;
    (*operators).top = (*operators).top + 1;
}
 
/* depile jusqu'à la précédente parenthèse */
 
void
push_to_previous_parenthesis(struct pile* operators) {
 
    while ((*operators).table[(*operators).top - 1] != '(') {
 
	printf("%c ", (*operators).table[(*operators).top - 1]);
	(*operators).top = (*operators).top - 1;
    }
    (*operators).top = (*operators).top - 1;
}
 
/* on vide la pile operateurs, souvent en fin de calcul */
 
void
push_pile_to_end(struct pile* operators) {
 
    while ((*operators).top >= 1) {
 
	printf("%c ", (*operators).table[(*operators).top - 1]);
	(*operators).top = (*operators).top - 1;
    }
    (*operators).top = (*operators).top - 1;
}
conversion.h :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
#ifndef CONVERSION_H
#define CONVERSION_H
 
#define SIZE 100
 
struct pile {
 
    char table[SIZE];
    int top;
};
 
void
conversion(const char* string, struct pile* operators);
 
int
next_no_digit(const char* string, int i);
 
int
isOperator(const char character);
 
void
add_operator_to_pile(const char operator, struct pile* operators);
 
void
push_to_previous_parenthesis(struct pile* operators);
 
void
pile_initialisation(struct pile* operators);
 
void
push_pile_to_end(struct pile* operators);
 
#endif
Pour le moment, le code ne fonctionne qu'à « moitié » et je ne comprends pas pourquoi. J'ai l'impression qu'il s'arrête à la parenthèse droite...

Donc si vous pouviez m'éclairer ;-).

Merci à vous.

Bye.