Нахождение в текстовом файле строк, в которых есть некоторое ключевое слово - Prolog

Узнай цену своей работы

Формулировка задачи:

Помогите мне пожалуйста. Нахождение в текстовом файле строк, в которых есть некоторое ключевое слово.
Листинг программы
  1. DOMAINS
  2. list_string = string*
  3. file = myfile
  4. PREDICATES
  5. nondeterm result
  6. nondeterm work_file(string,list_string)
  7. nondeterm make_list(string,char,list_string)
  8. nondeterm read_list(list_string)
  9. save_to_file(string,list_string)
  10. transform(char,char)
  11. write_list(list_string)
  12. CLAUSES
  13. work_file(Namefile,List):-
  14. nl,
  15. write(" Create a file? (Y / N) "), nl,
  16. write(" By default, the processing of strings will be performed in an already created file "), nl,
  17. readchar(C),
  18. make_list(Namefile,C,List).
  19. make_list(Namefile,C,List):-
  20. transform(C,C1),
  21. C1 = 'y',
  22. write(" Enter a list of strings: "), nl,
  23. read_list(List),
  24. save_to_file(Namefile,List),!.
  25. make_list(Namefile,_,List):-
  26. existfile(Namefile),
  27. openread(myfile,Namefile),
  28. readdevice(myfile),
  29. read_list(List),
  30. readdevice(keyboard),
  31. closefile(myfile);
  32. write(" The file does not exist. It is recommended to create it. "), nl,
  33. make_list(Namefile,'y',List).
  34. write_list([]).
  35. write_list([H|T]):-
  36. write(H),nl,
  37. write_list(T).
  38. transform('Y','y').
  39. transform('y','y').
  40. read_list([H|T]):-
  41. readln(H),
  42. H <> "",
  43. read_list(T).
  44. read_list([]).
  45. save_to_file(Name,List):-
  46. openwrite(myfile,Name),
  47. writedevice(myfile),
  48. write_list(List),nl,
  49. writedevice(screen),
  50. closefile(myfile).
  51. result:-
  52. write(" Find strings in a text file that have a certain keyword. "), nl,
  53. work_file("FILE_6.txt",List1),
  54. write(" Enter the keyword: "),
  55. readln(Word),
  56. write(" Strings before processing: "), nl,
  57. write_list(List1),nl,
  58. readchar(_),
  59. write(" Strings after processing: "), nl,
  60. write_list(List2),nl,
  61. readchar(_),!.
  62. GOAL
  63. result.

Решение задачи: «Нахождение в текстовом файле строк, в которых есть некоторое ключевое слово»

textual
Листинг программы
  1. DOMAINS
  2.  
  3.     list_string = string*
  4.     file = myfile  
  5.    
  6.  PREDICATES
  7.  
  8.     nondeterm result    
  9.     nondeterm work_file(string,list_string)
  10.     nondeterm make_list(string,char,list_string)
  11.     nondeterm read_list(list_string)
  12.     nondeterm find_words(string,list_string)
  13.     save_to_file(string,list_string)
  14.     transform(char,char)
  15.     write_list(list_string)    
  16.        
  17.  CLAUSES  
  18.  
  19.     work_file(Namefile,List):-
  20.         nl,
  21.         write(" Create a file? (Y / N) "), nl,
  22.         write(" By default, the processing of strings will be performed in an already created file "), nl,
  23.         readchar(C),
  24.         make_list(Namefile,C,List).    
  25.        
  26.     make_list(Namefile,C,List):-
  27.         transform(C,C1),
  28.         C1 = 'y',
  29.         write(" Enter a list of strings: "), nl,
  30.         read_list(List),
  31.         save_to_file(Namefile,List),!.
  32.     make_list(Namefile,_,List):-
  33.         existfile(Namefile),
  34.         openread(myfile,Namefile),
  35.         readdevice(myfile),
  36.         read_list(List),
  37.         readdevice(keyboard),
  38.         closefile(myfile);
  39.         write(" The file does not exist. It is recommended to create it. "), nl,
  40.         make_list(Namefile,'y',List).
  41.        
  42.     write_list([]).
  43.     write_list([H|T]):-
  44.         write(H),nl,
  45.         write_list(T).
  46.        
  47.     transform('Y','y').
  48.     transform('y','y').
  49.    
  50.     read_list([H|T]):-
  51.         readln(H),
  52.         H <> "",
  53.         read_list(T).
  54.     read_list([]).
  55.    
  56.     save_to_file(Name,List):-
  57.         openwrite(myfile,Name),
  58.         writedevice(myfile),
  59.         write_list(List),nl,
  60.         writedevice(screen),
  61.         closefile(myfile).
  62.        
  63.     find_words("",[]).
  64.     find_words(Str,[Word|T]):-
  65.     fronttoken(Str,Word,Ost_str),
  66.     frontchar(Word,C,Ost),
  67.     find_words(Ost_str,T).
  68.     find_words(Str,T):-
  69.     fronttoken(Str,_,Ost),
  70.     find_words(Ost,T).
  71.        
  72.         result:-  
  73.         write("   Find strings in a text file that have a certain keyword. "), nl,
  74.         work_file("FILE_6.txt",List1),
  75.     write(" Strings before processing: "), nl,
  76.     write_list(List1),nl,
  77.     readchar(_),
  78.     write("Enter the keyword:  "),
  79.     readln(Str),nl,
  80.     write(" Strings after processing: "), nl,
  81.     readchar(_),!.
  82.      
  83.  GOAL
  84.         result.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 3.917 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы