Alyks

Untitled

Dec 9th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.92 KB | None | 0 0
  1. program task4;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.     System.SysUtils,
  8.     Contnrs;
  9.  
  10. function IsBalanced(Str: String): Boolean;
  11. var
  12.     i: Integer;
  13.     Curr: Char;
  14.     ContinueCycle: Boolean;
  15.     Stack: TStack;
  16. begin
  17.     Stack := TStack.Create();
  18.     ContinueCycle := true;
  19.     i := 1;
  20.     while ((i < Length(Str)) and ContinueCycle) do
  21.     begin
  22.         Curr := Str[i];
  23.         if (Curr = '(') then
  24.             Stack.Push(Pointer(Curr))
  25.         else if (Curr = ')') then
  26.         begin
  27.             if (Stack.Count = 0) then
  28.                 ContinueCycle := false
  29.             else
  30.                 Stack.Pop();
  31.         end;
  32.         Inc(i);
  33.     end;
  34.     IsBalanced := ContinueCycle;
  35. end;
  36.  
  37. function IsStringContainsParentheses(Str: String): Boolean;
  38. begin
  39.     IsStringContainsParentheses := (Pos('(', Str) <> 0) or (Pos(')', Str) <> 0);
  40. end;
  41.  
  42. function ReadFile(FilePath: String): String;
  43. var
  44.     Str: String;
  45.     InputFile: TextFile;
  46. begin
  47.     Str := '';
  48.     if (FileExists(FilePath)) then
  49.     begin
  50.         AssignFile(InputFile, FilePath);
  51.         Reset(InputFile);
  52.         if (Not Eof(InputFile)) then
  53.             Readln(InputFile, Str);
  54.         CloseFile(InputFile);
  55.     end;
  56.     ReadFile := Str;
  57. end;
  58.  
  59. procedure SaveResult(ResultStr: String);
  60. var
  61.     Decision: String;
  62.     OutputFile: TextFile;
  63. begin
  64.     while ((Decision <> 'Y') and (Decision <> 'N')) do
  65.     begin
  66.         Writeln('Сохранить результат в файл? (Y - сохранить, N - не сохранять)');
  67.         Readln(Decision);
  68.     end;
  69.  
  70.     if (Decision = 'Y') then
  71.     begin
  72.         try
  73.             AssignFile(OutputFile, 'output.txt');
  74.             Rewrite(OutputFile);
  75.             Writeln(OutputFile, ResultStr);
  76.             CloseFile(OutputFile);
  77.             Writeln('Результат сохранен в файл output.txt');
  78.         except
  79.             Writeln('Произошла ошибка при сохранении файла');
  80.         end;
  81.     end;
  82. end;
  83.  
  84. procedure Main();
  85. var
  86.     InputType, ResultStr, Str, FilePath: String;
  87.     Res: Boolean;
  88. begin
  89.     Writeln('Данная программа проверяет баланс скобок в строке');
  90.     Writeln;
  91.     while ((InputType <> 'C') and (InputType <> 'F')) do
  92.     begin
  93.         Writeln('Введите C, если хотите ввести строку из консоли, или F, если хотите ввести строку из файла');
  94.         Readln(InputType);
  95.     end;
  96.     if (InputType = 'C') then
  97.     begin
  98.         Writeln('Введите строку, для которой необходимо проверить баланс скобок');
  99.         Readln(Str);
  100.     end
  101.     else if (InputType = 'F') then
  102.     begin
  103.         Writeln('Введите путь к файлу');
  104.         Readln(FilePath);
  105.         Str := ReadFile(FilePath);
  106.     end;
  107.  
  108.     Res := IsStringContainsParentheses(Str);
  109.     if (Res) then
  110.     begin
  111.         Res := IsBalanced(Str);
  112.         if (Res) then
  113.             ResultStr := 'Баланс скобок соблюдён'
  114.         else
  115.             ResultStr := 'Баланс скобок не соблюдён'
  116.     end
  117.     else
  118.         ResultStr := 'В строке отсутсвуют скобки';
  119.     Writeln(ResultStr);
  120.     SaveResult(ResultStr);
  121.     Readln;
  122. end;
  123.  
  124. begin
  125.     Main();
  126. end.
Add Comment
Please, Sign In to add comment