Cod sursa(job #2607471)

Utilizator Arteni_CristiArteni Cristi Arteni_Cristi Data 29 aprilie 2020 20:33:20
Problema Evaluarea unei expresii Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 1.62 kb
var t,op:array[0..100005] of ansistring;
    v:array[0..100005] of boolean;
    n,m,i,j,k,a,b:longint;
    s:ansistring;
    q:string;
begin
assign(input,'evaluare.in'); reset(input);
assign(output,'evaluare.out'); rewrite(output);
readln(s);
for i:=1 to length(s) do
 begin
  if pos(s[i],'0123456789')<>0 then
   begin
    q:=q+s[i];
    if (i=length(s)) or (pos(s[i+1],'0123456789')=0) then
     begin
      inc(n);
      t[n]:=q;
      v[n]:=true;
      q:=''
     end
   end else
  if s[i]='(' then begin inc(m); op[m]:='(' end else
  if (s[i]='*') or (s[i]='/') then
   begin
    inc(k);
    inc(m); op[m]:=s[i];
    while (op[m-1]='*') or (op[m-1]='/') do
     begin
      inc(n); t[n]:=op[m-1];
      op[m-1]:=op[m];
      dec(m)
     end
   end else
  if (s[i]='+') or (s[i]='-') then
   begin
    inc(k);
    inc(m); op[m]:=s[i];
    while pos(op[m-1],'+-/*')<>0 do
     begin
      inc(n); t[n]:=op[m-1];
      op[m-1]:=op[m];
      dec(m)
     end
   end else
  if s[i]=')' then
   begin
    while (m>0) and (op[m]<>'(') do
     begin
      inc(n); t[n]:=op[m];
      dec(m)
     end;
    dec(m)
   end
 end;
if m>0 then for i:=m downto 1 do
 begin
  inc(n);
  t[n]:=op[i]
 end;
i:=1;
while k>0 do
 begin
  while pos(t[i],'-+/*')=0 do inc(i);
  j:=i;
  while v[j]=false do dec(j); v[j]:=false;
  val(t[j],b);
  while v[j]=false do dec(j); v[j]:=false;
  val(t[j],a);
  case t[i] of
   '+' : str(a+b,q);
   '-' : str(a-b,q);
   '*' : str(a*b,q);
   '/' : str(a div b,q)
  end;
  t[i]:=q;
  v[i]:=true;
  dec(k)
 end;
writeln(t[n]);
close(input);
close(output)
end.