## 数据要求,得用ansistring.
###比较冗长的代码,但或许逻辑比较清晰
pascal:
const
MAXL=100000;
var
a:array[1..2*MAXL] of longint;// numbers‘ stack
b:array[1..MAXL] of char;// operaters' stack
top1,top2:longint;
s:ansistring;//expression
function pop:longint;
begin
pop:=a[top1];
dec(top1);
end;
procedure push(x:longint);
begin
inc(top1);
a[top1]:=x;
end;
procedure printf;//output
var s1:string;t,code:longint;
begin
str(a[1],s1);
s1:=copy(s1,length(s1)-3,4);
val(s1,t,code);
write(t);
end;
function call(x,y:longint;ch:char):longint;//calculate
var
s1,s2:string;
w1,w2:longint;
code:longint;
begin
str(x,s1);
str(y,s2);
if length(s1)>=4 then
s1:=copy(s1,length(s1)-3,4);
if length(s2)>=4 then
s2:=copy(s2,length(s2)-3,4);
val(s1,w1,code);
val(s2,w2,code);
if ch='*' then begin call:=w1*w2;exit; end;
if ch='+' then begin call:=w1+w2;exit; end;
end;
function can(a,b:char):boolean;//To determine the order of operations
begin
if (a='+')and(b='*') then exit(true);
if (a='@') then exit (true);
exit(false);
end;
procedure work(s:ansistring);
var
i,l,k,x,y,res:longint;
begin
i:=1;l:=length(s);
while (b[1]<>'@') do
begin
while (top2>1)and(can(b[top2],b[top2-1])) do
begin
x:=pop;
y:=pop;
res:=call(x,y,b[top2-1]);
push(res);
dec(top2);
b[top2]:=b[top2+1];
end;
if(b[1]='@') then break;
if (s[i] in ['*','+','@']) then
begin inc(top2);b[top2]:=s[i];inc(i) end
else
begin
k:=0;// number
while (not(s[i] in ['*','+','@'])) do
begin
k:=k*10+(ord(s[i])-ord('0'));
inc(i);
end;
push(k);
end;// end if
end;// end while
end;
procedure init;//input
begin
top1:=0;top2:=0;
readln(s);
s:=s+'@';
work(s);
printf;
end;
begin
init;
end.
非常希望rqnoj能改进一下,能在讨论中使用markdown,这样能使代码变得好看些。。。
查看该题目
发布该题题解
由 vook 在 2014-02-06 22:53:45 提交,有效耗时202ms,如果您发现任何评测错误,请点击这里提交
c:char;
begin
k:=0;i:=1;j:=0;
while not eoln do
begin
read(c);
case c of
'+':begin k:=(k+i*j) mod 10000;i:=1;j:=0;end;
'*':begin i:=i*j mod 10000;j:=0; end;
'0'..'9':begin j:=(j*10+ord(c)-48) mod 10000 end;
end; end;
writeln((k+i*j) mod 10000);
end.
hehe