Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program task1;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- function RemoveSpaces(Str: String): String;
- var
- i: Integer;
- ResultStr: String;
- begin
- ResultStr := '';
- for i := 1 to Length(Str) do
- begin
- if (Str[i] <> ' ') then
- ResultStr := ResultStr + Str[i];
- end;
- RemoveSpaces := ResultStr;
- end;
- function RemoveUnnecessary(Str: String): String;
- var
- ResultStr: String;
- Temp: Char;
- begin
- Str := RemoveSpaces(Str);
- ResultStr := '';
- while (Length(Str) > 0) do
- begin
- Temp := Str[1];
- Delete(Str, 1, 1);
- if (Pos(Temp, Str) = 0) then
- ResultStr := ResultStr + Temp;
- end;
- RemoveUnnecessary := ResultStr;
- end;
- function ReadFile(FilePath: String): String;
- var
- Str: String;
- InputFile: TextFile;
- begin
- Str := '';
- if (FileExists(FilePath)) then
- begin
- AssignFile(InputFile, FilePath);
- Reset(InputFile);
- if (Not Eof(InputFile)) then
- Readln(InputFile, Str);
- CloseFile(InputFile);
- end;
- ReadFile := Str;
- end;
- procedure SaveResult(ResultStr: String);
- var
- Decision: String;
- OutputFile: TextFile;
- begin
- while ((Decision <> 'Y') and (Decision <> 'N')) do
- begin
- Writeln('Сохранить результат в файл? (Y - сохранить, N - не сохранять)');
- Readln(Decision);
- end;
- if (Decision = 'Y') then
- begin
- try
- AssignFile(OutputFile, 'output.txt');
- Rewrite(OutputFile);
- Writeln(OutputFile, ResultStr);
- CloseFile(OutputFile);
- Writeln('Результат сохранен в файл output.txt');
- except
- Writeln('Произошла ошибка при сохранении файла');
- end;
- end;
- end;
- procedure Main();
- var
- InputType, ResultStr, Str, FilePath: String;
- begin
- Writeln('Данная программа удаляет из строки все повторяющиеся символы и все пробелы');
- Writeln;
- while ((InputType <> 'C') and (InputType <> 'F')) do
- begin
- Writeln('Введите C, если хотите ввести строку из консоли, или F, если хотите ввести строку из файла');
- Readln(InputType);
- end;
- if (InputType = 'C') then
- begin
- Writeln('Введите строку, из которой необходимо удалить повторяющиеся символы');
- Readln(Str);
- ResultStr := RemoveUnnecessary(Str);
- end
- else if (InputType = 'F') then
- begin
- Writeln('Введите путь к файлу');
- Readln(FilePath);
- ResultStr := RemoveUnnecessary(ReadFile(FilePath));
- end;
- if (ResultStr <> '') then
- begin
- Writeln('Результат: ', ResultStr);
- SaveResult(ResultStr);
- end;
- Readln;
- end;
- begin
- Main();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement