Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- var
- Str: String;
- OpenRound, OpenSquare, CloseRound, CloseSquare: Char;
- i: integer;
- NumOR, NumOS, NumCR, NumCS: integer;
- CountRound, CountSquare: integer;
- label
- Execute, Skip, Again;
- Function Rec(Stroka:string):string;
- Var
- A,B:string;
- posA,posB:integer;
- Label
- FinishRec;
- begin
- for i := 1 to length(Stroka) do
- begin
- if (Stroka[i] = '[') or (Stroka[i] = '(') then
- begin
- A:=Stroka[i];
- posA:=i;
- end;
- if (Stroka[i] = ']') or (Stroka[i] = ')') then
- begin
- B:=Stroka[i];
- posB:=i;
- if (A = '[') and (B = ']') then
- begin
- Delete(Stroka,posA,1);
- Delete(Stroka,posB-1,1);
- A:='';
- B:='';
- if Stroka <> '' then Rec(Stroka);
- goto FinishRec;
- end;
- if (A = '(') and (B = ')') then
- begin
- Delete(Stroka,posA,1);
- Delete(Stroka,posB-1,1);
- A:='';
- B:='';
- if Stroka <> '' then Rec(Stroka);
- goto FinishRec;
- end;
- end;
- end;
- FinishRec:
- if (Stroka[1] = '(') or (Stroka[1] = '(') or (Stroka[1] = '(') or (Stroka[1] = '(') then Rec:='The placement is wrong.'
- else Rec:='The placement is right.';
- end;
- {Function Rec(Stroka: string): string;
- label
- Finish;
- begin
- i := 1;
- while i <= Length(Stroka) - 1 do
- begin
- if Stroka[i] = OpenRound then
- begin
- if Stroka[i + 1] = CloseSquare then
- begin
- Rec := 'The placement is wrong.';
- goto Finish;
- end;
- if Stroka[i + 1] = CloseRound then
- begin
- Delete(Stroka, i, 1);
- Delete(Stroka, i, 1);
- Rec(Stroka);
- end;
- end;
- if Stroka[i] = OpenSquare then
- begin
- if Stroka[i + 1] = CloseRound then
- begin
- Rec := 'The placement is wrong.';
- goto Finish;
- end;
- if Stroka[i + 1] = CloseSquare then
- begin
- Delete(Stroka, i, 1);
- Delete(Stroka, i, 1);
- Rec(Stroka);
- end;
- end;
- inc(i);
- end;
- Rec := 'The placement is right.';
- Finish:
- end;
- }
- begin
- OpenRound := '(';
- CloseRound := ')';
- OpenSquare := '[';
- CloseSquare := ']';
- Writeln('Enter the string:');
- Readln(Str);
- NumOR := 0;
- NumOS := 0;
- NumCR := 0;
- NumCS := 0;
- i := 1;
- While i <= Length(Str) do
- begin
- if Str[i] = OpenRound then
- inc(NumOR);
- if Str[i] = CloseRound then
- inc(NumCR);
- if Str[i] = OpenSquare then
- inc(NumOS);
- if Str[i] = CloseSquare then
- inc(NumCS);
- if (Str[i] <> OpenRound) and (Str[i] <> CloseRound) and
- (Str[i] <> OpenSquare) and (Str[i] <> CloseSquare) then
- Delete(Str, i, 1)
- else
- inc(i);
- end;
- // Writeln(Str);
- if (NumOR <> NumCR) or (NumOS <> NumCS) or (Length(Str) mod 2 <> 0) then
- begin
- Writeln('The placement is wrong. Number of brackets does not match.');
- goto Execute;
- end;
- CountRound := 0;
- CountSquare := 0;
- for i := 1 to Length(Str) do
- begin
- if Str[i] = OpenRound then inc(CountRound);
- if Str[i] = CloseRound then dec(CountRound);
- if Str[i] = OpenSquare then inc(CountSquare);
- if Str[i] = CloseSquare then dec(CountSquare);
- if (CountRound < 0) or (CountSquare < 0) then
- begin
- Writeln('The placement is wrong.');
- goto Execute;
- end;
- end;
- { g:=0;
- k:=1;
- Position:=Round(Length(Str)/2);
- for i := 1 to NumOR do
- begin
- if ((Str[Position - g] = OpenRound) and (Str[Position + k] = CloseRound)) or ((Str[Position - g] = OpenSquare) and (Str[Position + k] = CloseSquare)) then
- begin
- inc(g);
- inc(k);
- end
- else
- begin
- Writeln('The placement is wrong.');
- goto Execute;
- end;
- end;
- Writeln('The placement is right.'); }
- // Writeln('Результат рекурсивной функции: ', Rec(Str));
- Writeln('Результат рекурсивной процедуры:', Rec(Str));
- Again:
- i := 1;
- while i <= Length(Str) - 1 do
- begin
- if Str[i] = OpenRound then
- begin
- if Str[i + 1] = CloseSquare then
- begin
- Writeln('The placement is wrong.');
- goto Execute;
- end;
- if Str[i + 1] = CloseRound then
- begin
- Delete(Str, i, 1);
- Delete(Str, i, 1);
- goto Again;
- end;
- end;
- if Str[i] = OpenSquare then
- begin
- if Str[i + 1] = CloseRound then
- begin
- Writeln('The placement is wrong.');
- goto Execute;
- end;
- if Str[i + 1] = CloseSquare then
- begin
- Delete(Str, i, 1);
- Delete(Str, i, 1);
- goto Again;
- end;
- end;
- inc(i);
- end;
- Writeln('The placement is right.');
- Execute:
- Writeln('Program is over! Press ENTER to leave.');
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement