Advertisement
Glebyshka

F

Apr 2nd, 2021
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.64 KB | None | 0 0
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.     System.SysUtils;
  8.  
  9. var
  10.     Str: String;
  11.     OpenRound, OpenSquare, CloseRound, CloseSquare: Char;
  12.     i: integer;
  13.     NumOR, NumOS, NumCR, NumCS: integer;
  14.     CountRound, CountSquare: integer;
  15.  
  16. label
  17.     Execute, Skip, Again;
  18.  
  19. Function Rec(Stroka:string):string;
  20. Var
  21.     A,B:string;
  22.     posA,posB:integer;
  23. Label
  24.     FinishRec;
  25. begin
  26.     for i := 1 to length(Stroka) do
  27.     begin
  28.         if (Stroka[i] = '[') or (Stroka[i] = '(') then
  29.         begin
  30.             A:=Stroka[i];
  31.             posA:=i;
  32.         end;
  33.         if (Stroka[i] = ']') or (Stroka[i] = ')') then
  34.         begin
  35.             B:=Stroka[i];
  36.             posB:=i;
  37.             if (A = '[') and (B = ']') then
  38.             begin
  39.                 Delete(Stroka,posA,1);
  40.                 Delete(Stroka,posB-1,1);
  41.                 A:='';
  42.                 B:='';
  43.                 if Stroka <> '' then Rec(Stroka);
  44.                 goto FinishRec;
  45.             end;
  46.             if (A = '(') and (B = ')') then
  47.             begin
  48.                 Delete(Stroka,posA,1);
  49.                 Delete(Stroka,posB-1,1);
  50.                 A:='';
  51.                 B:='';
  52.                 if Stroka <> '' then Rec(Stroka);
  53.                 goto FinishRec;
  54.             end;
  55.         end;
  56.     end;
  57.     FinishRec:
  58.     if (Stroka[1] = '(') or  (Stroka[1] = '(') or (Stroka[1] = '(') or (Stroka[1] = '(') then Rec:='The placement is wrong.'
  59.     else Rec:='The placement is right.';
  60. end;
  61.  
  62. {Function Rec(Stroka: string): string;
  63. label
  64.     Finish;
  65. begin
  66.     i := 1;
  67.     while i <= Length(Stroka) - 1 do
  68.     begin
  69.         if Stroka[i] = OpenRound then
  70.         begin
  71.             if Stroka[i + 1] = CloseSquare then
  72.             begin
  73.                 Rec := 'The placement is wrong.';
  74.                 goto Finish;
  75.             end;
  76.             if Stroka[i + 1] = CloseRound then
  77.             begin
  78.                 Delete(Stroka, i, 1);
  79.                 Delete(Stroka, i, 1);
  80.                 Rec(Stroka);
  81.             end;
  82.         end;
  83.         if Stroka[i] = OpenSquare then
  84.         begin
  85.             if Stroka[i + 1] = CloseRound then
  86.             begin
  87.                 Rec := 'The placement is wrong.';
  88.                 goto Finish;
  89.             end;
  90.             if Stroka[i + 1] = CloseSquare then
  91.             begin
  92.                 Delete(Stroka, i, 1);
  93.                 Delete(Stroka, i, 1);
  94.                 Rec(Stroka);
  95.             end;
  96.         end;
  97.         inc(i);
  98.     end;
  99.  
  100.     Rec := 'The placement is right.';
  101. Finish:
  102. end;
  103.     }
  104. begin
  105.  
  106.     OpenRound := '(';
  107.     CloseRound := ')';
  108.     OpenSquare := '[';
  109.     CloseSquare := ']';
  110.  
  111.     Writeln('Enter the string:');
  112.     Readln(Str);
  113.  
  114.     NumOR := 0;
  115.     NumOS := 0;
  116.     NumCR := 0;
  117.     NumCS := 0;
  118.  
  119.     i := 1;
  120.     While i <= Length(Str) do
  121.     begin
  122.         if Str[i] = OpenRound then
  123.             inc(NumOR);
  124.         if Str[i] = CloseRound then
  125.             inc(NumCR);
  126.         if Str[i] = OpenSquare then
  127.             inc(NumOS);
  128.         if Str[i] = CloseSquare then
  129.             inc(NumCS);
  130.         if (Str[i] <> OpenRound) and (Str[i] <> CloseRound) and
  131.           (Str[i] <> OpenSquare) and (Str[i] <> CloseSquare) then
  132.             Delete(Str, i, 1)
  133.         else
  134.             inc(i);
  135.     end;
  136.    // Writeln(Str);
  137.  
  138.     if (NumOR <> NumCR) or (NumOS <> NumCS) or (Length(Str) mod 2 <> 0) then
  139.     begin
  140.         Writeln('The placement is wrong. Number of brackets does not match.');
  141.         goto Execute;
  142.     end;
  143.  
  144.     CountRound := 0;
  145.     CountSquare := 0;
  146.  
  147.     for i := 1 to Length(Str) do
  148.     begin
  149.         if Str[i] = OpenRound then inc(CountRound);
  150.         if Str[i] = CloseRound then dec(CountRound);
  151.         if Str[i] = OpenSquare then inc(CountSquare);
  152.         if Str[i] = CloseSquare then dec(CountSquare);
  153.         if (CountRound < 0) or (CountSquare < 0) then
  154.         begin
  155.             Writeln('The placement is wrong.');
  156.             goto Execute;
  157.         end;
  158.     end;
  159.  
  160.     { g:=0;
  161.       k:=1;
  162.       Position:=Round(Length(Str)/2);
  163.       for i := 1 to NumOR do
  164.       begin
  165.       if ((Str[Position - g] = OpenRound) and (Str[Position + k] = CloseRound)) or ((Str[Position - g] = OpenSquare) and (Str[Position + k] = CloseSquare)) then
  166.       begin
  167.       inc(g);
  168.       inc(k);
  169.       end
  170.       else
  171.       begin
  172.       Writeln('The placement is wrong.');
  173.       goto Execute;
  174.       end;
  175.       end;
  176.  
  177.       Writeln('The placement is right.'); }
  178.  
  179.    // Writeln('Результат рекурсивной функции: ', Rec(Str));
  180.  
  181. Writeln('Результат рекурсивной процедуры:', Rec(Str));
  182.  
  183. Again:
  184.     i := 1;
  185.     while i <= Length(Str) - 1 do
  186.     begin
  187.         if Str[i] = OpenRound then
  188.         begin
  189.             if Str[i + 1] = CloseSquare then
  190.             begin
  191.                 Writeln('The placement is wrong.');
  192.                 goto Execute;
  193.             end;
  194.             if Str[i + 1] = CloseRound then
  195.             begin
  196.                 Delete(Str, i, 1);
  197.                 Delete(Str, i, 1);
  198.                 goto Again;
  199.             end;
  200.         end;
  201.         if Str[i] = OpenSquare then
  202.         begin
  203.             if Str[i + 1] = CloseRound then
  204.             begin
  205.                 Writeln('The placement is wrong.');
  206.                 goto Execute;
  207.             end;
  208.             if Str[i + 1] = CloseSquare then
  209.             begin
  210.                 Delete(Str, i, 1);
  211.                 Delete(Str, i, 1);
  212.                 goto Again;
  213.             end;
  214.         end;
  215.         inc(i);
  216.     end;
  217.  
  218.     Writeln('The placement is right.');
  219.  
  220. Execute:
  221.     Writeln('Program is over! Press ENTER to leave.');
  222.     Readln;
  223.  
  224. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement