Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lab2_5;
- uses
- System.SysUtils;
- Type
- Stack = array[1..100] of Char;
- function GetOpen(const aCh: Char): Char;
- begin
- case aCh of
- ')': GetOpen := '(';
- ']': GetOpen := '[';
- '}': GetOpen := '{';
- else
- GetOpen := '*';
- end;
- end;
- function Proc(var StackPnt: Integer; StrSrc: String): Boolean;
- var
- i: Integer;
- Ch: Char;
- Balance: Boolean;
- MassStack: Stack;
- const
- D1 = ['(', '[', '{'];
- D2 = [')', ']', '}'];
- begin
- StackPnt := 1;
- i := 1;
- Balance := True;
- while (i <= length(StrSrc)) and Balance do
- begin
- if StrSrc[i] in D1 then
- begin
- MassStack[StackPnt] := StrSrc[i];
- inc(StackPnt);
- end
- else
- if StrSrc[i] in D2 then
- begin;
- dec(StackPnt);
- Ch := MassStack[StackPnt];
- if Ch <> GetOpen(StrSrc[i]) then
- Balance := False;
- end;
- inc(i);
- end;
- Proc := (StackPnt = 1);
- end;
- function isStrCorrect(StrSrc: String): Boolean;
- var
- i: integer;
- isCorrect: boolean;
- const
- D1 = ['(', '[', '{'];
- D2 = [')', ']', '}'];
- begin
- isCorrect := false;
- for i := 1 to length(StrSrc) do
- if StrSrc[i] in D1 then
- isCorrect := true
- else
- if StrSrc[i] in D2 then
- isCorrect := true;
- isStrCorrect := isCorrect;
- end;
- function FileNameInputRead(): string;
- var
- isCorrect: boolean;
- FileName: string;
- begin
- isCorrect := false;
- repeat
- writeln('Enter file name:');
- readln(FileName);
- if FileExists(FileName) then
- isCorrect := true
- else
- writeln('This file does not exist!');
- until isCorrect;
- FileNameInputRead := FileName;
- end;
- procedure IsBracketCorrect();
- var
- FileIn: TextFile;
- FileName, StrSrc: string;
- StackPnt: Integer;
- begin
- FileName := FileNameInputRead();
- Assign(FileIn, Filename);
- Reset(FileIn);
- while not eof(FileIn) do
- begin
- readln(FileIn, StrSrc);
- writeln(StrSrc);
- if isStrCorrect(StrSrc) then
- if Proc(StackPnt, StrSrc) then
- Writeln('Brackets agreed.')
- else
- Writeln('Brackets are not agreed.')
- else
- Writeln('There are no brackets in the line!');
- end;
- close(FileIn);
- end;
- begin
- writeln('This program will check the balance of the brackets in the lines');
- IsBracketCorrect();
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement