MaksNew

Untitled

Nov 8th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.36 KB | None | 0 0
  1. Program Lab3_1;
  2.  
  3. Uses
  4.     System.SysUtils;
  5.  
  6. type
  7.     InputType = (FromConsole, FromFile);
  8.  
  9.  
  10. Var
  11.     Path: String;
  12.     Sentence: String;
  13.     Order: Integer;
  14.  
  15.  
  16. function GetInputType(): InputType;
  17. var
  18.     Ret: InputType;
  19.     Input: String;
  20.     IsCorrect: Boolean;
  21. begin
  22.     IsCorrect := False;
  23.     repeat
  24.         Writeln('Выберите способ ввода предложения файл/консоль (ф/к)');
  25.         Readln(Input);
  26.         if (Input = 'ф') or (Input = 'Ф') or (Input = 'Файл') or (Input = 'файл')
  27.         then
  28.         begin
  29.             Ret := InputType.FromFile;
  30.             IsCorrect := True;
  31.         end
  32.         else if (Input = 'к') or (Input = 'К') or (Input = 'Консоль') or
  33.           (Input = 'консоль') then
  34.         begin
  35.             Ret := InputType.FromConsole;
  36.             IsCorrect := True;
  37.         end;
  38.     until IsCorrect;
  39.     Result := Ret;
  40. end;
  41.  
  42. function GetSentenceFromConsole(var Sentence:String): String;
  43. begin
  44.     Writeln('Введите исходное предложение');
  45.     Readln(Sentence);
  46.     Result := Sentence;
  47. end;
  48.  
  49. Function GetFilePath(): String;
  50. Var
  51.     Path: String;
  52.     IsCorrect: Boolean;
  53. Begin
  54.     Repeat
  55.         Writeln('Введите абсолютный путь к файлу ');
  56.         Readln(Path);
  57.         IsCorrect := False;
  58.         If FileExists(Path) then
  59.         Begin
  60.             Writeln('Файл успешно открыт');
  61.             IsCorrect := true
  62.         End
  63.         Else
  64.             Writeln('Файл не найден');
  65.  
  66.     Until IsCorrect;
  67.     Result := Path;
  68. End;
  69.  
  70. Function GetSentenceFromFile(Var Sentence:String): String;
  71. Var
  72.     SenFile: TextFile;
  73. Begin
  74.     Path := GetFilePath();
  75.     AssignFile(SenFile, Path);
  76.     Reset(SenFile);
  77.     Read(SenFile, Sentence);
  78.     CloseFile(SenFile);
  79.     Result := Sentence;
  80. End;
  81.  
  82. Procedure PrintSentenceBeforeEditing(Sentence: String);
  83. Begin
  84.     Writeln;
  85.     Writeln('Исходное предложение: ');
  86.     Write(Sentence);
  87.     Writeln;
  88. End;
  89.  
  90. function GetSentence(): String;
  91. var
  92.     SelectedInputType: InputType;
  93. begin
  94.     SelectedInputType := GetInputType();
  95.     if (SelectedInputType = InputType.FromConsole) then
  96.     begin
  97.         Sentence := GetSentenceFromConsole(Sentence)
  98.     end
  99.     else if (SelectedInputType = InputType.FromFile) then
  100.     begin
  101.         Sentence := GetSentenceFromFile(Sentence);
  102.     end;
  103.     PrintSentenceBeforeEditing(Sentence);
  104.     Result := Sentence;
  105. end;
  106.  
  107. Function Output(): String;
  108. Var
  109.     Patho: String;
  110.     IsCorrect: Boolean;
  111. Begin
  112.     IsCorrect := False;
  113.     Repeat
  114.         Writeln('Введите директорию, в которую хотите сохранить исправленное предложение');
  115.         Readln(Patho);
  116.         If DirectoryExists(Patho) then
  117.             IsCorrect := True
  118.         Else
  119.             Writeln('Такой директории не существует. Попробуйте ещё раз');
  120.     Until IsCorrect;
  121.     Result := Patho;
  122. End;
  123.  
  124. Procedure SaveSentenceToFile(Sentence: String);
  125. Var
  126.     I, J: Integer;
  127.     OutputFile: TextFile;
  128.     Directory: String;
  129. Begin
  130.     Directory := Output();
  131.     AssignFile(OutputFile, Directory + '\output.txt');
  132.     Rewrite(OutputFile);
  133.     Write(OutputFile,Sentence);
  134.     Writeln(OutputFile);
  135.     Writeln('Результат сохранён по указанному пути');
  136.     CloseFile(OutputFile);
  137.     Write;
  138. End;
  139.  
  140. Procedure PrintSentenceAfterEditing(Sentence: String);
  141. Begin
  142.     Writeln;
  143.     Writeln('Исправленное предложение:');
  144.     Writeln(Sentence);
  145.     Writeln;
  146. End;
  147.  
  148. Function Editing(Sentence: String): String;
  149. Var
  150. I:Integer;
  151. Begin
  152.     While Sentence[1]=' ' do
  153.     Begin
  154.         Delete(Sentence, 1, 1);
  155.     End;
  156.     While Pos('  ', Sentence) > 0 do
  157.     Begin
  158.         Delete(Sentence, Pos('  ', Sentence), 1);
  159.     End;
  160.     While Sentence[Length(Sentence)] = ' ' do
  161.     Begin
  162.          Delete(Sentence, Length(Sentence), 1);
  163.     End;
  164.     for I := 0 to Length(Sentence) do
  165.     if ((Sentence[I] in [',', '.', '?', '!', ';', ':']) and (Sentence[I-1]=' ')) then
  166.     Delete(Sentence, I-1, 1);
  167.     Editing(Sentence);
  168. Result := Sentence;
  169. End;
  170.  
  171. Begin
  172.     Sentence := GetSentence();
  173.     Sentence := Editing(Sentence);
  174.     SaveSentenceToFile(Sentence);
  175.     Readln;
  176. End.
Advertisement
Add Comment
Please, Sign In to add comment