Virtual Object
::
Programs
:: Подсчет выражения |
||||
Наша кнопка |
||||
Подсчет выражения {$APPTYPE CONSOLE}
var a: ansistring;
n: longint;
⁄⁄-------------------------------------------------
function step(x,y:longint):longint;
var i,t:longint;
begin
t:=x;
for i:=2 to y do t:=t*x;
if y=0 then step:=1 else step:=t;
end;
⁄⁄-------------------------------------------------
function calc(l,r:longint):longint;
var leng,scob,i,numf,x:longint;
z: array [0..6] of longint;
stop:boolean;
begin
calc:=0;
if l>r then exit;
fillchar(z,sizeof(z),0);
scob:=0;
x:=0;
i:=r;
numf:=0;
stop:=false;
while ((i>=l)and(not stop)) do
begin
if a[i]=′)′ then begin numf:=1; inc(scob); end else
if a[i]=′(′ then begin numf:=1; dec(scob); end else
if scob=0 then
begin
if (((a[i]=′+′)or(a[i]=′-′))and(z[1]=0)) then z[1]:=i else
if (((a[i]=′*′)or(a[i]=′⁄′))and(z[2]=0)) then z[2]:=i else
if (a[i]=′^′) then z[3]:=i;
if a[i] in [′+′,′-′,′*′,′^′,′⁄′] then x:=1;
if (a[i]=′+′)or(a[i]=′-′) then stop:=true;
end;
dec(i);
end;
leng:=r-l+1;
if x=0 then
if numf=0 then calc:=StrToInt(copy(a,l,leng)) else
calc:=calc(l+1,r-1) else
begin
while (x<4)and(z[x]=0) do inc(x);
case x of
1: if a[z[x]]=′+′ then calc:=calc(l,z[x]-1) + calc(z[x]+1,r) else
calc:=calc(l,z[x]-1) - calc(z[x]+1,r);
2: if a[z[x]]=′*′ then calc:=calc(l,z[x]-1) * calc(z[x]+1,r) else
calc:=calc(l,z[x]-1) div calc(z[x]+1,r);
3: calc:=step(calc(l,z[x]-1),calc(z[x]+1,r));
end;
end;
end;
⁄⁄-------------------------------------------------
begin
assign(input,′input.txt′); reset(input);
assign(output,′output.txt′); rewrite(output);
readln(a);
n:=calc(1,length(a));
writeln(n);
end.
|
||||
Object © 2004 - 2005. All rights reserved. |
||||