记录编号 |
18551 |
评测结果 |
AAAAAAAWWA |
题目名称 |
表达式转换 |
最终得分 |
80 |
用户昵称 |
donny |
是否通过 |
未通过 |
代码语言 |
Pascal |
运行时间 |
0.002 s |
提交时间 |
2010-09-16 14:30:37 |
内存使用 |
0.17 MiB |
显示代码纯文本
program express(input,output);
var
q:array[#0..#255]of integer;
a:array[1..10000]of char;
b:array[0..10000]of char;
c:array[0..5000]of char;
shu:array[1..10000]of longint;
aa,bb,cc,i,j,k,l,m:longint;
f:set of char;
pan:boolean;
function sss(x,y:longint):longint;
var
i,j:longint;
begin
j:=x;
for i:=1 to y-1 do
j:=j*x;
exit(j);
end;
begin
assign(input,'express.in');
reset(input);
assign(output,'express.out');
rewrite(output);
aa:=0;
while (not eoln) do
begin
inc(aa);
read(a[aa]);
end;
q['$']:=0;
q['+']:=1;
q['-']:=1;
q['*']:=2;
q['/']:=2;
q['^']:=3;
q['(']:=0;
q[')']:=0;
f:=['+','-','*','/','^','(',')'];
bb:=0;
c[0]:='$';
cc:=0;
for i:=1 to aa do
if not (a[i] in f) then
begin
inc(bb);
b[bb]:=a[i];
end
else
begin
if (q[a[i]]>q[c[cc]])and(a[i]<>'(')and(a[i]<>')') then
begin
inc(cc);
c[cc]:=a[i];
end
else
if a[i]='(' then
begin
inc(cc);
c[cc]:=a[i];
end
else
begin
k:=cc;
pan:=true;
for j:=k downto 1 do
begin
if (c[j]='(') then begin cc:=j-1; pan:=false; break; end;
if (q[c[j]]<q[a[i]]) then begin cc:=j; pan:=false; break; end;
if c[j]<>')' then
begin
inc(bb);
b[bb]:=c[j];
end;
end;
if pan then
cc:=0;
if a[i]<>')' then
begin
inc(cc);
c[cc]:=a[i];
end;
end;
end;
for i:=cc downto 1 do
if c[i]<>')' then
begin
inc(bb);
b[bb]:=c[i];
end;
for i:=1 to bb do
if not(b[i] in f) then
case b[i] of
'0':shu[i]:=0;
'1':shu[i]:=1;
'2':shu[i]:=2;
'3':shu[i]:=3;
'4':shu[i]:=4;
'5':shu[i]:=5;
'6':shu[i]:=6;
'7':shu[i]:=7;
'8':shu[i]:=8;
'9':shu[i]:=9;
end
else
shu[i]:=maxint;
m:=bb;
for i:=1 to bb-1 do
write(b[i],' ');
writeln(b[bb]);
for i:=1 to (m div 2) do
begin
for j:=1 to bb do
if b[j] in f then
begin
case b[j] of
'+':begin shu[j-2]:=shu[j-2]+shu[j-1]; for k:=j-1 to bb-2 do begin shu[k]:=shu[k+2]; b[k]:=b[k+2]; end; bb:=bb-2; break; end;
'-':begin shu[j-2]:=shu[j-2]-shu[j-1]; for k:=j-1 to bb-2 do begin shu[k]:=shu[k+2]; b[k]:=b[k+2]; end; bb:=bb-2; break; end;
'*':begin shu[j-2]:=shu[j-2]*shu[j-1]; for k:=j-1 to bb-2 do begin shu[k]:=shu[k+2]; b[k]:=b[k+2]; end; bb:=bb-2; break; end;
'/':begin shu[j-2]:=shu[j-2] div shu[j-1]; for k:=j-1 to bb-2 do begin shu[k]:=shu[k+2]; b[k]:=b[k+2]; end; bb:=bb-2; break; end;
'^':begin shu[j-2]:=sss(shu[j-2],shu[j-1]); for k:=j-1 to bb-2 do begin shu[k]:=shu[k+2]; b[k]:=b[k+2]; end; bb:=bb-2; break; end;
end;
end;
for j:=1 to bb-1 do
if b[j]in f then
write(b[j],' ')
else
write(shu[j],' ');
if b[bb]in f then
writeln(b[bb])
else
writeln(shu[bb]);
end;
close(input);
close(output);
end.