Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Lab3_1;
- uses System.SysUtils, System.StrUtils;
- Const
- Separators = ' ,.:;-?!()[]{}@#$%&';
- function GetEndIndex(InputString: String; Index: Integer): Integer;
- begin
- while not(ContainsText(Separators, InputString[Index])) do
- begin
- inc(Index)
- end;
- dec(Index);
- GetEndIndex := Index;
- end;
- function GetNextWordIndex(InputString: String; Index: Integer): Integer;
- begin
- inc(Index);
- while (ContainsText(Separators, InputString[Index])) do
- begin
- inc(Index)
- end;
- GetNextWordIndex := Index;
- end;
- function GetWord(InputString: String; FromIndex, ToIndex: Integer): String;
- var
- I: Integer;
- Ret: string;
- begin
- Ret := '';
- if ToIndex > high(InputString) then
- ToIndex := high(InputString);
- for I := FromIndex to ToIndex do
- Ret := Ret + InputString[I];
- GetWord := Ret;
- end;
- function GetInputType(): String;
- var
- Ret: string;
- IsCorrect: Boolean;
- begin
- IsCorrect := False;
- repeat
- Writeln('Выберите способ ввода предложения файл/консоль (ф/к)');
- Readln(Ret);
- if (Ret = 'ф') or (Ret = 'Ф') or (Ret = 'Файл') or (Ret = 'файл') then
- begin
- Ret := 'File';
- IsCorrect := True;
- end
- else if (Ret = 'к') or (Ret = 'К') or (Ret = 'Консоль') or
- (Ret = 'консоль') then
- begin
- Ret := 'Console';
- IsCorrect := True;
- end;
- until IsCorrect;
- GetInputType := Ret;
- end;
- function GetInputFilePath(): String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- IsCorrect := False;
- repeat
- Writeln('Введите абсолютный путь к файлу ');
- Readln(Path);
- if FileExists(Path) then
- IsCorrect := True
- else
- Writeln('Файл не найден');
- until IsCorrect;
- GetInputFilePath := Path;
- end;
- function GetInputStringFromConsole(): String;
- var
- Ret: String;
- begin
- Writeln('Введите исходное предложение');
- Readln(Ret);
- GetInputStringFromConsole := Ret;
- end;
- function GetInputStringFromFile(): String;
- var
- Ret, FilePath: String;
- InputFile: TextFile;
- begin
- FilePath := GetInputFilePath();
- AssignFile(InputFile, FilePath);
- Reset(InputFile);
- Read(InputFile, Ret);
- CloseFile(InputFile);
- GetInputStringFromFile := Ret;
- end;
- function GetInputString(): String;
- var
- Ret, InputType: String;
- begin
- InputType := GetInputType();
- if (InputType = 'Console') then
- Ret := GetInputStringFromConsole()
- else if (InputType = 'File') then
- Ret := GetInputStringFromFile();
- GetInputString := Ret;
- end;
- function SwapTwoWords(ToSwap: String;FirstStartindex: Integer): String;
- var
- Word1, Word2, Separator: String;
- FirstEndIndex, SecondStartIndex, SecondEndIndex: Integer;
- begin
- FirstEndIndex := GetEndIndex(ToSwap, FirstStartindex);
- SecondStartIndex := GetNextWordIndex(ToSwap, FirstEndIndex);
- SecondEndIndex := GetEndIndex(ToSwap, SecondStartIndex);
- Word1 := GetWord(ToSwap, FirstStartindex, FirstEndIndex);
- Word2 := GetWord(ToSwap, SecondStartIndex, SecondEndIndex);
- Separator := GetWord(ToSwap, FirstEndIndex + 1, SecondStartIndex - 1);
- SwapTwoWords := Word2 + Separator + Word1;
- end;
- function SwapString(StringToSwap: String): String;
- var
- I: Integer;
- Ret: String;
- begin
- I := 1;
- while I < Length(StringToSwap) + 1 do
- begin
- if ContainsText(Separators, StringToSwap[I]) then
- Ret := Ret + StringToSwap[I]
- else
- begin
- Ret := Ret + SwapTwoWords(StringToSwap, I);
- I:= Ret.Length;
- end;
- inc(I);
- end;
- SwapString := Ret;
- end;
- function GetOutputDirectory(): String;
- var
- Ret: String;
- IsCorrect: Boolean;
- begin
- IsCorrect := False;
- repeat
- Writeln('Введите директорию, в которую хотите сохранить вывод программы');
- Readln(Ret);
- if DirectoryExists(Ret) then
- IsCorrect := True
- else
- Writeln('Такой директории не существует.Попробуйте ещё раз');
- until IsCorrect;
- GetOutputDirectory := Ret;
- end;
- procedure PrintOutputToFile(Input, Output: String);
- var
- OutputFile: TextFile;
- Directory: String;
- begin
- Directory := GetOutputDirectory();
- AssignFile(OutputFile, Directory + '\output.txt');
- Rewrite(OutputFile);
- Writeln(OutputFile, 'Входная строка: ');
- Writeln(OutputFile, Input);
- Writeln(OutputFile, 'Выходная строка: ');
- Writeln(OutputFile, Output);
- Writeln('Файл сохранён по указанному пути');
- CloseFile(OutputFile);
- end;
- procedure PrintOutputToConsole(Input, Output: String);
- begin
- Writeln('Входная строка: ');
- Writeln(Input);
- Writeln('Выходная строка: ');
- Writeln(Output);
- end;
- var
- Input, Output: String;
- begin
- Input := GetInputString();
- Output := SwapString(Input);
- PrintOutputToConsole(Input, Output);
- PrintOutputToFile(Input, Output);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement