bonjour atous j`ai un program qui doit lire un fichier et convertir les expression infix vers les postfix. j`ai ecris le programme je crois que sa marche mais je n`arrive pas a enlever les parentheses, voulez vous m`aidez. le program est suppose faire A+B donne AB+ ou encore A+(B-C) donne ABC-+
le contenu du fichier a lire est :
7-(( X *(( X + Y)/(J -3))+Y)/ (4 -2*5))
((A+B))
a+b
(A+B )- C
((a+ b) - ((c-d)))
(a+b) - (c+d) - (f+g)
(((a)))
p/r+c-d*5
5+4-7*2+8/4
5+a+7*b-8*c
a+B
A+B - C
(A+B) * (c - d)
A * B*C -D +E/F/(G+H)
((A +b )*c -(d - e)) - (f +G)
A - B / (C * D * E)
A +((( b - C)* (d - e)+D) / e )* (h - J)
A + b * e/f
x * y
(((a+b)*c)-d)
x-y
voici le programme:
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

/* Infix to Postfix conversion
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXCOLS 80
#define TRUE 1
#define FALSE 0
void postfix(char *,char *);
int isoperand(char);
void popandtest(struct stack *,char *,int *);
int prcd(char,char);
void push(struct stack *,char);
char pop(struct stack *);
int empty(struct stack *);
int full(struct stack *);
int isdigit(int);
int isalpha(char);
void main(void);
int pos=0;
char symb,c;
struct stack
{
     int top;
char items[MAXCOLS];
};
int empty(struct stack *opstk)
{
return (opstk->top == -1);/*empty stack */
}
int full(struct stack *opstk)
{
return (opstk->top == MAXCOLS-1);
}
int isdigit(int ch)
{
if(ch >= '0' && ch <= '9')
return (1);
else
return(0);
}
int isalpha(char ch)
{
if((ch >= 'a' && ch <= 'z')||(ch >= 'A' && ch <= 'Z'))
return (1);
else
return(0);
}
 
void push(struct stack *opstk, char x)
{
if (!full(opstk))
{
opstk->items[++(opstk->top)] = x;
}
else
printf("Stack is already full!\n");
return;
}
char pop(struct stack *opstk)
{
if (empty(opstk))
{
printf("Stack is empty, nothing to pop!\n");

}

return(opstk->items[opstk->top--]);
}
void popandtest(struct stack *opstk, char *x, int *und)
{
if (empty(opstk))
*und = 1;
else
{
*und = 0;
*x = opstk->items[opstk->top--];
}
}
int prcd(char op1,char op2)
{
int i=0;
i++;
if(((op1 == '*')&&(op2 == '+')&&(op2 == '-')&&(op2==')')||(op2==')')))
return 1;
elseif (((op1 == '/'))&&((op2 == '+')&&(op2 == '-')||(op2==')')))
return 1;
elseif (((op1 == '+')||(op1=='-'))&&((op2 == '+')||(op2
=='-')||(op2==')')))
return 1;
else
if(((op1 == '*')||(op1=='/'))&&((op2 == '*')||(op2
=='/')||(op2==')')))
return 1;
else
return 0;
}
int isoperand(char op)
{
if(isdigit(op)||isalpha(op))
return 1;
else
return 0;
}
 
void postfix(char infix[],char postr[])
{
int position, und;
int outpos = 0;
char topsymb = '+';
char symb;
struct stack opstk;
opstk.top = -1; 

for(position=0; (symb = infix[position]) != '\0'; position++)
if(isoperand(symb))
postr[outpos++] = symb;
else
{
popandtest(&opstk, &topsymb, &und);
while(!und && prcd(topsymb, symb)){
postr[outpos++] = topsymb;
popandtest(&opstk, &topsymb, &und);
} 
if(!und)
push(&opstk, topsymb);
if(und || (symb != ')'))
push(&opstk, symb);
else
topsymb = pop(&opstk);
}
while(!empty(&opstk))
postr[outpos++] = pop(&opstk);
postr[outpos] = '\0';
return;
}
void main(void)
{
char infix[MAXCOLS];
char postr[MAXCOLS];
int pos = 0;
char ans='Y';//ch;

FILE *infile;
char ch;
int j=0,i=1;
if(!(infile=fopen("H:\\cis211\\TLU\\proj2.txt","r")))
printf("ERROR OPENING FILE\n");
else

{
//opstk.top = -1; /*empty stack */
while(fscanf(infile, "%c", &ch) != EOF) //((ch=fgetc(infile))!= EOF)
{
//while((ans=='Y')||(ans=='y'))
//{
if(ch != '\n')
{
if(ch != ' ') infix[pos++] = ch;
}
else
{
printf("pos:%d\n", pos);
infix[pos]='\0';
printf(" %s%s\n","The original infix expression is:", infix);
postfix(infix, postr);
printf("%s%s\n","The postfix expression is:", postr);
 
//printf("Continue (Y/N): ");
//scanf(" %c", &ans);
pos=0;
}
 
//}
}
fclose(infile);
} 
getchar();
 
}/* end main */