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
| Program Polynômes;
type parbre=^arbre;
arbre=record
coeff:real;{$v 0:40}
expo:integer;{$v 40:60}
g,d:parbre;
end;
type Affecter=array['A'..'Z',1..2] of parbre;
procedure Liberer(var p:parbre);
begin
if p<>nil then begin
Liberer(p^.g);
Liberer(p^.d);
dispose(p);
end;
end;
function Convertir_reel(s:string):real;
{Nous convertissons une chaîne en un réél.}
var
erreur:integer;
begin
val(s,result,erreur);
end;
function Convertir_entier(s:string):integer;
{Nous convertissons une chaîne en un entier.}
var
erreur:integer;
begin
val(s,result,erreur);
end;
function Transformer_Chaine(x:real):string;
{Nous transformons un réel en une chaîne de caractères.}
begin
str(x:1:2,result);
end;
function cree(c:real;e:integer):parbre;
{Nous créons un arbre binaire.}
begin
new(result);
result^.coeff:=c;
result^.expo:=e;
result^.g:=nil;
result^.d:=nil;
end;
procedure Ajouter_Terme(c:real;e:integer;var p:parbre);
{Nous ajoutons un terme du polynôme dans l'arbre créé précedemment.}
begin
if p=nil then p:=cree(c,e)
else if p^.expo<e then Ajouter_Terme(c,e,p^.d)
else if p^.expo>e then Ajouter_Terme(c,e,p^.g)
else if p^.expo=e then p^.coeff:=p^.coeff+c;
end;
procedure Analyser(s:string;var p:parbre);
{Nous analysons un terme du polynôme.}
var
c:real;
position_x,e:integer;
cs,es:string;
begin
if s<>''then begin
position_x:=pos('x',s);
if position_x=0 then begin
c:=Convertir_reel(s);
Ajouter_Terme(c,0,p);
end
else begin
cs:=copy(s,1,position_x-1);
if (cs<>'')and_then (cs[length(cs)]='*') then delete(s,length(s),1);
if cs=''then c:=1
else if cs='-'then c:=-1
else c:=Convertir_reel(cs);
es:=copy(s,position_x+1,length(s)-position_x);
if es='' then e:=1
else if es[1]='^' then e:=Convertir_entier(copy(es,2,length(s)-1));
end;
end;
Ajouter_Terme(c,e,p);
end;
Procedure Separer(s:string);
{Nous séparons chaque terme du polynôme.}
var
Longueur:integer;
p:parbre;
begin
p:=nil;
while s<>''do begin
Longueur:=length(s);
while (Longueur<>0) and_then not(s[Longueur]in ['+','-']) do dec(Longueur);
if Longueur=0 then begin
Analyser(s,p);
s:='';
end
else if s[Longueur]='+' then begin
Analyser(copy(s,Longueur,length(s)-Longueur),p);
delete(s,Longueur,length(s)-Longueur+1);
end
else begin
Analyser(copy(s,Longueur,length(s)-Longueur+1),p);
delete(s,Longueur,length(s)-Longueur+1);
end
end;
end;
function Transformation_chaine(p:parbre):string;
{Nous faisons la conversion d'un polynôme en une chaîne de caractères.}
var
Terme:string;
begin
if p=nil then result:=''
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:='+'+ Transformer_chaine(p^.coeff)
else Terme:=Transformer_chaine(p^.coeff);
if p^.expo=1 then Terme:=Terme+'x'
else if p^.expo<>0 then Terme:=Terme+'x'+Transformer_chaine(p^.expo);
result:=Transformation_chaine(p^.d)+Terme+Transformation_chaine(p^.g)
end
else result:=Transformation_chaine(p^.d)+Transformation_chaine(p^.g);
end;
procedure Affectation(s:string;var t:Affecter);
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);
end
else begin
end
end;
function Evaluation(p:parbre;x:real):real;
begin
if p=nil then result:=0
else result:=Evaluation(p^.g,x)+p^.coeff*exp(p^.expo*ln(x))+ Evaluation(p^.d,x);
end;
{Début du programme principal.}
var
s:string;
p:parbre;
x,c:real;
e:integer;
begin
writeln('Veuillez écrire un polynôme à une indéterminée et à coefficients réels:');
readln(s);
p:=nil;
Ajouter_Terme(c,e,p);
Analyser(s,p);
Separer(s);
end. |
Partager