Нахождение в текстовом файле строк, в которых есть некоторое ключевое слово - Prolog
Формулировка задачи:
Помогите мне пожалуйста.
Нахождение в текстовом файле строк, в которых есть некоторое ключевое слово.
Листинг программы
- DOMAINS
- list_string = string*
- file = myfile
- PREDICATES
- nondeterm result
- nondeterm work_file(string,list_string)
- nondeterm make_list(string,char,list_string)
- nondeterm read_list(list_string)
- save_to_file(string,list_string)
- transform(char,char)
- write_list(list_string)
- CLAUSES
- work_file(Namefile,List):-
- nl,
- write(" Create a file? (Y / N) "), nl,
- write(" By default, the processing of strings will be performed in an already created file "), nl,
- readchar(C),
- make_list(Namefile,C,List).
- make_list(Namefile,C,List):-
- transform(C,C1),
- C1 = 'y',
- write(" Enter a list of strings: "), nl,
- read_list(List),
- save_to_file(Namefile,List),!.
- make_list(Namefile,_,List):-
- existfile(Namefile),
- openread(myfile,Namefile),
- readdevice(myfile),
- read_list(List),
- readdevice(keyboard),
- closefile(myfile);
- write(" The file does not exist. It is recommended to create it. "), nl,
- make_list(Namefile,'y',List).
- write_list([]).
- write_list([H|T]):-
- write(H),nl,
- write_list(T).
- transform('Y','y').
- transform('y','y').
- read_list([H|T]):-
- readln(H),
- H <> "",
- read_list(T).
- read_list([]).
- save_to_file(Name,List):-
- openwrite(myfile,Name),
- writedevice(myfile),
- write_list(List),nl,
- writedevice(screen),
- closefile(myfile).
- result:-
- write(" Find strings in a text file that have a certain keyword. "), nl,
- work_file("FILE_6.txt",List1),
- write(" Enter the keyword: "),
- readln(Word),
- write(" Strings before processing: "), nl,
- write_list(List1),nl,
- readchar(_),
- write(" Strings after processing: "), nl,
- write_list(List2),nl,
- readchar(_),!.
- GOAL
- result.
Решение задачи: «Нахождение в текстовом файле строк, в которых есть некоторое ключевое слово»
textual
Листинг программы
- DOMAINS
- list_string = string*
- file = myfile
- PREDICATES
- nondeterm result
- nondeterm work_file(string,list_string)
- nondeterm make_list(string,char,list_string)
- nondeterm read_list(list_string)
- nondeterm find_words(string,list_string)
- save_to_file(string,list_string)
- transform(char,char)
- write_list(list_string)
- CLAUSES
- work_file(Namefile,List):-
- nl,
- write(" Create a file? (Y / N) "), nl,
- write(" By default, the processing of strings will be performed in an already created file "), nl,
- readchar(C),
- make_list(Namefile,C,List).
- make_list(Namefile,C,List):-
- transform(C,C1),
- C1 = 'y',
- write(" Enter a list of strings: "), nl,
- read_list(List),
- save_to_file(Namefile,List),!.
- make_list(Namefile,_,List):-
- existfile(Namefile),
- openread(myfile,Namefile),
- readdevice(myfile),
- read_list(List),
- readdevice(keyboard),
- closefile(myfile);
- write(" The file does not exist. It is recommended to create it. "), nl,
- make_list(Namefile,'y',List).
- write_list([]).
- write_list([H|T]):-
- write(H),nl,
- write_list(T).
- transform('Y','y').
- transform('y','y').
- read_list([H|T]):-
- readln(H),
- H <> "",
- read_list(T).
- read_list([]).
- save_to_file(Name,List):-
- openwrite(myfile,Name),
- writedevice(myfile),
- write_list(List),nl,
- writedevice(screen),
- closefile(myfile).
- find_words("",[]).
- find_words(Str,[Word|T]):-
- fronttoken(Str,Word,Ost_str),
- frontchar(Word,C,Ost),
- find_words(Ost_str,T).
- find_words(Str,T):-
- fronttoken(Str,_,Ost),
- find_words(Ost,T).
- result:-
- write(" Find strings in a text file that have a certain keyword. "), nl,
- work_file("FILE_6.txt",List1),
- write(" Strings before processing: "), nl,
- write_list(List1),nl,
- readchar(_),
- write("Enter the keyword: "),
- readln(Str),nl,
- write(" Strings after processing: "), nl,
- readchar(_),!.
- GOAL
- result.
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д