Amorf

1234

Nov 25th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.90 KB | None | 0 0
  1. Program Laba3_2;
  2.  
  3. Uses
  4.     System.SysUtils;
  5.  
  6. Type
  7.     TSet = Set of Char;
  8.     TArray = Array of Char;
  9.  
  10. Const
  11.     Console = 1;
  12.     CFile = 2;
  13.     Exclusion = [' ',',',':',';'];
  14.  
  15. Function ChooseInput() : Integer;
  16.  
  17. Var
  18.     Choose : Integer;
  19.     IsCorrect : Boolean;
  20. Begin
  21.     Choose := 0;
  22.     Repeat
  23.         IsCorrect := True;
  24.         Try
  25.             Readln(Choose);
  26.         Except
  27.             Writeln('Ошибка ввода! Повторите выбор: ');
  28.             IsCorrect := False;
  29.         End;
  30.         If (IsCorrect) and (Choose <> Console) and (Choose <> CFile) then
  31.         Begin
  32.             Writeln('Ошибка ввода! Повторите выбор: ');
  33.             IsCorrect := False;
  34.         End;
  35.     Until IsCorrect;
  36.     ChooseInput := Choose;
  37. End;
  38.  
  39. Function CheckFile(Choose : Integer; Path : String) : Integer;
  40. Var
  41.     MyFile : TextFile;
  42. Begin
  43.     If Choose = CFile then
  44.     Begin
  45.         AssignFile(MyFile,Path);
  46.         Reset(MyFile);
  47.         If eof(MyFile) then
  48.         Begin
  49.             Choose := 1;
  50.             Writeln('Файл пуст. Ввод данных будет производиться с консоли.');
  51.         End;
  52.     End;
  53.     CheckFile := Choose;
  54. End;
  55.  
  56. Function InputPathInput(Choose : Integer) : String;
  57. Var
  58.     Path : String;
  59.     IsCorrect : Boolean;
  60. Begin
  61.     Path := '';
  62.     If Choose = CFile then
  63.     Begin
  64.         Writeln('Введите абсолютную ссылку на файл для ввода: ');
  65.         IsCorrect := False;
  66.         Repeat
  67.             Readln(Path);
  68.             If FileExists(Path) then
  69.                   IsCorrect := True
  70.             Else
  71.                   Writeln('Введите корректное расположение: ');
  72.         Until IsCorrect;
  73.     End;
  74.     InputPathInput := Path;
  75. End;
  76.  
  77. Function InputPathOutput() : String;
  78. Var
  79.     Path : String;
  80.     IsCorrect : Boolean;
  81. Begin
  82.     Writeln('Введите абсолютную ссылку на файл для вывода: ');
  83.     IsCorrect := False;
  84.     Repeat
  85.         Readln(Path);
  86.         If FileExists(Path) then
  87.             IsCorrect := True
  88.         Else
  89.             Writeln('Введите корректное расположение: ');
  90.     Until IsCorrect;
  91.     InputPathOutput := Path;
  92. End;
  93.  
  94. Function InputStringConsole() : String;
  95. Var
  96.     Str : String;
  97.     IsCorrect : Boolean;
  98. Begin
  99.     Str := '';
  100.     Repeat
  101.         IsCorrect := True;
  102.         Write('Введите последовательность символов: ');
  103.         Try
  104.             Readln(Str);
  105.         Except
  106.             Writeln('Ошибка ввода!');
  107.             IsCorrect := False;
  108.         End;
  109.     Until IsCorrect;
  110.     InputStringConsole := Str;
  111. End;
  112.  
  113. Function InputStringFile(Path : String) : String;
  114.  
  115. Var
  116.     Str : String;
  117.     MyFile : TextFile;
  118. Begin
  119.     AssignFile(MyFile,Path);
  120.     Reset(MyFile);
  121.     Try
  122.         Readln(MyFile, Str);
  123.     Except
  124.         Writeln('Ошибка ввода!');
  125.         Str := InputStringConsole();
  126.     End;
  127.     Close(MyFile);
  128.     InputStringFile := Str;
  129. End;
  130.  
  131.  
  132. Function InputString(Choose : Integer; Path : String) : String;
  133. Var
  134.     Str : String;
  135. Begin
  136.     If Choose = Console then
  137.         Str := InputStringConsole()
  138.     Else
  139.         Str := InputStringFile(Path);
  140.     InputString := Str;
  141. End;
  142.  
  143. Function CheckExcessSpace(Str : String) : String;
  144. Var
  145.     I : Integer;
  146.     Temp, Temp2 : Char;
  147. Begin
  148.     I := 1;
  149.     While I < High(Str) do
  150.     Begin
  151.         Temp := Str[I];
  152.         Temp2 := Str[I + 1];
  153.         If Temp in Exclusion then
  154.             If Temp2 in Exclusion then
  155.                 Delete(Str, I, 1)
  156.             Else
  157.                 Inc(I)
  158.         Else
  159.             Inc(I);
  160.     End;
  161.     CheckExcessSpace := Str;
  162. End;
  163.  
  164. Function CheckEmptiness(Str : String) : Boolean;
  165. Var
  166.     Emptiness : Boolean;
  167.     I, Counter : Integer;
  168.     Temp : Char;
  169. Begin
  170.     Emptiness := False;
  171.     I := 1;
  172.     Counter := High(Str) + 1;
  173.     While (I < Counter) or (Emptiness = True) do
  174.     Begin
  175.         Temp := Str[I];
  176.         If Temp in Exclusion then
  177.             Emptiness := True
  178.         Else
  179.             Emptiness := False;
  180.         Inc(I);
  181.     End;
  182.     CheckEmptiness := Emptiness;
  183. End;
  184.  
  185. Function ChangeString (Str : String; Emptiness : Boolean) : String;
  186. Var
  187.     I, J, CounterSpace, Counter, Counter2, Counter3,Counter4 : Integer;
  188.     BeginStr, EndStr : String;
  189.     Temp, Temp2 : Char;
  190. Begin
  191.     If (Emptiness = False) and (Length(Str) <> 1) then
  192.     Begin
  193.         I := 1;
  194.         Counter := High(Str) + 1;
  195.         CounterSpace := 0;
  196.         Counter3 := High(Str);
  197.         BeginStr := '';
  198.         EndStr := '';
  199.         While I < Counter do
  200.         Begin
  201.             Inc(I);
  202.             Temp := Str[I];
  203.             If Temp in Exclusion then
  204.             Begin
  205.                 Inc(CounterSpace);
  206.                 Counter2 := I - 1;
  207.                 Counter4 := I + 1;
  208.                 For J := 1 to Counter2 do
  209.                 Begin
  210.                     Temp2 := Str[J];
  211.                     BeginStr := BeginStr + Temp2;
  212.                 End;
  213.                 For J := Counter3 downto Counter4 do
  214.                 Begin
  215.                     Temp2 := Str[J];
  216.                     EndStr := Temp2 + EndStr;
  217.                 End;
  218.                 Inc(I);
  219.                 If CounterSpace mod 2 = 1 then
  220.                     Str := BeginStr + ')' + Temp + '[' + EndStr
  221.                 Else
  222.                     Str := BeginStr + ']' + Temp + '(' + EndStr;
  223.             End;
  224.  
  225.         End;
  226.         If CounterSpace mod 2 = 1 then
  227.             Str := '(' + Str + ']'
  228.         Else
  229.             Str := '(' + Str + ')';
  230.     End
  231.     Else
  232.         Str := '(' + Str + ')';
  233.     ChangeString := Str;
  234. End;
  235.  
  236. Procedure OutputSetFile(Str : String;Path : String);
  237.  
  238. Var
  239.     MyFile : TextFile;
  240.     I : Integer;
  241. Begin
  242.     AssignFile(MyFile,Path);
  243.     Rewrite(MyFile);
  244.     Write(MyFile,'Строка после преобразований имеет вид: ');
  245.     For I := 1 to High(Str) do
  246.         Write(MyFile,Str[I]);
  247.     Writeln('Данные записаны в файл');
  248.     Close(MyFile);
  249. End;
  250.  
  251. Procedure OutputStringConsole(Str : String);
  252. Var
  253.     I : Integer;
  254. Begin
  255.     Write('Строка после преобразований имеет вид: ');
  256.     For I := 1 to High(Str) do
  257.         Write(Str[I]);
  258. End;
  259.  
  260. Procedure OutputString(Path : String;Choose : Integer;Str : String);
  261. Begin
  262.     If Choose = Console then
  263.         OutputStringConsole(Str)
  264.     Else
  265.     Begin
  266.         Path := InputPathOutput();
  267.         OutputSetFile(Str,Path);
  268.     End;
  269. End;
  270.  
  271. Procedure OutputEmpty();
  272. Begin
  273.     Writeln('Данная строка не содержит слов');
  274. End;
  275.  
  276. Procedure Output(Path : String;Choose : Integer;Emptiness : Boolean; Str : String);
  277. Begin
  278.     If Emptiness = True then
  279.         OutputEmpty()
  280.     Else
  281.         OutputString(Path,Choose,Str);
  282. End;
  283.  
  284. Procedure Main();
  285.  
  286. Var
  287.     Choose : Integer;
  288.     Path, Str : String;
  289.     Emptiness : Boolean;
  290. Begin
  291.     Writeln('В заданном тексте каждое нечетное слово заключить в круглые скобки, а каждое четное слово заключить в квадратные скобки');
  292.     Writeln('Выберите, откуда будет производиться ввод:');
  293.     Writeln('1.Консоль');
  294.     Writeln('2.Файл');
  295.     Choose := ChooseInput();
  296.     Path := InputPathInput(Choose);
  297.     Choose := CheckFile(Choose,Path);
  298.     Str := InputString(Choose, Path);
  299.     Str := CheckExcessSpace(Str);
  300.     Emptiness := CheckEmptiness(Str);
  301.     Str := ChangeString(Str, Emptiness);
  302.     Writeln('Выберите, куда будет производиться вывод:');
  303.     Writeln('1.Консоль');
  304.     Writeln('2.Файл');
  305.     Choose := ChooseInput();
  306.     Output(Path,Choose,Emptiness,Str);
  307.     Readln;
  308. End;
  309.  
  310. Begin
  311.     Main;
  312. End.
Advertisement
Add Comment
Please, Sign In to add comment