Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Project1;
- Uses
- System.SysUtils;
- Type
- TChar = Array[0..5] of Char;
- TArray = Array[0..5] of Integer;
- Const
- Arr : TChar = ('[', ']', '{', '}', '(', ')');
- SQUARE_BRACKET = 0;
- BRACE = 2;
- BRACKET = 4;
- Var
- Counters : TArray;
- Function Choose() : Boolean;
- Var
- InputNumber : Integer;
- IsCorrect : Boolean;
- Const
- MIN_NUM = 0;
- MAX_NUM = 1;
- Begin
- InputNumber := 0;
- Repeat
- IsCorrect := True;
- Try
- Readln(InputNumber)
- Except
- Writeln('Please, enter a integer number:');
- IsCorrect := False;
- End;
- If ((InputNumber < MIN_NUM) or (InputNumber > MAX_NUM)) then
- Begin
- Writeln('You are out of input range!');
- IsCorrect := False;
- End;
- Until IsCorrect;
- If (InputNumber = 1) then
- Choose := True
- Else
- Choose := False;
- End;
- Function ConsoleInputString(): String;
- Var
- IsCorrect: Boolean;
- StrInput: String;
- Begin
- Repeat
- IsCorrect := True;
- Try
- Readln(StrInput);
- Except
- Writeln('Check input data.');
- IsCorrect := False;
- End;
- Until (IsCorrect);
- ConsoleInputString := StrInput;
- End;
- Procedure CountBrackets (J, I : Integer; Brakes : TChar; Counters: TArray; Str : String);
- Begin
- If Str[I] = Brakes[J] then
- Inc (Counters[J]);
- If Str[I] = Brakes[J + 1] then
- Inc (Counters[J + 1]);
- End;
- Procedure Loop (Brakes : TChar; Counters: TArray; Str : String);
- Var
- I : Integer;
- Begin
- For I := Low(Str) to High(Str) do
- Begin
- CountBrackets(SQUARE_BRACKET, I, Brakes, Counters, Str);
- If (Counters[SQUARE_BRACKET + 1] > counters[SQUARE_BRACKET]) then
- Break;
- CountBrackets(BRACE, I, Brakes, Counters, Str);
- If (counters[BRACE + 1] > counters[BRACE]) then
- Break;
- CountBrackets(BRACKET, I, Brakes, Counters, Str);
- If (counters[BRACKET + 1] > counters[BRACKET]) then
- Break;
- End;
- End;
- Procedure Check (Counters : TArray);
- Begin
- If ((counters[SQUARE_BRACKET] = counters[SQUARE_BRACKET + 1]) and (counters[BRACE] = counters[BRACE + 1]) and (counters[BRACKET] = counters[BRACKET + 1])) then
- Writeln('Congratulations! Balance is exist!')
- Else
- Writeln('There is no balance there.');
- End;
- Function InputFilePath() : String;
- Var
- IsCorrect : Boolean;
- Path : String;
- Begin
- Writeln('Input path to file: ');
- Repeat
- IsCorrect := True;
- Readln(Path);
- If(Not FileExists(Path)) Then
- Begin
- IsCorrect := False;
- Writeln('Wrong way to file. Input correct path.');
- End
- Else If (ExtractFileExt(Path) <> '.txt') Then
- Begin
- Writeln('Must have .txt');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputFilePath := Path;
- End;
- Function InputStringFromFile(Path : String) : String;
- Var
- StrInput : String;
- IsCorrect : Boolean;
- InputFile : TextFile;
- Begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Repeat
- IsCorrect := True;
- Try
- Readln(InputFile, StrInput);
- Except
- IsCorrect := False;
- Writeln('Mistake of reading string from file.');
- End;
- Until IsCorrect;
- CloseFile(InputFile);
- InputStringFromFile := StrInput;
- End;
- Procedure FileOutput(Path : String; Counters : TArray);
- Var
- IsCorrect : Boolean;
- OutputFile : TextFile;
- I: Integer;
- Begin
- AssignFile(OutputFile, Path);
- Repeat
- IsCorrect := True;
- Try
- Rewrite(OutputFile);
- Except
- IsCorrect := False;
- Writeln('Mistake with writing in file. Input another path.');
- Path := InputFilePath();
- End;
- Until IsCorrect;
- If ((counters[SQUARE_BRACKET] = counters[SQUARE_BRACKET + 1]) and (counters[BRACE] = counters[BRACE + 1]) and (counters[BRACKET] = counters[BRACKET + 1])) then
- Writeln(OutputFile, 'Congratulations! Balance is exist!')
- Else
- Writeln(OutputFile, 'There is no balance there.');
- CloseFile(OutputFile);
- Write('Success!');
- End;
- Var
- Chose : Boolean;
- StrInput, Path : String;
- Begin
- Writeln ('Check if the given text has a balance of opening and closing brackets.');
- Writeln ('Enter type of input.');
- Writeln ('1 is console input, 0 is file input.');
- Chose := Choose();
- If (Chose) then
- Begin
- Writeln('Input string:');
- StrInput := ConsoleInputString();
- End
- Else
- Begin
- Path := InputFilePath();
- StrInput := InputStringFromFile(Path);
- End;
- Loop(Arr, Counters, StrInput);
- Writeln ('Enter type of output.');
- Writeln ('1 is console output, 0 is file output.');
- Chose := Choose();
- If (Chose) then
- Check(Counters)
- Else
- Begin
- Path := InputFilePath();
- FileOutput(Path, Counters);
- End;
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement