Professional Documents
Culture Documents
التراجعية غير المباشرة :يكون اإلجراء آو الدالة تراجعية عندما يكون اإلجراء P1ينادي إجراء P 2وبدوره اإلجراء P2ينادي
اإلجراء P1
)Module récursif1(p. formels )Module récursif2(paramètres formels
Début Début
………………… ………………..
)).………………(Récursif2(………….. récursif1
………………… …………………
Fin récursif1 Fin récursif2
مبدأ وتقديم حل تراجعي:
نفذ ما يلي يدويا:
مالحظة :في تعريف التكرارية ،يجب التوقف بعد العديد من النداءات التراجعية$.
الحل التراجعي $يترجم بواسطة البنية الشرطية التالية$:
1
;function inverse(ch: string): string
;function factoriel(n: integer): longint begin
begin if ch = ‘’ then
if n = 0 then ’‘ =: inverse
factoriel := 1 else
else inverse := ch [length(ch)] + inverse (copy (ch,1,
;factoriel := n * factoriel(n - 1) length(ch)-1))
;end ;end
موجبين غير$ (القاسم املشترك األكبر) لعددين طبيعيينPGCD اكتب مع التحليل دالة تراجعية تسمح بحساب:01تطبيق
طريقة الطرح؟$معدومين باستعمال
:الحل
PGCD (22 , 6 ) = PGCD ( 22-6, 6 ) = PGCD ( 16,6) car 22 > 6
PGCD (16-6 ,6 ) = PGCD (10 , 6) car 16 > 6 =
PGCD (10-6, 6) = PGCD (4, 6) car 10 > 6 =
PGCD (4, 6-4) = PGCD (4, 2) car 4 < 6 =
PGCD (4-2, 2) = PGCD (2, 2) car 4 > 2 =2=
: نالحظ أن:الشرح
- Si a > b alors PGCD (a, b) = PGCD (a-b, a)
- Si b>a alors PGCD (a, b) = PGCD (a, b-a)
- Si a=b alors PGCD (a, b)=a (*** )*** شرط التوقف
تكرارية$ وبالتالي إذا املعالجةb وa للعددينPGCD لحسابPGCD نفس الدالة$نالحظ انه استعملنا
La fonction PGCD devient:
0) Fonction PGCD (a, b : entier) : entier
1) Si a=b alors PGCD a
Sinon si a>b alors PGCD PGCD (a-b, b)
Sinon PGCD PGCD (a, b-a)
Finsi
2) Fin PGCD
:02تطبيق
(تقرأ من الجهتين)؟palindrome اكتب برنامج يقوم بالتحقق من أن السلسة الحرفية املدخلة هي
الحل:
program Palindrome;
VAR s:string;
FUNCTION palyn (s: string):boolean;
BEGIN
IF (length(s) = 1) THEN palyn := TRUE
ELSE
2
IF s[1]=s[length(s)] THEN
palyn := palyn (COPY(s,2,length(s)-2))
ELSE
palyn := FALSE;
END;
begin
WRITE ('Entrez une chaine de caractere(s): ');
READLN (s);
IF palyn(s) THEN
WRITE ('Cette chaine est un palindrome.')
ELSE
WRITE ('Cette chaine n''est pas un palindrome.');
READLN ();
end.
Série d’exercices
Exercice N°1 :
Ecrire une fonction récursive PPCM qui permet de retourner le PPCM de deux entiers donnés.
Exercice N°2 :
Ecrire une fonction récursive Som_Tab permettant de calculer la somme des éléments d’un tableau.
Exercice N°3 :
Ecrire une fonction récursive Som_Mat permettant de calculer la somme des éléments d’une matrice.
Exercice N°4 :
Ecrire une fonction récursive Inverse_Tab qui permet d’inverser les éléments d’un tableau.
Exercice N°5 :
Ecrire une fonction récursive Inverse_Chaine qui permet d’inverser une chaîne de caractères.
Exercice N°6 :
Ecrire une fonction récursive Palindrome qui vérifie si une chaine de caractères est un palindrome ou non.
3
somme:=0
else
somme:=t[n]+ Som_Tab(t,d,n-1);
end;
Exercice N°3 :
Function Som_Mat(k,l: integer):integer;
begin
if k > n then
somme:=0
else
begin
if l<m+1 then
Som_Mat :=t[k,l]+somme (k,l+1)
else
Som_Mat:= Som_Mat (k+1,1);
end
end;
Exercice N°4 :
procedure inverse_tab(var t:tab;d,f:integer);
var x:integer;
begin
if d<f then
begin
x:=t[d];
t[d]:=t[f];
t[f]:=x;
inverse_tab(t,d+1,f-1);
end;
Exercice N°5 :
function inverse_chaine( ch: string):string;
begin
n:=length(ch);
if ch='' then
inverse:=''
else
inverse:= ch[n]+inverse(copy(ch,1,n-1));
end;
Exercice N°6 :
function Palindrome( ch: string):boolean;
begin
if length(ch)<2 then palindrome:= true
else if (ch[1]= ch[length(ch)]) then
palindrome:=palindrome(copy(ch,2, length(ch) -2))
else palindrome:= false;
end;