Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.70 KB | None | 0 0
  1. program task1;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.     System.SysUtils;
  8.  
  9. function RemoveSpaces(Str: String): String;
  10. var
  11.     i: Integer;
  12.     ResultStr: String;
  13. begin
  14.     ResultStr := '';
  15.     for i := 1 to Length(Str) do
  16.     begin
  17.         if (Str[i] <> ' ') then
  18.             ResultStr := ResultStr + Str[i];
  19.     end;
  20.     RemoveSpaces := ResultStr;
  21. end;
  22.  
  23. function RemoveUnnecessary(Str: String): String;
  24. var
  25.     ResultStr: String;
  26.     Temp: Char;
  27. begin
  28.     Str := RemoveSpaces(Str);
  29.     ResultStr := '';
  30.     while (Length(Str) > 0) do
  31.     begin
  32.         Temp := Str[1];
  33.         Delete(Str, 1, 1);
  34.         if (Pos(Temp, Str) = 0) then
  35.             ResultStr := ResultStr + Temp;
  36.     end;
  37.     RemoveUnnecessary := ResultStr;
  38. end;
  39.  
  40. function ReadFile(FilePath: String): String;
  41. var
  42.     Str: String;
  43.     InputFile: TextFile;
  44. begin
  45.     Str := '';
  46.     if (FileExists(FilePath)) then
  47.     begin
  48.         AssignFile(InputFile, FilePath);
  49.         Reset(InputFile);
  50.         if (Not Eof(InputFile)) then
  51.             Readln(InputFile, Str);
  52.         CloseFile(InputFile);
  53.     end;
  54.     ReadFile := Str;
  55. end;
  56.  
  57. procedure SaveResult(ResultStr: String);
  58. var
  59.     Decision: String;
  60.     OutputFile: TextFile;
  61. begin
  62.     while ((Decision <> 'Y') and (Decision <> 'N')) do
  63.     begin
  64.         Writeln('Сохранить результат в файл? (Y - сохранить, N - не сохранять)');
  65.         Readln(Decision);
  66.     end;
  67.  
  68.     if (Decision = 'Y') then
  69.     begin
  70.         try
  71.             AssignFile(OutputFile, 'output.txt');
  72.             Rewrite(OutputFile);
  73.             Writeln(OutputFile, ResultStr);
  74.             CloseFile(OutputFile);
  75.             Writeln('Результат сохранен в файл output.txt');
  76.         except
  77.             Writeln('Произошла ошибка при сохранении файла');
  78.         end;
  79.     end;
  80. end;
  81.  
  82. procedure Main();
  83. var
  84.     InputType, ResultStr, Str, FilePath: String;
  85. begin
  86.     Writeln('Данная программа удаляет из строки все повторяющиеся символы и все пробелы');
  87.     Writeln;
  88.     while ((InputType <> 'C') and (InputType <> 'F')) do
  89.     begin
  90.         Writeln('Введите C, если хотите ввести строку из консоли, или F, если хотите ввести строку из файла');
  91.         Readln(InputType);
  92.     end;
  93.     if (InputType = 'C') then
  94.     begin
  95.         Writeln('Введите строку, из которой необходимо удалить повторяющиеся символы');
  96.         Readln(Str);
  97.         ResultStr := RemoveUnnecessary(Str);
  98.     end
  99.     else if (InputType = 'F') then
  100.     begin
  101.         Writeln('Введите путь к файлу');
  102.         Readln(FilePath);
  103.         ResultStr := RemoveUnnecessary(ReadFile(FilePath));
  104.     end;
  105.  
  106.     if (ResultStr <> '') then
  107.     begin
  108.         Writeln('Результат: ', ResultStr);
  109.         SaveResult(ResultStr);
  110.     end;
  111.     Readln;
  112. end;
  113.  
  114. begin
  115.     Main();
  116. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement