MaksNew

Untitled

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