Advertisement
Alyks

Untitled

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