Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.59 KB | None | 0 0
  1. program lab2_5;
  2.  
  3. uses
  4.     System.SysUtils;
  5.  
  6. Type
  7.     Stack = array[1..100] of Char;
  8.  
  9. function GetOpen(const aCh: Char): Char;
  10. begin
  11.     case aCh of
  12.         ')': GetOpen := '(';
  13.         ']': GetOpen := '[';
  14.         '}': GetOpen := '{';
  15.     else
  16.         GetOpen := '*';
  17.     end;
  18. end;
  19.  
  20. function Proc(var StackPnt: Integer; StrSrc: String): Boolean;
  21. var
  22.     i: Integer;
  23.     Ch: Char;
  24.     Balance: Boolean;
  25.     MassStack: Stack;
  26. const
  27.     D1 = ['(', '[', '{'];
  28.     D2 = [')', ']', '}'];
  29. begin
  30.     StackPnt := 1;
  31.     i := 1;
  32.     Balance := True;
  33.     while (i <= length(StrSrc)) and Balance do
  34.     begin
  35.         if StrSrc[i] in D1 then
  36.         begin
  37.             MassStack[StackPnt] := StrSrc[i];
  38.             inc(StackPnt);
  39.         end
  40.         else
  41.             if StrSrc[i] in D2 then
  42.             begin;
  43.                 dec(StackPnt);
  44.                 Ch := MassStack[StackPnt];
  45.                 if Ch <> GetOpen(StrSrc[i]) then
  46.                     Balance := False;
  47.             end;
  48.         inc(i);
  49.     end;
  50.     Proc := (StackPnt = 1);
  51. end;
  52.  
  53. function isStrCorrect(StrSrc: String): Boolean;
  54. var
  55.     i: integer;
  56.     isCorrect: boolean;
  57. const
  58.     D1 = ['(', '[', '{'];
  59.     D2 = [')', ']', '}'];
  60. begin
  61.     isCorrect := false;
  62.     for i := 1 to length(StrSrc) do
  63.         if StrSrc[i] in D1 then
  64.             isCorrect := true
  65.         else
  66.             if StrSrc[i] in D2 then
  67.                 isCorrect := true;
  68.     isStrCorrect := isCorrect;
  69. end;
  70.  
  71. function FileNameInputRead(): string;
  72. var
  73.     isCorrect: boolean;
  74.     FileName: string;
  75. begin
  76.     isCorrect := false;
  77.     repeat
  78.         writeln('Enter file name:');
  79.         readln(FileName);
  80.         if FileExists(FileName) then
  81.             isCorrect := true
  82.         else
  83.             writeln('This file does not exist!');
  84.     until isCorrect;
  85.     FileNameInputRead := FileName;
  86. end;
  87.  
  88. procedure IsBracketCorrect();
  89. var
  90.     FileIn: TextFile;
  91.     FileName, StrSrc: string;
  92.     StackPnt: Integer;
  93. begin
  94.     FileName := FileNameInputRead();
  95.     Assign(FileIn, Filename);
  96.     Reset(FileIn);
  97.     while not eof(FileIn) do
  98.     begin
  99.         readln(FileIn, StrSrc);
  100.         writeln(StrSrc);
  101.         if isStrCorrect(StrSrc) then
  102.             if Proc(StackPnt, StrSrc) then
  103.                 Writeln('Brackets agreed.')
  104.             else
  105.                 Writeln('Brackets are not agreed.')
  106.         else
  107.             Writeln('There are no brackets in the line!');
  108.     end;
  109.     close(FileIn);
  110. end;
  111.  
  112. begin
  113.     writeln('This program will check the balance of the brackets in the lines');
  114.     IsBracketCorrect();
  115.     readln;
  116. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement