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; I: Integer): Integer;
- begin
- while not(ContainsText(Separators, InputString[I])) do
- begin
- inc(I)
- end;
- dec(I);
- GetEndIndex := I;
- end;
- function GetNextWordIndex(InputString: String; I: Integer): Integer;
- begin
- inc(I);
- while (ContainsText(Separators, InputString[I])) do
- begin
- inc(I)
- end;
- GetNextWordIndex := I;
- 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
- repeat
- Writeln('Введите абсолютный путь к файлу ');
- Readln(Path);
- IsCorrect := False;
- 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; var FirstStartCounts: Integer): String;
- var
- Word1, Word2, Separator: String;
- FirstEndIndex, SecondStartIndex, SecondEndIndex: Integer;
- begin
- FirstEndIndex := GetEndIndex(ToSwap, FirstStartCounts);
- SecondStartIndex := GetNextWordIndex(ToSwap, FirstEndIndex);
- SecondEndIndex := GetEndIndex(ToSwap, SecondStartIndex);
- Word1 := GetWord(ToSwap, FirstStartCounts, FirstEndIndex);
- Word2 := GetWord(ToSwap, SecondStartIndex, SecondEndIndex);
- Separator := GetWord(ToSwap, FirstEndIndex + 1, SecondStartIndex - 1);
- FirstStartCounts := SecondEndIndex;
- 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
- Ret := Ret + SwapTwoWords(StringToSwap, I);
- 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