Advertisement
Vladislav8653

laba_3_4_delphi*

Dec 9th, 2022
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.14 KB | None | 0 0
  1. Program Project1;
  2. Uses
  3.     System.SysUtils;
  4. Type
  5.     TChar = Array[0..5] of Char;
  6.     TArray = Array[0..5] of Integer;
  7. Const
  8.     Arr : TChar = ('[', ']', '{', '}', '(', ')');
  9.     SQUARE_BRACKET = 0;
  10.     BRACE = 2;
  11.     BRACKET = 4;
  12. Var
  13.     Counters : TArray;
  14.  
  15. Function Choose() : Boolean;
  16. Var
  17.     InputNumber : Integer;
  18.     IsCorrect : Boolean;
  19. Const
  20.     MIN_NUM = 0;
  21.     MAX_NUM = 1;
  22. Begin
  23.     InputNumber := 0;
  24.     Repeat
  25.         IsCorrect := True;
  26.         Try
  27.             Readln(InputNumber)
  28.         Except
  29.             Writeln('Please, enter a integer number:');
  30.             IsCorrect := False;
  31.         End;
  32.         If ((InputNumber < MIN_NUM) or (InputNumber > MAX_NUM)) then
  33.         Begin
  34.             Writeln('You are out of input range!');
  35.             IsCorrect := False;
  36.         End;
  37.  
  38.     Until IsCorrect;
  39.     If (InputNumber = 1) then
  40.         Choose := True
  41.     Else
  42.         Choose := False;
  43. End;
  44.  
  45. Function ConsoleInputString(): String;
  46. Var
  47.     IsCorrect: Boolean;
  48.     StrInput: String;
  49. Begin
  50.     Repeat
  51.         IsCorrect := True;
  52.         Try
  53.            Readln(StrInput);
  54.         Except
  55.            Writeln('Check input data.');
  56.            IsCorrect := False;
  57.         End;
  58.     Until (IsCorrect);
  59.     ConsoleInputString := StrInput;
  60. End;
  61.  
  62. Procedure CountBrackets (J, I : Integer; Brakes : TChar; Counters: TArray; Str : String);
  63. Begin
  64.     If Str[I] = Brakes[J] then
  65.         Inc (Counters[J]);
  66.     If Str[I] = Brakes[J + 1] then
  67.         Inc (Counters[J + 1]);
  68. End;
  69.  
  70. Procedure Loop (Brakes : TChar; Counters: TArray; Str : String);
  71. Var
  72.     I : Integer;
  73. Begin
  74.      For I := Low(Str) to High(Str) do
  75.      Begin
  76.         CountBrackets(SQUARE_BRACKET, I, Brakes, Counters, Str);
  77.         If (Counters[SQUARE_BRACKET + 1] > counters[SQUARE_BRACKET]) then
  78.             Break;
  79.         CountBrackets(BRACE, I, Brakes, Counters, Str);
  80.         If (counters[BRACE + 1] > counters[BRACE]) then
  81.             Break;
  82.         CountBrackets(BRACKET, I, Brakes, Counters, Str);
  83.         If (counters[BRACKET + 1] > counters[BRACKET]) then
  84.             Break;
  85.      End;
  86. End;
  87.  
  88. Procedure Check (Counters : TArray);
  89. Begin
  90.     If ((counters[SQUARE_BRACKET] = counters[SQUARE_BRACKET + 1]) and (counters[BRACE] = counters[BRACE + 1]) and (counters[BRACKET] = counters[BRACKET + 1])) then
  91.         Writeln('Congratulations! Balance is exist!')
  92.     Else
  93.         Writeln('There is no balance there.');
  94. End;
  95.  
  96. Function InputFilePath() : String;
  97. Var
  98.     IsCorrect : Boolean;
  99.     Path : String;
  100. Begin
  101.     Writeln('Input path to file: ');
  102.     Repeat
  103.         IsCorrect := True;
  104.         Readln(Path);
  105.         If(Not FileExists(Path)) Then
  106.         Begin
  107.             IsCorrect := False;
  108.             Writeln('Wrong way to file. Input correct path.');
  109.         End
  110.         Else If (ExtractFileExt(Path) <> '.txt') Then
  111.         Begin
  112.             Writeln('Must have .txt');
  113.             IsCorrect := False;
  114.         End;
  115.     Until IsCorrect;
  116.     InputFilePath := Path;
  117. End;
  118.  
  119. Function InputStringFromFile(Path : String) : String;
  120. Var
  121.     StrInput : String;
  122.     IsCorrect : Boolean;
  123.     InputFile : TextFile;
  124. Begin
  125.     AssignFile(InputFile, Path);
  126.     Reset(InputFile);
  127.     Repeat
  128.         IsCorrect := True;
  129.         Try
  130.             Readln(InputFile, StrInput);
  131.         Except
  132.             IsCorrect := False;
  133.             Writeln('Mistake of reading string from file.');
  134.         End;
  135.     Until IsCorrect;
  136.     CloseFile(InputFile);
  137.     InputStringFromFile := StrInput;
  138. End;
  139.  
  140. Procedure FileOutput(Path : String; Counters : TArray);
  141. Var
  142.     IsCorrect : Boolean;
  143.     OutputFile : TextFile;
  144.     I: Integer;
  145. Begin
  146.     AssignFile(OutputFile, Path);
  147.     Repeat
  148.         IsCorrect := True;
  149.         Try
  150.             Rewrite(OutputFile);
  151.         Except
  152.             IsCorrect := False;
  153.             Writeln('Mistake with writing in file. Input another path.');
  154.             Path := InputFilePath();
  155.         End;
  156.     Until IsCorrect;
  157.     If ((counters[SQUARE_BRACKET] = counters[SQUARE_BRACKET + 1]) and (counters[BRACE] = counters[BRACE + 1]) and (counters[BRACKET] = counters[BRACKET + 1])) then
  158.         Writeln(OutputFile, 'Congratulations! Balance is exist!')
  159.     Else
  160.         Writeln(OutputFile, 'There is no balance there.');
  161.     CloseFile(OutputFile);
  162.     Write('Success!');
  163. End;
  164.  
  165. Var
  166.     Chose : Boolean;
  167.     StrInput, Path : String;
  168. Begin
  169.     Writeln ('Check if the given text has a balance of opening and closing brackets.');
  170.     Writeln ('Enter type of input.');
  171.     Writeln ('1 is console input, 0 is file input.');
  172.     Chose := Choose();
  173.     If (Chose) then
  174.         Begin
  175.             Writeln('Input string:');
  176.             StrInput := ConsoleInputString();
  177.         End
  178.     Else
  179.         Begin
  180.             Path := InputFilePath();
  181.             StrInput := InputStringFromFile(Path);
  182.         End;
  183.  
  184.     Loop(Arr, Counters, StrInput);
  185.  
  186.     Writeln ('Enter type of output.');
  187.     Writeln ('1 is console output, 0 is file output.');
  188.     Chose := Choose();
  189.     If (Chose) then
  190.         Check(Counters)
  191.     Else
  192.         Begin
  193.             Path := InputFilePath();
  194.             FileOutput(Path, Counters);
  195.         End;
  196.     Readln;
  197. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement