Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {$mode delphi}
- {$r+}
- unit stack_array;
- interface
- type
- stack = record
- a: array of integer;
- n: integer;
- end;
- procedure init(var s: stack);
- procedure push(var s: stack; i: integer);
- function pop(var s: stack): integer;
- function isEmpty(s: stack): boolean;
- implementation
- procedure init(var s: stack);
- begin
- setLength(s.a, 1);
- s.n := 0;
- end;
- procedure push(var s: stack; i: integer);
- begin
- if length(s.a) = s.n then
- setLength(s.a, length(s.a) * 2);
- s.a[s.n] := i;
- s.n := s.n + 1;
- end;
- function pop(var s: stack): integer;
- begin
- pop := s.a[s.n - 1];
- s.n := s.n - 1;
- end;
- function isEmpty(s: stack): boolean;
- begin
- isEmpty := (s.n = 0);
- end;
- end.
- uses character, sysutils, stack_array;
- procedure error(s: string);
- begin
- writeln(s);
- halt;
- end;
- function readChar(): char;
- begin
- if seekEoln then error('Chyba!');
- read(readChar);
- end;
- function isOp(c: char): boolean;
- begin
- case c of
- '+', '-', '*', '/': exit(true);
- else exit(false);
- end;
- end;
- function evalOp(c: char; i, j: integer): integer;
- begin
- case c of
- '+': exit(i + j);
- '-': exit(i - j);
- '*': exit(i * j);
- '/': exit(i div j);
- end;
- end;
- function evalPostFix():integer;
- var
- c: char;
- i, j: integer;
- begin
- c := readChar();
- if isDigit(c) then exit(strToInt(c));
- i := evalPrefix();
- j := evalPrefix();
- if not isOp(c) then error('Chyba!');
- exit(evalOp(c, i, j));
- end;
- begin
- while not eof do
- read(c);
- if isOp(c) then evalOp(c);
- writeln(evalPostFix());
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement