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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
| Program Polynôme;
type parbre=^arbre;
arbre=record
coeff:real;{$v 0:30}
expo:integer;{$v 30:60}
g:parbre;
d:parbre;
end;
type affectpoly=array['A'..'Z',1..2]of parbre;
{------------------------------------------------------------------------------}
Function Cree(c:real;e:integer):parbre;
{On crée un arbre binaire représentant un polynôme.}
begin
new(result);
result^.coeff:=c;
result^.expo:=e;
result^.g:=nil;
result^.d:=nil;
end;
Procedure Ajoute(var p:parbre;c:real;e:integer);
{On ajoute un terme du polynôme dans l'arbre.}
begin
if p=nil then p:=cree(c,e)
else if p^.expo>e then ajoute(p^.g,c,e)
else if p^.expo<e then ajoute(p^.d,c,e)
else if p^.expo=e then p^.coeff:=p^.coeff+c
end;
{------------------------------------------------------------------------------}
Function Valeur_coeff(s:string):real;
{On convertit une chaîne de caractères en un réel.}
var
erreur:integer;
begin
val(s,result,erreur);
end;
Function Valeur_exposant(s:string):integer;
{On convertit une chaîne de caractères en un entier.}
var
erreur:integer;
begin
val(s,result,erreur);
end;
{------------------------------------------------------------------------------}
function Verif_Grammaire(s:string):boolean;
{On vérifie la syntaxe du polynôme rentré par l'utilisateur.}
var
i:integer;
begin
result:=false;
if length(s)=1 then if ((s[1] in ['0'..'9'])) or (s[1] in ['x'])
then begin
result:=true;
exit;
end
else begin
result:=false;
exit;
end;
if length(s)=2 then if (s[1] in ['-']) then if (s[2] in ['x'])
then begin
result:=true;
exit;
end
else begin
result:=false;
exit;
end;
for i:=1 to length(s)-1 do
begin
if s[i] in ['+','-'] then if s[i+1] in ['1'..'9'] then result:=true
else begin
result:=false;
exit;
end;
if s[i]='*' then if s[i+1]='x' then result:=true
else begin
result:=false;
exit;
end;
if s[i]='x' then if s[i+1] in ['^','-','+'] then result:=true
else begin
result:=false;
exit;
end;
if s[i]='^' then if s[i+1] in ['1'..'9'] then result:=true
else begin
result:=false;
exit;
end;
if (s[i]in['1'..'9'])then if s[i+1]in['+','-','*','1'..'9']then result:=true
else begin
result:=false;
exit;
end;
if not(s[i] in ['1'..'9']) and_then (s[i]=s[i+1]) then begin
result:=false;
exit;
end
else if (s[i] in ['-','+','x','*','^','1'..'9']) then begin
result:=true;
exit;
end;
end;
end;
Procedure Analyse(var p:parbre;s:string);
{On analyse un terme du polynôme.}
var
c1,e1:string;
c:real;
a,i,e:integer;
begin
if s<>''then
begin
a:=pos('x',s);///dans le terme donné, on se place sur le caractère x
if a=0 then begin///et s'il n'y a pas de x alors comme c'est une constante,
c:=valeur_coeff(s);///on convertit la chaîne en un réel
ajoute(p,c,0);///et on l'ajoute dans l'arbre avec expo=0
end
else begin
i:=length(s);
if s[i] in ['1'..'9'] then
begin
e1:=copy(s,a+2,i-a+1);///on copie l'exposant dans la chaîne e1
e:=valeur_exposant(e1);///et on convertit en un entier celle_ci
end
else e:=1;
if s[1] in ['1'..'9'] then
begin
c1:=copy(s,1,i-a+2);///on copie le coefficient dans la chaîne c1
c:=valeur_coeff(c1);///et on convertit en un réel celle_ci
end
else if s[1]='x'then c:=1;///s'il n'y a rien devant le x ,coeff=1
if s[1]='-' then
begin///quand le coeff est negatif
c1:=copy(s,2,i-a+2);///on le copie sans le signe -
if c1='x' then c:=-1 ///quand le coeff=-1 n'a pas été écrit
else c:=-valeur_coeff(c1);///on convertit la chaîne en un réel
///sans oublier le signe -
end;
ajoute(p,c,e);
end;
end;
end;
Function Separe(s:string):parbre;
{On sépare chaque terme du polynôme.}
var
s1,s2:string;
i:integer;
begin
new(result);///on crée notre arbre
while s<>'' do
begin///si la chaîne est non vide
i:=length(s);
while (i<>0) and_then not(s[i]in['+','-']) do dec(i);///on cherche les signes:
///+ et - dans notre polynôme tel que si on n'en trouve pas,on diminue i de 1
if i=0 then
begin
if (result^.expo=0) and_then (result^.coeff=0) then dispose(result);
analyse(result,s);
s:='';
end
else if s[i]='+' then
begin
s1:=copy(s,i+1,length(s)-i);///on copie une partie de la chaîne sans le + et
if (result^.expo=0) and_then (result^.coeff=0) then dispose(result);
analyse(result,s1);
delete(s,i,length(s)-i+1);///on supprime la chaîne que l'on vient de copier
end
else
begin
s2:=copy(s,i,length(s)-i+1);///on copie une partie de la chaîne avec le - et
if (result^.expo=0) and_then (result^.coeff=0) then dispose(result);
analyse(result,s2);
delete(s,i,length(s)-i+1);///on supprime la chaîne que l'on vient de copier
end;
end;
end;
{------------------------------------------------------------------------------}
{Transformation d'un arbre en un polynôme}
Function Valeur_to_string(x:real):string;
{On convertit la valeur lue dans l'arbre en une chaîne de xaractères.}
begin
str(x:2:1,result);
end;
Function ArbreToPoly(p:parbre):string;
function Sous_ArbreToPoly(p:parbre):string;
var
terme:string;
begin
if p=nil then result:='' ///cas où notre arbre est vide.
else if p^.coeff<>0 then
begin
if p^.coeff=1 then terme:='+'
else if p^.coeff=(-1) then terme:='-'
else if p^.coeff>0 then terme:='+'+valeur_to_string(p^.coeff)
else terme:=valeur_to_string(p^.coeff);
if p^.expo=1 then terme:=terme+'x'
else if p^.expo<>0 then terme:=terme+'x^'+valeur_to_string(p^.expo);
result:=sous_ArbreToPoly(p^.d)+terme+sous_ArbreToPoly(p^.g);
end
else result:=sous_ArbreToPoly(p^.d)+sous_ArbreToPoly(p^.g);
end;
begin
result:=Sous_ArbreToPoly(p);
if result[1]='+' then delete(result,1,1);
end;
{------------------------------------------------------------------------------}
{Evaluation du polynôme en une valeur x donnée}
Function Eval(p:parbre;x:real):real;
begin
if p=nil then result:=0
else result:=eval(p^.g,x)+p^.coeff*exp(p^.expo*ln(x))+eval(p^.d,x);
end;
{------------------------------------------------------------------------------}
{Affectation d'un polynôme à des variables identifiées par un caractère}
Procedure Affectation(s:string;var t:affectpoly);
var
a:char;
s1:string;
begin
s1:=s;
a:=s1[1];
delete(s1,1,pos('=',s1));
if s1='' then exit
else if (s1[1]>='A') and (s1[1]<='Z') then begin
t[a,1]:=t[s1[1],1];
delete(s1,1,2);
delete(s1,length(s1)-1,1);
t[a,2]:=separe(s1);
end
else begin
t[a,1]:=separe(s);
t[a,2]:=separe('x');
end;
end;
{------------------------------------------------------------------------------}
{}
Procedure Produit(p,q:parbre;var prod:parbre);
Procedure Multiplier_Terme_Poly(q:parbre;var prod:parbre;coeff:real;expo:integer);
begin
new(prod);
if q<>nil then begin
if (prod^.expo=0) and_then (prod^.coeff=0) then dispose(prod);
ajoute(prod,q^.coeff*coeff,q^.expo+expo);
multiplier_Terme_Poly(q^.g,prod,coeff,expo);
multiplier_Terme_Poly(q^.d,prod,coeff,expo);
end;
end;
begin
if p<>nil then begin
if (prod^.expo=0) and_then (prod^.coeff=0) then dispose(prod);
multiplier_Terme_Poly(q,prod,p^.coeff,p^.expo);
produit(p^.g,q,prod);
produit(p^.d,q,prod);
end;
end;
{*******************************************************************************
************************Début du programme principal****************************
*******************************************************************************}
var
gram,gram1:boolean;
Valeur,x:real;
t:affectpoly;
p,p1,p2:parbre;
s,s1,s2:string;
begin
writeln('Bienvenue dans le programme d''analyse d''un polynôme');
writeln('Rentrer un polynôme svp :');
readln(s);
gram:=Verif_Grammaire(s);
while gram=false do
begin
writeln('Le polynôme que vous avez rentrer n''a pas la bonne syntaxe, la bonne syntaxe est : 3*x^8-7*x^9....');
writeln('Rentrer un autre polynôme svp :');
readln(s);
gram:=Verif_Grammaire(s);
end;
writeln('Rentrer un second polynôme svp :');
readln(s1);
gram1:=Verif_Grammaire(s1);
while gram1=false do
begin
writeln('Le polynôme que vous avez rentrer n''a pas la bonne syntaxe, la bonne syntaxe est : 3*x^8-7*x^9....');
writeln('Rentrer un polynôme svp :');
readln(s1);
gram1:=Verif_Grammaire(s1);
end;
p:=separe(s);
p1:=separe(s1);
Writeln('Rentrer la valeur pour laquelle vous voullez calculer votre premier polynôme :');
readln(x);
valeur:=Eval(p,x);
Writeln('Notre polynôme calculé en ',x:2:1,' vaut :',valeur:2:1);
writeln('bure :');
readln(t);
Affectation(s,t);
Produit(p,p1,p2);
s2:=ArbreToPoly(p2);
writeln('Notre polynôme est :',s2);
end. |
Partager