Cod sursa(job #183011)

Utilizator llobyLodoaba Mihai lloby Data 21 aprilie 2008 17:06:29
Problema Evaluarea unei expresii Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 4.63 kb
Program ecuatie;
type vec=array[0..255] of integer;
     cev=array[0..255] of char;
var s: string;
    t: text;
    semne: set of char;
    add: longint;
Procedure rezolv(v:vec;e:cev ;k: integer);
var put, ad,i: longint;
     neg: boolean;
begin

  e[0]:='+';
  put:=1;
  ad:=0;
  i:=1;
  neg:=false;
  repeat
      if e[i-1]='+'  then begin
                       if (e[i]<>'*') and (e[i]<>'/') then ad:=ad+v[i]
                           else if e[i]='*' then put:=put*v[i]
                           else if e[i]='/' then put:=put*v[i];

                     end;
      if e[i-1]='-' then begin
                       if (e[i]<>'*') and (e[i]<>'/') then ad:=ad-v[i]
                           else if e[i]='*' then begin
                                                   put:=put*v[i];
                                                   neg:=true;
                                                 end
                           else if e[i]='/' then begin
                                                    neg:=true;
                                                    put:=put*v[i];
                                                 end;
                    end;
      if e[i-1]='*' then begin
                        put:=put*v[i];
                        if e[i]='+' then begin if neg=false then
                                              ad:=ad+put
                                              else ad:=ad-put;
                                              put:=1;
                                              neg:=false;
                                         end;
                        if e[i]='-' then begin if neg=false then
                                              ad:=ad+put
                                              else ad:=ad-put;
                                              put:=1;
                                              neg:=false;
                                         end;
                    end;
      if e[i-1]='/' then begin
                        put:=put div v[i];
                        if e[i]='+' then begin
                                              if neg=false then
                                              ad:=ad+put
                                              else ad:=ad-put;
                                              put:=1;
                                              neg:=false;
                                         end;
                        if e[i]='-' then begin
                                              if neg=false then
                                              ad:=ad+put
                                              else ad:=ad-put;
                                              put:=1;
                                              neg:=false;
                                         end;
                    end;
      i:=i+1;

  until i=k+1;
  add:=ad;



end;
Procedure desc(s: string);
var i,k,nr,p,j,q,par: longint;
    err: integer;
    v: vec;
    e: cev;
    s1: string;
begin
    k:=0;
    i:=1;
    nr:=0;
    insert('+',s,length(s)+1);
   repeat
        if (s[i]<>'(') and (s[i]<>')') then  begin
                                                if s[i] in semne then
                                                   begin
                                                      k:=k+1;
                                                      e[k]:=s[i];
                                                      v[k]:=nr;
                                                      nr:=0;
                                                   end
                                                 else begin
                                                        val(s[i],p,err);
                                                        nr:=nr*10+p;
                                                      end;
                                                    i:=i+1;
                                                 end
       else
          begin
               par:=1;
              for j:=i+1 to length(s) do
                begin
                 if s[j]='(' then par:=par+1;
                 if s[j]=')' then par:=par-1;
                 if par=0 then begin q:=j;
                 j:=length(s);
                 end;
                end;
              s1:=copy(s,i+1,q-i-1);
              desc(s1);
              nr:=add;
              i:=q+1;
          end;


   until i=length(s)+1;
   rezolv(v,e,k);
end;
begin
   semne:=['+','-','*','/'];
   assign(t,'evaluare.in'); reset(t);
   read(t,s);
   close(t);
   desc(s);
   assign(t,'evaluare.out'); rewrite(t);
   writeln(t,add);
   close(t);
end.