uses wincrt;
label 1,2,3,4;
type zz=array[1..20] of string;
 aa=array[1..20] of byte;
 kk=array[1..30] of real;
var
i,n,m,k,j,o,l,x: integer; a,b,v:string;
c:array[1..10] of string;
f:text; result:real;

function calculate(x:string):string;
var i,j,k:integer; a:zz; t:aa; s,s1,s2:real; h:string;
c1:integer; p:integer; c:kk;
begin
k:=1;
s:=0;
s1:=1;
for i:=1 to 20 do
a[i]:='';
for i:=1 to 30 do
t[i]:=0;
 for i:=1 to length(x) do
 begin
 if (x[i]='+') or (x[i]='-') or (x[i]='*') or (x[i]='/')
 then
 begin
 if x[i]='+' then t[k]:=1;
 if x[i]='-' then t[k]:=2; {разделяет плюсы, минусы и цифры}
 if x[i]='*' then t[k]:=3;
 if x[i]='/' then t[k]:=4;
 k:=k+1;
 end
 else
 a[k]:=a[k]+x[i];
 end;
 for i:=1 to k do {переводит в числовой массив}
 val(a[i],c[i],c1);
 if x[1]='-' then p:=1;
 if (p<>1) and (t[1]=2) and (t[2]<>3) and (t[2]<>4) then s:=c[1];
 if (t[1]=2) and (t[2]=2) then s:=c[1];
if (t[1]=1) or (k=1) then s:=c[1]; {начальные значения}

for i:=1 to k do
 begin
 if (t[i]=3) then
 begin
 if (t[i-1]<>2) and (t[i-1]<>3) and (t[i-1]<>4) then
 begin
 {s:=s+c[i]*c[i+1];}
 s1:=c[i]*c[i+1];
 {s:=s+s1;}
 end; {знак *}
 if (t[i-1]=2) then
 begin
 {s:=s-c[i]*c[i+1];}
 s1:=-c[i]*c[i+1];
 {s:=s+s1;}
 end;
 if (t[i-1]=4) or (t[i-1]=3) then
 begin
 s1:=s1*c[i+1];
 {s:=s+s1;}
 end;
 end;


 if (t[i]=4) then
 begin
 if (t[i-1]<>2) and (t[i-1]<>3) and (t[i-1]<>4) then
 begin
 {s:=s+c[i]/c[i+1]; }
 s1:=c[i]/c[i+1];
 end;
 if t[i-1]=2 then
 begin {знак /}
 s1:=-c[i]/c[i+1];
 end;
 if (t[i-1]=3) or (t[i-1]=4) then
 begin
 s1:=s1/c[i+1];
 {s:=s1+s; }
 end;
 end;
 if (t[i+1]<>3) and (t[i+1]<>4) and (s1<>1) then
 begin
 s:=s+s1;
 s1:=1;
 end;
 end;
for i:=1 to k do
begin
if t[i]=2
then
begin
if (i=1) and (p=1) and (t[2]=2) then s:=s-c[2];
if (t[i+1]<>4) and (t[i+1]<>3) then
s:=s-c[i+1] {знак -}
else
s:=s+c[i];
end;

if (t[i]=1) and (t[i+1]<>3) and (t[i+1]<>4)
then
s:=s+c[i+1]; {знак +}
{writeln(c[i]:5:3);}
end;

str(s:5:3,h);
calculate:=h;
end;


begin
assign(f,'calc.in');
reset(f);
readln(f,a);
1:
k:=0;
for i:=1 to length(a) do
if a[i]='(' then k:=k+1;
if k=0 then goto 2;
i:=0;
l:=0;
while i<=length(a) do
begin
3: if l>k then goto 2;
i:=i+1;
if a[i]='(' then
begin
l:=l+1;
n:=i;
b:='';
while a[i]<>')' do
begin
if (a[i]='(') and (a[i+1]='(') then goto 3;
if a[i]='(' then
begin
n:=i;
i:=i+1;
b:='';
end;
b:=b+a[i];
{writeln(b);}
i:=i+1;
end;
m:=i;
a:=copy(a,1,n-1)+calculate(b)+copy(a,m+1,length(a));
{writeln(a);}
goto 1;
end;
end;
2:
val(calculate(a),result,o);
write(result:5:3);
end.


 Похожие публикации
2010-11-04 • Просмотров [ 13782 ]