讨论 / PID 733 expr
sbxiaobai7 2014-03-01 19:56:39
点我顶贴 收藏 删除
# 栈的操作 只要后四位计算

## 数据要求,得用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.

#1 sbxiaobai7@2014-03-01 20:00:41
回复 删除
原来不能用markdown语法。。。

非常希望rqnoj能改进一下,能在讨论中使用markdown,这样能使代码变得好看些。。。

#2 sbxiaobai7@2014-03-01 20:02:43
回复 删除
而且点了题解选项还是变成了讨论。。。
#3 vook@2014-03-03 02:15:59
回复 删除
有效用时多少秒啊?

#4 sbxiaobai7@2014-03-07 03:16:58
回复 删除
327ms
#5 vook@2014-03-12 03:36:50
回复 删除
SID1040215 / 表达式求值 / ACCEPTED

查看该题目

发布该题题解

由 vook 在 2014-02-06 22:53:45 提交,有效耗时202ms,如果您发现任何评测错误,请点击这里提交

#6 zyf111@2015-11-02 04:14:34
回复 删除
var i,j,k,l,n,m:longint;

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

查看更多回复
提交回复