Advertisement
Guest User

Untitled

a guest
Dec 26th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. //Проверить, имеется ли в заданном тексте баланс открывающих и закрывающих скобок, имея в виду,
  2. //что балансом, например, будет комбинация (…), в то время как комбинация )..(..)..( балансом не является.
  3.  
  4. program lab_9;
  5. uses
  6. crt;
  7.  
  8. function CheckString (InputString: String): boolean;
  9. var
  10. count: integer;
  11. currentPosition: integer;
  12. openBracket, closeBracket: integer;
  13. begin
  14. openBracket:= 0;
  15. closeBracket:= 0;
  16. count:= 0;
  17. currentPosition:= 1;
  18. while (count >= 0) and (currentPosition <= length (InputString)) do
  19. begin
  20. if InputString[currentPosition]= '(' then
  21. begin
  22. inc (openBracket);
  23. inc (count);
  24. end;
  25. if InputString[currentPosition]= ')' then
  26. begin
  27. inc (closeBracket);
  28. dec (count);
  29. end;
  30. inc (CurrentPosition);
  31. end;
  32. if (openBracket = 0) then
  33. writeln ('In the text there is no opening brackets')
  34. else
  35. if (closeBracket = 0) then
  36. writeln ('In the text there is no closing brackets');
  37.  
  38. if (count <> 0) then
  39. CheckString:= false
  40. else
  41. CheckString:= true;
  42. end;
  43.  
  44. function InText: string;
  45. var
  46. Fil: text;
  47. FString: string;
  48. adds: string;
  49. begin
  50. writeln ('Enter the name file:');
  51. readln (adds);
  52. while not FileExists (adds) do
  53. begin
  54. writeln ('False name. Try again:');
  55. readln (adds);
  56. end;
  57. assign (Fil, adds);
  58. reset (Fil);
  59. while not Eof (Fil) do
  60. begin
  61. readln (Fil, Fstring);
  62. writeln (Fstring);
  63. end;
  64. close (Fil);
  65. Intext:= Fstring;
  66. end;
  67.  
  68. var
  69. InString: String;
  70. choise: byte;
  71. begin
  72. repeat
  73. writeln ('Input method: 1 - keyboard | 2 - text file');
  74. readln (choise);
  75. case choise of
  76. 1:
  77. begin
  78. writeln ('Enter text string:');
  79. readln (InString);
  80. end;
  81. 2: InString:= InText;
  82. end;
  83. if CheckString (InString) then
  84. writeln ('There is a balance brackets')
  85. else
  86. writeln ('There is no balance brackets');
  87. writeln ('To exit the program, press Esc, to repeat - any other key');
  88. until (readKey = #27);
  89. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement