记录编号 18610 评测结果 AAAAAAAAAA
题目名称 表达式转换 最终得分 100
用户昵称 GravatarDes. 是否通过 通过
代码语言 Pascal 运行时间 0.016 s
提交时间 2010-09-16 20:52:19 内存使用 0.12 MiB
显示代码纯文本
program express;
var p:array['('..'^']of integer;
    s,m:string;
    a:array[0..1000]of longint;
    b,c:array[1..1000]of char;
    t,k,n,j,i,x,y,oo:longint;
begin
assign(input,'express.in');
reset(input);
assign(output,'express.out');
rewrite(output);
readln(s);
p['(']:=1;
p[')']:=5;
p['+']:=2;
p['-']:=2;
p['*']:=3;
p['/']:=3;
p['^']:=4;
for t:=1 to length(s) do
  begin
    if (s[t]>='0')and(s[t]<='9') then
      begin
        inc(x);
        a[x]:=ord(s[t])-ord('0');
      end
    else
      begin
        inc(y);
        b[y]:=s[t];
        if y>=2 then
        if (b[y]<>')')and(b[y]<>'(') then
          repeat
            if (y>1)and(p[b[y]]<=p[b[y-1]])then
              begin
                if (b[y-1]<>'(')and(b[y-1]<>')') then
                  begin
                    inc(x);
                    c[x]:=b[y-1];
                    inc(oo);
                  end;
                dec(y);
                b[y]:=b[y+1];
              end;
          until (y>1)and(p[b[y]]>p[b[y-1]])or(y=1)
        else if b[y]=')' then
          begin
            dec(y);
            if b[y]<>'(' then
              repeat
                if b[y]<>')' then
                  begin
                    inc(x);
                    c[x]:=b[y];
                    inc(oo);
                  end;
                dec(y);
              until b[y]='(';
            dec(y);
          end;
     end;
  end;
while (y<>0)and(b[y]<>'(') do
  begin
   inc(x);
   c[x]:=b[y];
   dec(y);
   inc(oo);
  end;
dec(oo);
for t:=1 to x do
  if (c[t] in ['('..'^']) then write(c[t],' ')
  else write(a[t],' ');
writeln;
repeat
for t:=1 to x do
  if c[t] in ['('..'^'] then
    begin
      if c[t]='+' then a[t-2]:=a[t-1]+a[t-2]
      else if c[t]='-' then a[t-2]:=a[t-2]-a[t-1]
      else if c[t]='*' then a[t-2]:=a[t-1]*a[t-2]
      else if c[t]='/' then a[t-2]:=a[t-2]div a[t-1]
      else if c[t]='^' then
        begin
          j:=a[t-2];
          if a[t-1]-1>0 then
          for k:=1 to a[t-1]-1 do
            a[t-2]:=a[t-2]*j;
        end;
      for k:=t-1 to x do
        a[k]:=a[k+2];
      for k:=t-1 to x do
        c[k]:=c[k+2];
      x:=x-2;
      for k:=1 to x do
        if (c[k] in ['('..'^']) then write(c[k],' ')
        else write(a[k],' ');
      writeln;
      dec(oo);
      break;
   end;
until oo=-1;
close(input);
close(output);
end.