Программа выбирает самое длинное слово из предложения и строит из его букв другие слова.
%trace
database
words(string)
domains
intlist = integer*
predicates
repeat
assert_database
save_and_clear_database
entrance_point
find_longest_word(string, string, string)
isnt_white_space(char)
get_longest(string, string, string)
get_needed_word_len(integer, integer)
check_word_len(integer, integer)
get_possible_words(string, integer, intlist)
check_founded_word(string)
letter_by_number(string, integer, char)
get_start_list(integer, intlist, intlist)
get_word(intlist, string, string)
check_for_end(intlist, integer)
get_list(intlist, integer, intlist, integer)
find_new_h(integer, integer, integer, integer, integer)
clauses
repeat.
repeat:- repeat.
assert_database:-
consult("words.dba").
save_and_clear_database :-
save("words.dba"),
retract(words(_)),
fail.
save_and_clear_database:- !.
get_needed_word_len(WordLen, NeededWordLen) :-
repeat,
write("Input needed length of the word: "),
readint(N),
check_word_len(WordLen, N),
NeededWordLen = N.
check_word_len(Max, N) :-
N <= Max.
check_word_len(Max,_) :-
write("The value must be not more than ", Max),
nl,
fail.
entrance_point:-
makewindow(1,$31,94,"Enter string", 10, 10, 8,44),
readln(Str),
find_longest_word(Str, Word, _),
str_len(Word, WordLen),
get_needed_word_len(WordLen, NeedWordLen),
write("Possible words are:"), nl,
get_start_list(NeedWordLen, [], StartList),
str_len(Word, MaxN),
get_possible_words(Word, MaxN, StartList),
save_and_clear_database.
entrance_point:-
write("Something is wrong").
get_start_list(0, List, List).
get_start_list(WordLen, List, ResList) :-
WordLen1 = WordLen - 1,
List1 = [WordLen|List],
get_start_list(WordLen1, List1, ResList).
check_for_end([], _).
check_for_end([H|T], MaxN) :-
H = MaxN,
MaxN1 = MaxN-1,
check_for_end(T, MaxN1).
%founds all the possible words
get_possible_words(_, MaxN, CurList) :-
check_for_end(CurList, MaxN), !.
get_possible_words(Word, MaxN, CurList) :-
get_list(CurList, MaxN, NewList, _),
get_possible_words(Word, MaxN, NewList), !,
get_word(NewList, Word, NewWord),
check_founded_word(NewWord).
get_list([], _, [], 1).
get_list([H|T], MaxN, NewList, Add) :-
get_list(T, MaxN, NewList1, Add1),
find_new_h(H, MaxN, NewH, Add1, Add),
NewList = [NewH|NewList1].
find_new_h(H, MaxN, NewH, Add1, Add) :-
NewH = H + Add1,
NewH <= MaxN,
Add = 0.
find_new_h(_, _, 1, Add1, Add1).
get_word([], _, "").
get_word([H|T], Word, NewWord) :-
letter_by_number(Word, H, Letter),
get_word(T, Word, NewWord1),
frontchar(NewWord, Letter, NewWord1).
letter_by_number(Word, 1, Ch) :-
frontchar(Word, Ch, _).
letter_by_number(Word, Num, Ch) :-
frontchar(Word, _, Left),
Num1 = Num - 1,
letter_by_number(Left, Num1, Ch).
check_founded_word(Word) :-
words(Word).
check_founded_word(Word) :-
write(Word), nl,
assert(words(Word)).
find_longest_word("", "", "").
find_longest_word(Str, Word, CurWord):-
frontchar(Str, C, S1),
isnt_white_space(C),
find_longest_word(S1, Word1, NewCurWord),
frontchar(CurWord, C, NewCurWord),
get_longest(Word1, CurWord, Word).
find_longest_word(Str, Word, CurWord):-
Str<>"",
frontchar(Str,_,Str1),
find_longest_word(Str1, Word, _),
CurWord="".
get_longest(Str1, Str2, Res):-
str_len(Str1,Len1), str_len(Str2,Len2),
Len1>Len2,
Res = Str1.
get_longest(_, Str2, Str2).
isnt_white_space(Ch):-
Ch<>' ', Ch<>','.
goal
entrance_point.
|