Advertisement
Egor_Vakar

lab3_1(delphi)

Nov 7th, 2021 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.83 KB | None | 0 0
  1. program lab3_1;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5.   System.SysUtils;
  6.  
  7. type
  8.     TArray = array of String;
  9.  
  10. Function TakeSource: Byte;
  11. const
  12.     CHOOSING_CONSOLE = 1;
  13.     CHOOSING_FILE = 2;
  14. var
  15.     Source: Byte;
  16.     IsCorrect: Boolean;
  17. begin
  18.     Source := 0;
  19.     repeat
  20.         IsCorrect := True;
  21.         try
  22.             Readln(Source);
  23.         except
  24.             Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  25.             IsCorrect := False;
  26.         end;
  27.         if (IsCorrect and (Source <> CHOOSING_CONSOLE) and (Source <> CHOOSING_FILE)) then
  28.         begin
  29.             Write('Incorrect input!!! Select the source:' + #13#10 + '1:Console'  + #13#10 + '2:File'  + #13#10 + 'Enter 1 or 2: ');
  30.             IsCorrect := False;
  31.         end;
  32.     until IsCorrect;
  33.     TakeSource := Source;
  34. end;
  35.  
  36. Function TakeInPath: String;
  37. var
  38.     Path: String;
  39.     IsCorrect: Boolean;
  40.     InFile: TextFile;
  41. begin
  42.     Write('Enter file path: ');
  43.     repeat
  44.         IsCorrect := True;
  45.         Readln(Path);
  46.         if not FileExists(Path) then
  47.         begin
  48.             Write('File is not found' + #13#10 + 'Enter file path: ');
  49.             IsCorrect := False;
  50.         end
  51.         else
  52.             if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
  53.                 (Path[Length(Path)- 2] <> 't') or (Path[Length(Path)- 3] <> '.')) then
  54.             begin
  55.                 Write('File is found, but it is not ".txt" type file' + #13#10 + 'Enter file path: ');
  56.                 IsCorrect := False;
  57.             end
  58.             else
  59.             begin
  60.                 Assign(InFile, Path);
  61.                 try
  62.                     Reset(InFile);
  63.                 except
  64.                     Write('Error!!! Program can''t open file!' + #13#10 + 'Enter file path: ');
  65.                     IsCorrect := False;
  66.  
  67.                 end;
  68.             end;
  69.     until IsCorrect;
  70.     CloseFile(InFile);
  71.     TakeInPath := Path;
  72. end;
  73.  
  74. Function TakeOutPath: String;
  75. var
  76.     Path: String;
  77.     IsCorrect: Boolean;
  78.     OutFile: TextFile;
  79. begin
  80.     Write('Enter file path: ');
  81.     repeat
  82.         IsCorrect := True;
  83.         Readln(Path);
  84.         if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
  85.             (Path[Length(Path)- 2] <> 't') or (Path[Length(Path) - 3] <> '.')) then
  86.         begin
  87.             Write('It should be a ".txt" type file' + #13#10 + 'Enter file path: ');
  88.             IsCorrect := False;
  89.         end
  90.         else
  91.         begin
  92.             AssignFile(OutFile, Path);
  93.             try
  94.                 Rewrite(OutFile);
  95.             except
  96.                 Write('Error!!! There are problems with file!' + #13#10 + 'Enter file path: ');
  97.                 IsCorrect := False;
  98.             end;
  99.         end;
  100.     until IsCorrect;
  101.     CloseFile(OutFile);
  102.     TakeOutPath := Path;
  103. end;
  104.  
  105. Function TakeSentenceFromConsole: String;
  106. var
  107.     Sentence: String;
  108. begin
  109.     Write('Enter the sentence: ');
  110.     Readln(Sentence);
  111.     TakeSentenceFromConsole := Sentence;
  112. end;
  113.  
  114. Function TakeSentenceFromFile(const Path: String): String;
  115. var
  116.     InFile: TextFile;
  117.     IsCorrect: Boolean;
  118.    Sentence: String;
  119. begin
  120.     IsCorrect := True;
  121.     AssignFile(InFile, Path);
  122.     Reset(InFile);
  123.     try
  124.         Read(InFile,Sentence);
  125.     except
  126.         Writeln('Incorrect file content!!!');
  127.         IsCorrect := False;
  128.     end;
  129.     if ((IsCorrect) and (not Eof(InFile))) then
  130.     begin
  131.         Writeln('Incorrect file content!!!');
  132.         IsCorrect := False;
  133.     end;
  134.     if  not IsCorrect then
  135.         Sentence := '';
  136.     TakeSentenceFromFile := Sentence;
  137.     CloseFile(InFile);
  138. end;
  139.  
  140. Function TakeSentence(Source: Byte): String;
  141. var
  142.     InPath: String;
  143.     Sentence: String;
  144. begin
  145.     if (Source = 1) then
  146.         Sentence := TakeSentenceFromConsole
  147.     else
  148.     begin
  149.         InPath := TakeInPath;
  150.         Sentence := TakeSentenceFromFile(InPath);
  151.         while (Sentence = '') do
  152.         begin
  153.             InPath := TakeInPath;
  154.             Sentence := TakeSentenceFromFile(InPath);
  155.         end;
  156.     end;
  157.     TakeSentence := Sentence;
  158. end;
  159.  
  160. Function MakeSentenceNormal(Sentence: String): String;
  161. begin
  162.     Sentence := StringReplace(Sentence, '.', ' ', [rfReplaceAll, rfIgnoreCase]);
  163.     Sentence := StringReplace(Sentence, ',', ' ', [rfReplaceAll, rfIgnoreCase]);
  164.     Sentence := StringReplace(Sentence, ',', ' ', [rfReplaceAll, rfIgnoreCase]);
  165.     Sentence := StringReplace(Sentence, '-', ' ', [rfReplaceAll, rfIgnoreCase]);
  166.     Sentence := StringReplace(Sentence, ';', ' ', [rfReplaceAll, rfIgnoreCase]);
  167.     Sentence := StringReplace(Sentence, ':', ' ', [rfReplaceAll, rfIgnoreCase]);
  168.     Sentence := StringReplace(Sentence, '!', ' ', [rfReplaceAll, rfIgnoreCase]);
  169.     Sentence := StringReplace(Sentence, '?', ' ', [rfReplaceAll, rfIgnoreCase]);
  170.     Sentence := StringReplace(Sentence, '—', ' ', [rfReplaceAll, rfIgnoreCase]);
  171.     Sentence := Trim(Sentence);
  172.     MakeSentenceNormal := Sentence;
  173. end;
  174.  
  175. Function FindArrayOfWords(Sentence: String): TArray;
  176. var
  177.     ArrayOfWords, TotalArrayOfWords: TArray;
  178.     WordsCounter, Index, i: Integer;
  179.     Word: String;
  180. begin
  181.     Sentence := MakeSentenceNormal(Sentence);
  182.     SetLength(ArrayOfWords, 100);
  183.     WordsCounter := 0;
  184.     Index := Pos(' ', Sentence);
  185.     while (Index <> 0) do
  186.     begin
  187.         Word := Copy(Sentence, 0, Index - 1);
  188.         If (Word <> '') then
  189.         begin
  190.             ArrayOfWords[wordsCounter] := Word;
  191.             WordsCounter := WordsCounter + 1;
  192.         end;
  193.         Sentence := Copy(Sentence, Index + 1);
  194.         Index := Pos(' ', Sentence);
  195.     end;
  196.     if (Sentence <> '') then
  197.     begin
  198.         ArrayOfWords[wordsCounter] := Sentence;
  199.         WordsCounter := WordsCounter + 1;
  200.     end;
  201.     SetLength(TotalArrayOfWords, WordsCounter);
  202.     for i := 0 to (WordsCounter - 1) do
  203.         TotalArrayOfWords[i] := ArrayOfWords[i];
  204.     FindArrayOfWords := TotalArrayOfWords;
  205. end;
  206.  
  207. Function FindAnswer(ArrayOfWords: TArray): Integer;
  208. var
  209.     Counter, i: Integer;
  210. begin
  211.     Counter := 0;
  212.     for i := 0 to (Length(ArrayOfWords) - 1) do
  213.         if (AnsiCompareText(ArrayOfWords[i],ArrayOfWords[Length(ArrayOfWords) - 1]) = 0) then
  214.             Counter := Counter + 1;
  215.     FindAnswer := Counter;
  216. end;
  217.  
  218. Procedure OutputToConsole(const Sentence: String;  const Answer: Integer);
  219. begin
  220.     Writeln('Sentence is:', #13#10, Sentence, #13#10, 'Last word founded ',  Answer, ' times.');
  221. end;
  222.  
  223. Procedure OutputToFile(const Path: String; const Sentence: String;  const Answer: Integer);
  224. var
  225.     OutFile: TextFile;
  226. begin
  227.     AssignFile(OutFile, Path);
  228.     Rewrite(OutFile);
  229.     Writeln(OutFile, 'Sentence is:', #13#10, Sentence, #13#10, 'Last word founded ',  Answer, ' times.');
  230.     CloseFile(OutFile);
  231.     Writeln('Done');
  232. end;
  233.  
  234. Procedure Output(const Source: Byte; const Sentence: String;  const Answer: Integer);
  235. var
  236.     OutPath: String;
  237. begin
  238.     if (Source = 1) then
  239.         OutputToConsole(Sentence,Answer)
  240.     else
  241.     begin
  242.         OutPath := TakeOutPath;
  243.         OutputToFile(OutPath,Sentence,Answer);
  244.     end;
  245. end;
  246.  
  247. var
  248.     InputSource, OutputSource: Byte;
  249.     Sentence: String;
  250.     Answer: Integer;
  251.     ArrayOfWords: TArray;
  252. begin
  253.     Write('Welcome to the program that count how many times the last word occurs in a sentence.' + #13#10 + 'Select the source for entering the sentence:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
  254.     InputSource := TakeSource();
  255.     Sentence := TakeSentence(InputSource);
  256.     ArrayOfWords := FindArrayOfWords(Sentence);
  257.     Answer := FindAnswer(ArrayOfWords);
  258.     Write('Select the source for output: '+ #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
  259.     OutputSource := TakeSource();
  260.     Output(OutputSource, Sentence, Answer);
  261.     Readln;
  262. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement