Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. {$mode delphi}
  2. {$r+}
  3. unit stack_array;
  4.  
  5. interface
  6.  
  7. type
  8. stack = record
  9. a: array of integer;
  10. n: integer;
  11. end;
  12.  
  13. procedure init(var s: stack);
  14. procedure push(var s: stack; i: integer);
  15. function pop(var s: stack): integer;
  16. function isEmpty(s: stack): boolean;
  17.  
  18. implementation
  19.  
  20. procedure init(var s: stack);
  21. begin
  22. setLength(s.a, 1);
  23. s.n := 0;
  24. end;
  25.  
  26. procedure push(var s: stack; i: integer);
  27. begin
  28. if length(s.a) = s.n then
  29. setLength(s.a, length(s.a) * 2);
  30. s.a[s.n] := i;
  31. s.n := s.n + 1;
  32. end;
  33.  
  34. function pop(var s: stack): integer;
  35. begin
  36. pop := s.a[s.n - 1];
  37. s.n := s.n - 1;
  38. end;
  39.  
  40. function isEmpty(s: stack): boolean;
  41. begin
  42. isEmpty := (s.n = 0);
  43. end;
  44.  
  45. end.
  46.  
  47. uses character, sysutils, stack_array;
  48.  
  49. procedure error(s: string);
  50. begin
  51. writeln(s);
  52. halt;
  53. end;
  54.  
  55. function readChar(): char;
  56. begin
  57. if seekEoln then error('Chyba!');
  58. read(readChar);
  59. end;
  60.  
  61. function isOp(c: char): boolean;
  62. begin
  63. case c of
  64. '+', '-', '*', '/': exit(true);
  65. else exit(false);
  66. end;
  67. end;
  68.  
  69. function evalOp(c: char; i, j: integer): integer;
  70. begin
  71. case c of
  72. '+': exit(i + j);
  73. '-': exit(i - j);
  74. '*': exit(i * j);
  75. '/': exit(i div j);
  76. end;
  77. end;
  78.  
  79. function evalPostFix():integer;
  80. var
  81. c: char;
  82. i, j: integer;
  83. begin
  84. c := readChar();
  85. if isDigit(c) then exit(strToInt(c));
  86. i := evalPrefix();
  87. j := evalPrefix();
  88. if not isOp(c) then error('Chyba!');
  89. exit(evalOp(c, i, j));
  90. end;
  91.  
  92. begin
  93. while not eof do
  94. read(c);
  95. if isOp(c) then evalOp(c);
  96. writeln(evalPostFix());
  97. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement