"'O
0
c
:J
0
:<:;
li)
.,,
..--t
c::
:l
0
~
N
"
@
~
.......
0
J::
:;
O'l
"'
·;::::
c::
0
>c::
o.
c::
0
.9
u
ü
:l
.,,
2
o.
~
"
:;
0
11
.,,
0
c:
:l
0
QJ
10.4 • Calcul formel
......................... Fonctions ............................ .
Fonction somme(ch: chaîne) : liste
Rôle : calcule la somme sous la forme d'une liste [numérateur,dénominateur]
Variables locales : k, a, b, c, d (entiers), L (liste)
Début
L +-- []
Pour k de 0 à longueur(ch)- 1 Faire
Si ch[k]=='/' ou ch[k]=='+' Alors
1
Ajouter k à L
Fin Si
Fin Pour
a +-- int(ch[O:L[O]])
b +-- int(ch[L[O]+ 1 :L[l]])
c +-- int(ch[L[l]+ 1 :L[2]])
d +-- int(ch[L[2]+ 1 :longueur(ch)])
Retourner(a'"d+b-i'c,b-i'd)
Fin Fonction
Fonction p(a,b: entiers): entier
Rôle : calcule le pgcd de a et b par l'algorithme d'Euclide
Début
Si b==O Alors
J
Retourner (a)
Sinon
1
Retourner (p(b,a%b))
FinSi
Fin Fonction
Fonction fractionsimp(a,b: entiers) : chaîne
Rôle : simplifie la fraction a/b
Variables locales : u,v (entiers)
Début
u +-- a//p(a,b)
V +-- b//p(a,b)
Retourner(str(u)+'/'+st r(v))
Fin Fonction
. .. ....... . . . ... .. ... . . Partie principale ... . . . . . .. . ... . .......... . .
Début
Afficher "Ecrire une somme de fractions de la forme a/b+c/d : "
Saisir expression
som +-- somme(expression)
simp +-- fractionsimp(som[O],som[l])
Afficher (expression+'='+simp)
Fin
b) Implémentation Python
######################### Fonction s ##############################
d e f s o mme ( c h ) :
L=[ ]
f o r k i n ra nge( 1 e n ( c h )) :
if c h [ k]== '/ ' o r c h[k ]=='+':
L. a pp e nd( k)
a= i nt (c h[ O: L[O]])
233
Précédent

- 241/302

Suivant