Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
31
Task/Gray-code/Prolog/gray-code-2.pro
Normal file
31
Task/Gray-code/Prolog/gray-code-2.pro
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
:- use_module(library(apply)).
|
||||
|
||||
to_gray(N, G) :-
|
||||
N0 is N >> 1,
|
||||
G is N xor N0.
|
||||
|
||||
from_gray(G, N) :-
|
||||
( G > 0
|
||||
-> S is G >> 1,
|
||||
from_gray(S, N0),
|
||||
N is G xor N0
|
||||
; N is G ).
|
||||
|
||||
make_num(In, Out) :-
|
||||
atom_to_term(In, Out, _),
|
||||
integer(Out).
|
||||
|
||||
write_record(Number, Gray, Decoded) :-
|
||||
format('~w~10|~2r~10+~2r~10+~2r~10+~w~n',
|
||||
[Number, Number, Gray, Decoded, Decoded]).
|
||||
|
||||
go :-
|
||||
setof(N, between(0, 31, N), Numbers),
|
||||
maplist(to_gray, Numbers, Grays),
|
||||
maplist(from_gray, Grays, Decodeds),
|
||||
format('~w~10|~w~10+~w~10+~w~10+~w~n',
|
||||
['Number', 'Binary', 'Gray', 'Decoded', 'Number']),
|
||||
format('~w~10|~w~10+~w~10+~w~10+~w~n',
|
||||
['------', '------', '----', '-------', '------']),
|
||||
maplist(write_record, Numbers, Grays, Decodeds).
|
||||
go :- halt(1).
|
||||
Loading…
Add table
Add a link
Reference in a new issue