Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program lab3_4;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- Function InputFileLocation(): string;
- Var
- IsCorrect: Boolean;
- Location: String;
- Begin
- Repeat
- IsCorrect := false;
- WriteLn('Enter file location:');
- ReadLn(Location);
- If FileExists(Location) then
- IsCorrect := true
- Else
- Begin
- WriteLn('Please enter the correct location');
- WriteLn('');
- End;
- Until IsCorrect;
- InputFileLocation := Location;
- End;
- Function ChooseInput(): integer;
- Var
- Line: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := true;
- WriteLn('Do you want to input from file? (y/n)');
- ReadLn(line);
- Line := Line.ToLower();
- If(Line <> '') and (Line <> 'y') and (Line <> 'n') then
- Begin
- IsCorrect := false;
- WriteLn('Enter valid answer');
- End;
- Until IsCorrect;
- If (Line = '') or (Line = 'y') then
- ChooseInput := 0
- Else
- ChooseInput := 1;
- End;
- Procedure OutputToFile(Flag: Integer);
- var
- TFile: TextFile;
- begin
- AssignFile(TFile, InputFileLocation());
- Rewrite(TFile);
- if Flag = 0 then
- writeln(TFile, 'Баланск скобок соблюдён')
- Else
- writeln(TFile, 'Баланск скобок нарушен');
- CloseFile(TFile);
- end;
- Function FindBalance(Str: String): Integer;
- Var
- I, Flag: Integer;
- Begin
- I := 1;
- Flag := 0;
- while ((Flag >= 0) And (I <= High(Str))) do
- begin
- if Str[I] = '(' then flag := flag + 1;
- if Str[I] = ')' then flag := flag - 1;
- i:=i+1
- end;
- FindBalance := Flag;
- End;
- Procedure PrintToConsole(Flag: Integer);
- Begin
- if Flag = 0 then
- writeln('Баланск скобок соблюдён')
- Else
- writeln('Баланск скобок нарушен');
- End;
- Function GetLineFromConsole(): String;
- var
- IsCorrect: Boolean;
- Str: String;
- begin
- repeat
- IsCorrect := true;
- ReadLn(Str);
- if Length(Str) = 0 then
- begin
- WriteLn('The line is empty, please, try to enter it one more time');
- IsCorrect := false;
- end;
- until IsCorrect;
- GetLineFromConsole := Str;
- end;
- Function GetLineFromFile(): String;
- var
- IsCorrect: Boolean;
- TFile: TextFile;
- Str: String;
- begin
- repeat
- Str := '';
- IsCorrect := true;
- AssignFile(TFile, InputFileLocation());
- Reset(TFile);
- ReadLn(TFile, Str);
- CloseFile(TFile);
- if (Length(Str) = 0) then
- begin
- WriteLn('String is empty. Enter another file location');
- IsCorrect := false;
- end;
- until IsCorrect;
- GetLineFromFile := Str;
- end;
- Procedure Main();
- Var
- ChosenInput: Integer;
- Str: String;
- I, Flag : Integer;
- Begin
- Str := '';
- WriteLn('Проверить, имеется ли в заданном тексте баланс открывающих и закрывающих скобок.');
- ChosenInput := ChooseInput();
- If (ChosenInput = 0) then
- Str := GetLineFromFile()
- Else
- Begin
- writeln('Введите строку');
- Str := GetLineFromConsole();
- End;
- writeln('Результат:');
- Flag := FindBalance(Str);
- PrintToConsole(Flag);
- WriteLn;
- OutputToFile(Flag);
- Readln;
- End;
- Begin
- Main();
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement