Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Проверить, имеется ли в заданном тексте баланс открывающих и закрывающих скобок, имея в виду,
- //что балансом, например, будет комбинация (…), в то время как комбинация )..(..)..( балансом не является.
- program lab_9;
- uses
- crt;
- function CheckString (InputString: String): boolean;
- var
- count: integer;
- currentPosition: integer;
- openBracket, closeBracket: integer;
- begin
- openBracket:= 0;
- closeBracket:= 0;
- count:= 0;
- currentPosition:= 1;
- while (count >= 0) and (currentPosition <= length (InputString)) do
- begin
- if InputString[currentPosition]= '(' then
- begin
- inc (openBracket);
- inc (count);
- end;
- if InputString[currentPosition]= ')' then
- begin
- inc (closeBracket);
- dec (count);
- end;
- inc (CurrentPosition);
- end;
- if (openBracket = 0) then
- writeln ('In the text there is no opening brackets')
- else
- if (closeBracket = 0) then
- writeln ('In the text there is no closing brackets');
- if (count <> 0) then
- CheckString:= false
- else
- CheckString:= true;
- end;
- function InText: string;
- var
- Fil: text;
- FString: string;
- adds: string;
- begin
- writeln ('Enter the name file:');
- readln (adds);
- while not FileExists (adds) do
- begin
- writeln ('False name. Try again:');
- readln (adds);
- end;
- assign (Fil, adds);
- reset (Fil);
- while not Eof (Fil) do
- begin
- readln (Fil, Fstring);
- writeln (Fstring);
- end;
- close (Fil);
- Intext:= Fstring;
- end;
- var
- InString: String;
- choise: byte;
- begin
- repeat
- writeln ('Input method: 1 - keyboard | 2 - text file');
- readln (choise);
- case choise of
- 1:
- begin
- writeln ('Enter text string:');
- readln (InString);
- end;
- 2: InString:= InText;
- end;
- if CheckString (InString) then
- writeln ('There is a balance brackets')
- else
- writeln ('There is no balance brackets');
- writeln ('To exit the program, press Esc, to repeat - any other key');
- until (readKey = #27);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement