5
41. Hãy đặc tả hàm trả sắp xếp mảng số thực A theo thứ tự giảm dần. Không tường minh: sort_array(A: R*) B:R* pre post: (len A 1 ^ B = A) V (len A > 1 ^ B = insert_pos( hd A, sort_array(lt A))) insert_pos (x : R, A : R*) B: R* pre post: ( len A = 0 ^ B = cons (x, A)) V (len A > 0 ^ ((hd A x ^ B = cons( x, A)) V (hd A > x ^ B = cons(hd A, insert_pos(x, lt A)))) ------------------------------------------------------------ ---------------------------- 42. Hãy đặc tả hàm sắp xếp mảng số thực A theo thứ tự giá trị tuyệt đối tăng dần. Không tường minh: sort_abs( A : R*) B: R* pre: post: (len A 1 ^ B = A) V (len A > 1 ^ B = insert_pos (hd A, sort_abs (lt A))) insert_pos( x : R, A : R*) B:R* pre: post: (len A = 0 ^ B = cons( x, A)) v ( len A > 0 ^ ((abs (hd A) abs (x) ^ B = cons ( x, A) ) v (abs (hd A) < abs (x) ^ B = cons ( hd A, insert_pos( x, lt A))))

Bài giải môn đặc tả hình thức từ câu 41-47

Embed Size (px)

DESCRIPTION

Bài giải môn đặc tả hình thức từ câu 41-47

Citation preview

41. Hy c t hm tr sp xp mng s thc A theo th t gim dn. Khng tng minh:sort_array(A: R*) B:R*pre

post: (len A 1 ^ B = A) V (len A > 1 ^ B = insert_pos( hd A, sort_array(lt A))) insert_pos (x : R, A : R*) B: R*pre

post: ( len A = 0 ^ B = cons (x, A)) V (len A > 0 ^ ((hd A x ^ B = cons( x, A)) V (hd A > x ^ B = cons(hd A, insert_pos(x, lt A))))----------------------------------------------------------------------------------------42. Hy c t hm sp xp mng s thc A theo th t gi tr tuyt i tng dn. Khng tng minh:sort_abs( A : R*) B: R*pre:

post: (len A 1 ^ B = A) V (len A > 1 ^ B = insert_pos (hd A, sort_abs (lt A))) insert_pos( x : R, A : R*) B:R*pre:

post: (len A = 0 ^ B = cons( x, A)) v ( len A > 0 ^ ((abs (hd A) abs (x) ^ B = cons ( x, A) ) v (abs (hd A) < abs (x) ^ B = cons ( hd A, insert_pos( x, lt A))))----------------------------------------------------------------------------------------43. Hy c t hm sp xp mng s thc A theo quy lut sau: Cc s dng (nu c) u mng v c th t gim dn. Cc s m (nu c) cui mng v c th t tng dn. Khng tng minh:sort ( A : R*) B: R*pre:

post: (len A 1 ^ B = A) v ( len A > 1 ^ ((hd A 0 ^ insert_pos( hd A, sort (lt A))) v ( hd A < 0 ^ insert_nag ( hd A, sort (lt A)))) insert_pos ( x : R, A :R*) B:R*

pre: x 0

post: (len A = 0 ^ B = cons(x, A)) v (len A > 0 ^ ( ((hd A x ^ hd A 0) v hd A < 0) ^ B = cons ( x, A)) v ( hd A > x ^ B = cons (hd A, insert_pos(x, lt A)))) insert_nag (x :R, A : R*) B: R*pre: x < 0

post: (len A = 0 ^ B = cons( x, A) ) v (len A > 0 ^ ( (hd A x ^ hd A < 0 ^ B = cons (x, A)) v ( (hd A < x v hd A 0) ^ B = cons ( hd A, insert_nag (x, lt A))))----------------------------------------------------------------------------------------44. Hy c t hm sp xp mng s nguyn A theo quy lut: cc s chn (nu c) u mng v c th t tng dn, cc s l (nu c) cui mng v c th t gim dn Khng tng minh:sort ( A : R*) B : R*pre:

post: (len A 1 ^ B = A) v ( len A > 1 ^ ((is_even(hd A) ^ insert_even( hd A, sort (lt A))) v ( is_odd(hd A) ^ insert_odd ( hd A, sort (lt A)))) insert_even ( x : R, A :R*) B:R*pre: is_even(x)

post: (len A = 0 ^ B = cons(x, A)) v (len A > 0 ^ ( ((is_even(hd A) ^ hd A x) v is_odd(hd A)) ^ B = cons ( x, A)) v ( (is_even(hd A) ^ hd A < x) ^ B = cons (hd A, insert_even(x, lt A)))) insert_odd (x :R, A : R*) B: R*pre: is_odd(x)

post: (len A = 0 ^ B = cons( x, A) ) v (len A > 0 ^ ( (is_odd(hd A) ^ hd A x ^ B = cons (x, A)) v ( ( (is_odd(hd A) ^ hd A > x) v is_even (hd A))^ B = cons ( hd A, insert_odd (x, lt A))))------------------------------------------------------------------------------------------46. Hy c t hm kim tra mt chui s c phi l chui con ca chui t hay khng? Khng tng minh:is_child (s : String, t : String) r:Bpre:

post: r = () Tng minh:is_child : String String Bis_child (s,t) = if(len t < len s) then FALSEelse if(s = subseq (t, 1, len s) then TRUEelse is_child(s, lt( t ))----------------------------------------------------------------------------47. Hy c t hm to ra chui k t o ngc ca chui k t s. Khng tng minh:reverse_String(s : String) r:Stringpre:

post: (len s 1 ^ r = s) v (i inds r r(i) = s (len s i +1))hoc cch khc:

(len s < 1 ^ r = s) v (reverse_String (lt A) hd A) Tng minh:

reverse_String: String String

reverse_String(s) = if (len s 1) then r

else reverse_String (lt A) hd A

48. Mt ting (word) l mt chui k t khng c k t khong trng. c t hm chun ha mt chui k t s: xa b cc k t khong trng u v cui chui, gia cc ting (word) c duy nht mt k t khong trng. Khng tng minhstandard_String ( s : String) r : Stringpre:

post: (len s = 0 ^ r = s) v ( len s > 0 ^ ( (hd s = ^ r = standard_String(lt s)) v (s (len s) = ^ r = standard_String( s(1, len s 1))) v ( i inds s s(i) = ^ s(i+1) = ^ r = standard_String(s(1, i)s(i+2, len s))) v (r = s)) Tng minh:

standard_String: String Stringstandard_String(s) = if(len s = 0) then selse if(hd s = ) then standard_String( lt s)else if(s (len s) = ) then standard_String( s (1, len s 1))else