Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Laba3_2;
- Uses
- System.SysUtils;
- Type
- TSet = Set of Char;
- TArray = Array of Char;
- Const
- Console = 1;
- CFile = 2;
- Exclusion = [' ',',',':',';'];
- Function ChooseInput() : Integer;
- Var
- Choose : Integer;
- IsCorrect : Boolean;
- Begin
- Choose := 0;
- Repeat
- IsCorrect := True;
- Try
- Readln(Choose);
- Except
- Writeln('Ошибка ввода! Повторите выбор: ');
- IsCorrect := False;
- End;
- If (IsCorrect) and (Choose <> Console) and (Choose <> CFile) then
- Begin
- Writeln('Ошибка ввода! Повторите выбор: ');
- IsCorrect := False;
- End;
- Until IsCorrect;
- ChooseInput := Choose;
- End;
- Function CheckFile(Choose : Integer; Path : String) : Integer;
- Var
- MyFile : TextFile;
- Begin
- If Choose = CFile then
- Begin
- AssignFile(MyFile,Path);
- Reset(MyFile);
- If eof(MyFile) then
- Begin
- Choose := 1;
- Writeln('Файл пуст. Ввод данных будет производиться с консоли.');
- End;
- End;
- CheckFile := Choose;
- End;
- Function InputPathInput(Choose : Integer) : String;
- Var
- Path : String;
- IsCorrect : Boolean;
- Begin
- Path := '';
- If Choose = CFile then
- Begin
- Writeln('Введите абсолютную ссылку на файл для ввода: ');
- IsCorrect := False;
- Repeat
- Readln(Path);
- If FileExists(Path) then
- IsCorrect := True
- Else
- Writeln('Введите корректное расположение: ');
- Until IsCorrect;
- End;
- InputPathInput := Path;
- End;
- Function InputPathOutput() : String;
- Var
- Path : String;
- IsCorrect : Boolean;
- Begin
- Writeln('Введите абсолютную ссылку на файл для вывода: ');
- IsCorrect := False;
- Repeat
- Readln(Path);
- If FileExists(Path) then
- IsCorrect := True
- Else
- Writeln('Введите корректное расположение: ');
- Until IsCorrect;
- InputPathOutput := Path;
- End;
- Function InputStringConsole() : String;
- Var
- Str : String;
- IsCorrect : Boolean;
- Begin
- Str := '';
- Repeat
- IsCorrect := True;
- Write('Введите последовательность символов: ');
- Try
- Readln(Str);
- Except
- Writeln('Ошибка ввода!');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputStringConsole := Str;
- End;
- Function InputStringFile(Path : String) : String;
- Var
- Str : String;
- MyFile : TextFile;
- Begin
- AssignFile(MyFile,Path);
- Reset(MyFile);
- Try
- Readln(MyFile, Str);
- Except
- Writeln('Ошибка ввода!');
- Str := InputStringConsole();
- End;
- Close(MyFile);
- InputStringFile := Str;
- End;
- Function InputString(Choose : Integer; Path : String) : String;
- Var
- Str : String;
- Begin
- If Choose = Console then
- Str := InputStringConsole()
- Else
- Str := InputStringFile(Path);
- InputString := Str;
- End;
- Function CheckExcessSpace(Str : String) : String;
- Var
- I : Integer;
- Temp, Temp2 : Char;
- Begin
- I := 1;
- While I < High(Str) do
- Begin
- Temp := Str[I];
- Temp2 := Str[I + 1];
- If Temp in Exclusion then
- If Temp2 in Exclusion then
- Delete(Str, I, 1)
- Else
- Inc(I)
- Else
- Inc(I);
- End;
- CheckExcessSpace := Str;
- End;
- Function CheckEmptiness(Str : String) : Boolean;
- Var
- Emptiness : Boolean;
- I, Counter : Integer;
- Temp : Char;
- Begin
- Emptiness := False;
- I := 1;
- Counter := High(Str) + 1;
- While (I < Counter) or (Emptiness = True) do
- Begin
- Temp := Str[I];
- If Temp in Exclusion then
- Emptiness := True
- Else
- Emptiness := False;
- Inc(I);
- End;
- CheckEmptiness := Emptiness;
- End;
- Function ChangeString (Str : String; Emptiness : Boolean) : String;
- Var
- I, J, CounterSpace, Counter, Counter2, Counter3,Counter4 : Integer;
- BeginStr, EndStr : String;
- Temp, Temp2 : Char;
- Begin
- If (Emptiness = False) and (Length(Str) <> 1) then
- Begin
- I := 1;
- Counter := High(Str) + 1;
- CounterSpace := 0;
- Counter3 := High(Str);
- BeginStr := '';
- EndStr := '';
- While I < Counter do
- Begin
- Inc(I);
- Temp := Str[I];
- If Temp in Exclusion then
- Begin
- Inc(CounterSpace);
- Counter2 := I - 1;
- Counter4 := I + 1;
- For J := 1 to Counter2 do
- Begin
- Temp2 := Str[J];
- BeginStr := BeginStr + Temp2;
- End;
- For J := Counter3 downto Counter4 do
- Begin
- Temp2 := Str[J];
- EndStr := Temp2 + EndStr;
- End;
- Inc(I);
- If CounterSpace mod 2 = 1 then
- Str := BeginStr + ')' + Temp + '[' + EndStr
- Else
- Str := BeginStr + ']' + Temp + '(' + EndStr;
- End;
- End;
- If CounterSpace mod 2 = 1 then
- Str := '(' + Str + ']'
- Else
- Str := '(' + Str + ')';
- End
- Else
- Str := '(' + Str + ')';
- ChangeString := Str;
- End;
- Procedure OutputSetFile(Str : String;Path : String);
- Var
- MyFile : TextFile;
- I : Integer;
- Begin
- AssignFile(MyFile,Path);
- Rewrite(MyFile);
- Write(MyFile,'Строка после преобразований имеет вид: ');
- For I := 1 to High(Str) do
- Write(MyFile,Str[I]);
- Writeln('Данные записаны в файл');
- Close(MyFile);
- End;
- Procedure OutputStringConsole(Str : String);
- Var
- I : Integer;
- Begin
- Write('Строка после преобразований имеет вид: ');
- For I := 1 to High(Str) do
- Write(Str[I]);
- End;
- Procedure OutputString(Path : String;Choose : Integer;Str : String);
- Begin
- If Choose = Console then
- OutputStringConsole(Str)
- Else
- Begin
- Path := InputPathOutput();
- OutputSetFile(Str,Path);
- End;
- End;
- Procedure OutputEmpty();
- Begin
- Writeln('Данная строка не содержит слов');
- End;
- Procedure Output(Path : String;Choose : Integer;Emptiness : Boolean; Str : String);
- Begin
- If Emptiness = True then
- OutputEmpty()
- Else
- OutputString(Path,Choose,Str);
- End;
- Procedure Main();
- Var
- Choose : Integer;
- Path, Str : String;
- Emptiness : Boolean;
- Begin
- Writeln('В заданном тексте каждое нечетное слово заключить в круглые скобки, а каждое четное слово заключить в квадратные скобки');
- Writeln('Выберите, откуда будет производиться ввод:');
- Writeln('1.Консоль');
- Writeln('2.Файл');
- Choose := ChooseInput();
- Path := InputPathInput(Choose);
- Choose := CheckFile(Choose,Path);
- Str := InputString(Choose, Path);
- Str := CheckExcessSpace(Str);
- Emptiness := CheckEmptiness(Str);
- Str := ChangeString(Str, Emptiness);
- Writeln('Выберите, куда будет производиться вывод:');
- Writeln('1.Консоль');
- Writeln('2.Файл');
- Choose := ChooseInput();
- Output(Path,Choose,Emptiness,Str);
- Readln;
- End;
- Begin
- Main;
- End.
Advertisement
Add Comment
Please, Sign In to add comment