Cod sursa(job #640229)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 24 noiembrie 2011 22:47:54
Problema Evaluarea unei expresii Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.13 kb
Program expresie;
 var a:array [1..100001] of char;
     i:longint;
     k:int64;
     fi,fo:text;
function rez():int64;forward;
function factor():int64;
 var x:int64;
begin
 x:=0;
 if a[i]='(' then begin inc(i); x:=rez(); inc(i); end
              else while (a[i]>='0') and (a[i]<='9') do begin
                                          x:=x*10+ord(a[i])-48;
                                          inc(i);
                                          end;

 factor:=x;
end;
function termen():int64;
 var y:int64;
begin
 y:=factor;
  while (a[i]='*') or (a[i]='/') do
   if a[i]='*' then begin inc(i); y:=y*factor; end
                else begin inc(i); y:=y div factor; end;
 termen:=y;
end;
function rez():int64;
 var v:int64;
begin
 v:=termen();
  while (a[i]='+') or (a[i]='-') do
   if a[i]='+' then begin
                     inc(i);
                      v:=v+termen;
                     end
    else begin inc(i); v:=v-termen; end;
 rez:=v;
end;
begin
 assign(fi,'expresie.in');
  assign(fo,'expresie.out');
 reset(fi); rewrite(fo);
  read(fi,a);  i:=1;
   k:=rez;
 write(fo,k);
 close(fo);
end.