Cod sursa(job #574411)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 7 aprilie 2011 10:08:02
Problema Evaluarea unei expresii Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 2.98 kb
var e:array [1..100000] of char;     {e-expresia postfixata unde et e capatul expresiei}
    stv:array[0..100000] of char;    {stiva semnelor unde cap e capatul stivei}
    st:array[0..100000] of longint; {stiva numerelor in timpul evaluarii}
    buf1:array [1.. 1 shl 17] of char;
    c:char;
    i, j, n, nr, et, nt, cap:longint;
    ok:boolean;                       {responsabil pentru numerele de mai multe cifre}
    f, g:text;

begin
assign (f, 'evaluare.in'); settextbuf (f, buf1); reset (f);
assign (g, 'evaluare.out'); rewrite (g);


{transofrmarea din infixat in postfixat}
et:=0;
while not eoln (f) do
  begin
  read (f, c);
  case c of
    '+':begin
        if ok then begin et:=et+1; e[et]:=' '; ok:=false; end;
        while (cap>0) and (stv[cap]<>'(') do
          begin
          et:=et+1; e[et]:=stv[cap]; cap:=cap-1;
          end;
        cap:=cap+1; stv[cap]:=c;
        end;
    '-':begin
        if ok then begin et:=et+1; e[et]:=' '; ok:=false; end;
        while (cap>0) and (stv[cap]<>'(') do
          begin
          et:=et+1; e[et]:=stv[cap]; cap:=cap-1;
          end;
        cap:=cap+1; stv[cap]:=c;
        end;
    '*':begin
        if ok then begin et:=et+1; e[et]:=' '; ok:=false; end;
        while (stv[cap]='*') or (stv[cap]='/') do
          begin
          et:=et+1; e[et]:=stv[cap]; cap:=cap-1;
          end;
        cap:=cap+1; stv[cap]:=c;
        end;
    '/':begin
        if ok then begin et:=et+1; e[et]:=' '; ok:=false; end;
        while (stv[cap]='*') or (stv[cap]='/') do
          begin
          et:=et+1; e[et]:=stv[cap]; cap:=cap-1;
          end;
        cap:=cap+1; stv[cap]:=c;
        end;
    '(':begin
        if ok then begin et:=et+1; e[et]:=' '; ok:=false; end;
        cap:=cap+1; stv[cap]:=c;
        end;
    ')':begin
        if ok then begin et:=et+1; e[et]:=' '; ok:=false; end;
        while (stv[cap]<>'(') do
          begin
          et:=et+1; e[et]:=stv[cap]; cap:=cap-1;
          end;
        cap:=cap-1;
        end;
    else begin
         et:=et+1;
         e[et]:=c;
         ok:=true;
         end;
    end;
  end;
if ok then begin et:=et+1; e[et]:=' '; ok:=false; end;
while cap>0 do
  begin
  et:=et+1; e[et]:=stv[cap];
  cap:=cap-1;
  end;


{evaluarea expresiei postfixate}
i:=1; cap:=0;
while i <= et do
  begin
  case e[i] of
    '+':begin
        st[cap-1]:=st[cap-1]+st[cap];
        cap:=cap-1;
        end;
    '-':begin
        st[cap-1]:=st[cap-1]-st[cap];
        cap:=cap-1;
        end;
    '*':begin
        st[cap-1]:=st[cap-1]*st[cap];
        cap:=cap-1;
        end;
    '/':begin
        st[cap-1]:=st[cap-1] div st[cap];
        cap:=cap-1;
        end;
    else begin
         nr:=0;
         while e[i] <> ' ' do
           begin
           nr:=nr*10+ord(e[i])-48;
           i:=i+1
           end;
         cap:=cap+1; st[cap]:=nr;
         end;
    end;
  i:=i+1;
  end;

writeln (g,st[cap]);

close (f); close (g);
end.