1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
Program Calc;
Var
Debug:boolean;
Math:string;
imp:string;
x:byte;
Procedure Error(ErrorMsg:string);
begin
WriteLn(Math);
While x>1 do begin write(' '); dec(x); end; WriteLn('^');
WriteLn(ErrorMsg);
Halt(10);
end;
function EvalMath:real; forward;
Function RealVal(s:string):real;
var
v:real;
x:byte;
begin
v:=0;
for x:=1 to length(s) do begin
v:=v*10+ord(s[x])-ord('0');
end;
RealVal:=v;
end;
Function EvalFactor:real;
var
s:string;
v:real;
begin
Case Math[x] of
'(' : begin
inc(x); EvalFactor:=EvalMath;
if Math[x]<>')' then Error('Expected [)]');
inc(x);
end;
'0'..'9': begin
s:='';
While (x<=Length(Math))and(Math[x] in ['0'..'9']) do begin
s:=s+Math[x];
inc(x);
end;
v:=RealVal(s);
if debug then writeln(imp,v:0:2);
EvalFactor:=v;
end;
else Error('Invalid number');
end;
end;
Function EvalTerme:real;
var
v1,v2:real;
op:char;
begin
v1:=EvalFactor;
while Math[x] in ['*','/'] do begin
op:=Math[x]; inc(x);
if debug then begin
writeln(imp,op);
imp:=imp+' ';
end;
v2:=EvalFactor;
if op='*' then v1:=v1*v2 else v1:=v1/v2;
if debug then begin
dec(imp[0]);
writeln(imp,'=',v1:0:2);
end;
end;
EvalTerme:=v1;
end;
Function EvalMath:real;
var
v1,v2:real;
op:char;
begin
v1:=EvalTerme;
while Math[x] in ['+','-'] do begin
op:=Math[x]; inc(x);
if debug then begin
writeln(imp,op);
imp:=imp+' ';
end;
v2:=EvalTerme;
if op='+' then v1:=v1+v2 else v1:=v1-v2;
if debug then begin
dec(imp[0]);
writeln(imp,'=',v1:0:2);
end;
end;
EvalMath:=v1;
end;
begin
if (paramcount=0)or(paramstr(1)='/?') then begin
WriteLn('C By Paul Toth (101350.2175@Compuserve.com)');
WriteLn(' C [?] "mathematic operation"');
exit;
end;
Debug:=Paramstr(1)='?';
Math:='';
for x:=1+Ord(Debug) to ParamCount do begin
Math:=Math+ParamStr(x);
end;
x:=1;
imp:='';
WriteLn(EvalMath:0:2);
end. |
Partager