比赛 |
练习赛01 |
评测结果 |
AAAAWWAAWE |
题目名称 |
表达式转换 |
最终得分 |
60 |
用户昵称 |
Achilles |
运行时间 |
0.000 s |
代码语言 |
Pascal |
内存使用 |
0.00 MiB |
提交时间 |
2009-09-04 21:22:46 |
显示代码纯文本
program express;
var
s:string;
t,n,j,p2,i,temp:integer;
sz:array[1..300]of record
size,num:integer;
f:char;
end;
function p(s:string):string;
var
i,t2:integer;
s1,s2:string;
begin
while (s[1]='(')and(s[length(s)]=')') do
begin
t:=0;
p2:=0;
for j:=2 to length(s)-1 do
begin
if s[j]='(' then t:=t+1;
if s[j]=')' then t:=t-1;
if t<0 then begin
p2:=1;
break;
end;
end;
if p2=0 then begin
delete(s,1,1);
delete(s,length(s),1);
end
else break;
end;
t:=0;
t2:=0;
for i:=length(s) downto 1 do
begin
if (s[i]in['+','-'])and(t=0) then begin
s1:=copy(s,1,i-1);
s1:=p(s1);
s2:=copy(s,i+1,length(s)-i);
s2:=p(s2);
s:=s1+s2+s[i];
t2:=1;
break;
end;
if s[i]=')' then t:=t+1;
if s[i]='(' then t:=t-1;
end;
if t2=0 then begin
t:=0;
for i:=length(s) downto 1 do
begin
if (s[i]in['*','/'])and(t=0) then begin
s1:=copy(s,1,i-1);
s1:=p(s1);
s2:=copy(s,i+1,length(s)-i);
s2:=p(s2);
s:=s1+s2+s[i];
t2:=1;
break;
end;
if s[i]=')' then t:=t+1;
if s[i]='(' then t:=t-1;
end;
end;
if t2=0 then begin
t:=0;
for i:=length(s) downto 1 do
begin
if (s[i]='^')and(t=0) then begin
s1:=copy(s,1,i-1);
s1:=p(s1);
s2:=copy(s,i+1,length(s)-i);
s2:=p(s2);
s:=s1+s2+s[i];
t2:=1;
break;
end;
if s[i]=')' then t:=t+1;
if s[i]='(' then t:=t-1;
end;
end;
p:=s;
end;
begin
assign(input,'express.in');
assign(output,'express.out');
reset(input);
rewrite(output);
readln(s);
s:=p(s);
for i:=1 to length(s) do
if s[i] in['0'..'9'] then begin
sz[i].size:=1;
sz[i].num:=ord(s[i])-48;
end
else begin
sz[i].size:=2;
sz[i].f:=s[i];
end;
n:=length(s);
for i:=1 to length(s)-1 do
write(s[i],' ');
writeln(s[length(s)]);
while n>1 do begin
for i:=1 to n do
if sz[i].size=2 then begin
if sz[i].f='+' then begin
sz[i-2].size:=1;
sz[i-2].num:=sz[i-2].num+sz[i-1].num;
for j:=i+1 to n do
sz[j-2]:=sz[j];
n:=n-2;
break;
end;
if sz[i].f='-' then begin
sz[i-2].size:=1;
sz[i-2].num:=sz[i-2].num-sz[i-1].num;
for j:=i+1 to n do
sz[j-2]:=sz[j];
n:=n-2;
break;
end;
if sz[i].f='*' then begin
sz[i-2].size:=1;
sz[i-2].num:=sz[i-2].num*sz[i-1].num;
for j:=i+1 to n do
sz[j-2]:=sz[j];
n:=n-2;
break;
end;
if sz[i].f='/' then begin
sz[i-2].size:=1;
sz[i-2].num:=sz[i-2].num div sz[i-1].num;
for j:=i+1 to n do
sz[j-2]:=sz[j];
n:=n-2;
break;
end;
if sz[i].f='^' then begin
temp:=sz[i-2].num;
for j:=1 to sz[i-1].num-1 do
sz[i-2].num:=sz[i-2].num*temp;
for j:=i+1 to n do
sz[j-2]:=sz[j];
n:=n-2;
break;
end;
end;
for i:=1 to n-1 do
if sz[i].size=1 then write(sz[i].num,' ') else write(sz[i].f,' ');
if sz[n].size=1 then writeln(sz[n].num,' ') else writeln(sz[n].f,' ');
end;
close(input);
close(output);
end.