|
IORGA CODRIN
Clasa aXI-a A
REFERAT LA INFORMATIC?-
INSTRUC?IUNEA REPEAT
Aceast? instruc?iune reproduce structura repeat until ?i are forma general?:
REPEAT
i1;
i2;
. . .
in
UNTIL expresie logic?
Aici , i1,…,in reprezint? instruc?iuni.
Principiul de executare este urm?torul:
? Se execut? secven?a de instruc?iuni;
? Se evalueaz? expresia logic?;
? Dac? aceasta ia valoarea FALSE se execut? din nou secven?a de instruc?iuni, contrar se trece mai departe.
Iat? cum arat? instruc?iunea REPEAT prezentat? cu ajutorul diagramei de sintax?:
INSTRUC?IUNE
INSTRUC?IUNEA
REPEAT
Observa?ie: secven?a se execut? cel pu?in odat?, dup? care se pune problema dac? s? se repete sau nu (prin evaluarea expresiei logice).
Regula instruc?iunii REPEAT
Are forma:
{P} A {Q}, (Q and not b) <= P
{P} repeat A until b {Q and b}
Formula P se nume?te specifica?ie invariant?.
Pentru simplitate, aplicarea acestor reguli în demonstra?ii se va nota prin:
repeat repeat
{P} {P}
A sau A
{Q} {Q}
until b until b
{Q and b}
Observa?ie: din prezentarea neformalizat? a semanticii, se ?tie c? instruc?iumea:
repeat A until b
este echivalent? cu instruc?iunea compus?:
begin A;
while not b do
A
End
Se poate ar?ta acum, în termenii semanticii axiomantice, c? regula instruc?iunii repeat poate fi dedus? din regula instruc?iunii while, regula consecin?elor ?i regula instruc?iunii compuse. Într- adev?r, în ipostazele {P} A {Q} ?i (Q and not b) <= P poate fi construit? urm?toarea demonstra?ie:
{P}
begin A;
{Q}
while not b do
{Q}
A
end
{Q and b}.
Regula consecin?elor a fost aplicat? pentru a demonstra c? specifica?ia Q este invariant? pentru instruc?iunea while:
{Q and not b}
{P}
A
{Q}.
Exemplu. Nota?ia:
Program R1;
Type natural =0..maxint;
Var x, y, z, u : natural
{(x = a) and (y = b) and (a >= 0) and (b >= 0)}
{(x * y = x * y) and (x > 0)}
begin
z := 0;
{(z + x * y = x * y) and (x > 0)}
u := x;
repeat
{(z + u * y = x * y) and (x > 0)}
z := z + y;
{(z + (u - 1) * y = x * y) and (u >= 1)}
u := u - 1
{(z + u * y = x * y) and (u >= 0)}
until u = 0
{(z + u * y = x * y) and (u >= 0) and (u = 0)}
end.
{{(z = a * b)}
este o demonstra?ie a corectitudinii par?iale a unui algoritm de înmul?ire a dou? numere naturale prin adunare repetat?.
Pentru instruc?iunea repeat…until u=0, specifica?ia invariant? este (z+u*y=x*y) and (u>0).
Exemplu. Nota?ia:
{Program R2}
type natural = 0..maxinit;
var x, y : natural;
{(a > 0) and (b > 0) and (x = a) and (y = b)}
begin
repeat
{(x > 0) and (b > 0) and (x > a) and (y > b)}
while x > y do
{(x > 0) and (y > 0) and ((a, b) = (x, y))}
x := x - y;
while y > x do
{(x > 0) and (y > 0) and ((x, y) and (a, b))}
y := y - x;
{(0 < y <= x) and ((a, b) = (x, y))}
until x = y
{(0 < y <= x) and ((a, b) = (x, y)) and (x = y)}
end.
{x = (a, b)}
este o demonstra?ie a corectitudinii par?iale a unui algoritm de calcul al celui mai mare divizor comun (a, b) a dou? numere naturale. În demonstra?ie s-au mai folosit urm?toarele specifica?ii adev?rate:
(x > y) <= ((x, y) = (x - y, y));
(x, y) = (y, x);
(x, x) = x.
Exemplu. Urm?toarea nota?ie:
{program R3}
type natural = 0..maxint;
var x, s, t : natural;
{(x = a) and (y = b) and (a >= 0) and (b >= 0)}
begin s := 0;
repeat
{s = (a - x) * b}
t := 0;
repeat
{(s = (a - x) * b + t) and (y = b)}
s := s + 1;
t := t + 1;
until t = y;
{(s = (a – x) * b + t) and (y = b) and (t = y)}
{s = (a - x) * b + b)
x := x - 1
{s = (a - x) * b}
until x = 0
{( s =(a - x) * b) and (x = 0)}
end.
{x = a * b}
este o demonstra?ie a corectitudinii unui algoritm de înmul?ire a dou? numere naturale folosind opera?iile succ ?i pred.
Exemplu. Se cite?te un num?r natural n, mai mare sau egal cu 1. s? se calculeze suma primelor n numere naturale.
program sum?;
var n, s, i: integer;
begin
write (‚n=’); readln (n);
i := 1; s := 0;
repeat
s := s + i;
i := i + 1
until i > n;
writeln (‚s=’, s)
end.
Exemplu: se cite?te n, num?r natural. S? se descompun? în factori primi.
program factp;
var n, d, fm :integer;
begin
write (‚n=’); readln (n);
d := 2;
repeat
fm := 0; {fm reprezint? multiplicitatea divizorului d}
while n mod d = 0 do
begin
fm := fm + 1;
n := n div d
end;
if fm <> 0 then writeln (d, ‚la puterea’, fm);
d := d + 1
until n = 1
end.
BIBLIOGRAFIE:
- Manual pentru clasa a IX-a de informatic?. Editura L&S INFOMAT
- Limbajul Pascal. Concepte fundamentale. Volumul I ;editura TEHNIC?
|