Znalazłem na pewnej stronie o programowaniu gotowy kod kalkulatora RPN w Pascalu.
program rpncalc(input,output);
type pnum = ^num;
num = record x: real; next: pnum end;
var nums: pnum;
digits: packed array [1..10] of char;
tktype: (nm,ad,sb,mp,dv);
error: boolean;
z: real;
procedure push(x: real);
var p: pnum;
begin new(p); p^.x := x; p^.next := nums; nums := p end;
function pop: real;
var p: pnum;
begin
if nums <> nil then begin
p := nums;
nums := nums^.next;
pop := p^.x;
dispose(p)
end else begin
error := true;
pop := 0
end
end;
procedure skipws;
begin while (input^ = ' ') and (not eoln) do get(input) end;
procedure processtoken;
var c,cn: char;
f,e,x,y: real;
sgn,d,i: integer;
procedure getnumber;
begin
while (c <> ' ') and (not error) do begin
d := -1; i := 1;
while i < 11 do
if digits[i] = c then
begin d := i-1; i := 11 end
else i := i+1;
if c = '.' then
if f = -1 then f := 0 else error := true
else if d > -1 then begin
if z = -1 then z := 0;
if f = -1 then z := 10*z+d
else begin e := e/10; f := f+d*e end
end else error := true;
if eoln then c := ' ' else read(c)
end;
if z = -1 then error := true;
if not error then begin
if f = -1 then f := 0;
z := sgn*(z+f)
end
end;
begin { processtoken }
tktype := nm;
sgn := 1;
z := -1; f := -1; e := 1;
read(c);
if c in ['+', '-', '*', '/'] then begin
if eoln then cn := ' ' else read(cn);
if cn = ' ' then
case c of
'+': tktype := ad; '-': tktype := sb;
'*': tktype := mp; '/': tktype := dv
end
else begin
if c = '-' then sgn := -1 else error := c <> '+';
c := cn
end
end;
if (not error) and (tktype = nm) then getnumber;
if not error then begin
if tktype <> nm then begin
y := pop; x := pop;
if not error then
case tktype of
ad: z := x+y; sb: z := x-y;
mp: z := x*y; dv: z := x/y
end
end;
push(z);
skipws
end
end;
procedure init;
var t: real;
begin
while nums <> nil do t := pop;
error := false;
if not eof then skipws
end;
begin { main }
digits := '0123456789';
nums := nil;
init;
while not eof do begin
if not eoln then begin
repeat
processtoken;
if error then while not eoln do get(input)
until eoln;
if not error then begin
z := pop;
if nums = nil then writeln(z:1:4) else error := true
end;
if error then writeln('error')
end;
readln; init
end
end.
Jednak kompilacja staje na drugiej procedurze w tej linijce:
begin while (input^ = ' ') and (not eoln) do get(input) end;
[Error] Project2.dpr(35): Pointer type required
[Error] Project2.dpr(35): Operator not applicable to this operand type
[Error] Project2.dpr(114): Undeclared identifier: 'get'
Próbowałem to kompilować przez Delphi 7.