Pagini recente » Cod sursa (job #818637) | Cod sursa (job #1263260) | Cod sursa (job #2322504) | Cod sursa (job #1544308) | Cod sursa (job #328588)
Cod sursa(job #328588)
program evaluare;
var a,b:text;
i,j,k,x:integer;
z:array[1..2000000] of char;
function eval(x,y:longint):longint; forward;
function prodeval(x,y,zz:longint):longint;
var i:longint;
begin
i:=x;
while (z[i]<>'*') and (z[i]<>'/') do
i:=i+1;
if zz=1 then prodeval:=eval(x,i-1)*eval(i+1,y) else prodeval:=eval(x,i-1) div eval(i+1,y);
end;
function eval(x,y:longint):longint;
var i,xx,j,zz,signum:longint;
cod:integer;
s:string;
begin
if x>k then exit(0);
signum:=1;
eval:=0;
i:=x;
while ((z[i]<>'(') and (i=x)) or ((i<y) and not(z[i] in ['+','-','*','/','('])) do
i:=i+1;
if x=y then i:=y;
if i=y then
begin
s:='';
for j:=x to y do
s:=s+z[j];
val(s,eval,cod);
end
else
if (z[i]='+') or (z[i]='-') then eval:=eval(x,i-1)+eval(i,y) else
if (z[i]='*') or (z[i]='/') then
begin
if z[i]='*' then zz:=1 else zz:=2;
while (i<y) and ( (z[i]<>'+') or (z[i]<>'-') )do
i:=i+1;
if i=y then eval:=prodeval(x,y,zz) else eval:=prodeval(x,i-1,zz)+eval(i,y);
end else
if (z[i]='(') then
begin
xx:=1;
j:=i;
if i=x+1 then if z[x]='-' then signum:=-1;
while xx<>0 do
begin
i:=i+1;
if z[i]='(' then xx:=xx+1 else
if z[i]=')' then xx:=xx-1;
end;
if (i+1<=y) and (z[i+1]='*') or (z[i+1]='/') then
begin
if z[i+1]='*' then zz:=1 else zz:=2;
while (z[i]<>'+') and (z[i]<>'-') do i:=i+1;
eval:=prodeval(j,i-1,zz)*signum+eval(i,y);
end else
begin
if i+1>y then eval:=eval(j+1,i-1)*signum else eval:=eval(j+1,i-1)*signum+eval(i+1,y);
end;
end;
end;
begin
assign(a,'evaluare.in');
assign(b,'evaluare.out');
reset(a);
rewrite(b);
while not(eoln(a)) do
begin
k:=k+1;
Read(a,z[k]);
if (k=1) and (z[k]<>'+') and (z[k]<>'-') then begin z[2]:=z[k]; k:=k+1; z[1]:='+'; end;
if (z[k-1]='(') and (z[k]<>'-') then begin k:=k+1; z[k]:=z[k-1]; z[k-1]:='+'; end;
end;
j:=1;
Writeln(b,eval(j,k));
close(b);
end.