Cod sursa(job #145856)

Utilizator you_reheroMihai Gojinetchi you_rehero Data 29 februarie 2008 16:43:27
Problema Evaluarea unei expresii Scor 40
Compilator fpc Status done
Runda Arhiva educationala Marime 2.95 kb
const vl:array['0'..'9']of byte=(0,1,2,3,4,5,6,7,8,9);
var fi,fo:text;
    s:char;
    ct,nr,vf,ok,i:longint;
    op:array[0..200000]of longint;
    stiva:array[0..200000]of char;
begin
  assign(fi,'evaluare.in'); reset(fi);
  assign(fo,'evaluare.out'); rewrite(fo);
  vf:=0; nr:=0; ct:=0;
  while not eof(fi) do
    begin
      read(fi,s);
      if s='(' then
        begin
          inc(vf); stiva[vf]:=s;
        end
    else
      if s in ['0'..'9'] then begin nr:=nr*10+vl[s]; ok:=1; end
    else
      if s=')' then
        begin
          if ok=1 then
            begin
              inc(ct); op[ct]:=nr; nr:=0; ok:=0; end;
          for i:=vf downto 1 do
            if stiva[i]<>'(' then
              begin
                if stiva[i]='+' then op[ct-1]:=op[ct-1]+op[ct]
                 else
                  if stiva[i]='-' then op[ct-1]:=op[ct-1]-op[ct]
                   else
                    if stiva[i]='*' then op[ct-1]:=op[ct-1]*op[ct]
                     else op[ct-1]:=op[ct-1] div op[ct];
                 dec(ct);
              end
            else
             begin
               vf:=i-1;
               break;
             end;
        end
    else
      if s in ['+','-'] then
        begin
          if ok=1 then
            begin
              inc(ct); op[ct]:=nr; nr:=0; ok:=0; end;
          for i:=vf downto 1 do
            if stiva[i] in ['+','-','*','/'] then
              begin
                if stiva[i]='+' then op[ct-1]:=op[ct-1]+op[ct]
                 else
                  if stiva[i]='-' then op[ct-1]:=op[ct-1]-op[ct]
                   else
                    if stiva[i]='*' then op[ct-1]:=op[ct-1]*op[ct]
                     else op[ct-1]:=op[ct-1] div op[ct];
                dec(ct);
              end
            else
              begin
                vf:=i;
                break;
              end;
          inc(vf);
          stiva[vf]:=s;
        end
    else
      if s in ['*','/'] then
        begin
          if ok=1 then
            begin
              inc(ct); op[ct]:=nr; nr:=0; ok:=0; end;
            for i:=vf downto 1 do
              if stiva[i] in ['*','/'] then
                begin
                  if stiva[i]='*' then op[ct-1]:=op[ct-1]*op[ct]
                     else op[ct-1]:=op[ct-1] div op[ct];
                  dec(ct);
                end
              else
                begin
                  vf:=i;
                  break;
                end;
          inc(vf);
          stiva[vf]:=s;
        end;
  end;
  if ok=1 then
    begin
      inc(ct); op[ct]:=nr; end;
  for i:=vf downto 1 do
    begin
      if stiva[i]='+' then op[ct-1]:=op[ct-1]+op[ct]
       else
        if stiva[i]='-' then op[ct-1]:=op[ct-1]-op[ct]
         else
          if stiva[i]='*' then op[ct-1]:=op[ct-1]*op[ct]
           else op[ct-1]:=op[ct-1] div op[ct];
      dec(ct);
    end;
  writeln(fo,op[1]);
  close(fi);
  close(fo);
end.