a017.
五則運算
| From: [58.19.178.130] |
發表日期
:
2008-11-11 00:06
var n:array[1..100000]of int64;
f:array[1..100000]of char;
fff:array[1..255]of byte;
nt,ft,t:int64;
i:longint;
s:string;
ff,nn:set of char;
function min(a,b:int64):int64;
begin
if(a>b)then min:=b
else min:=a;
end;
function next(c:char):int64;
var e:int64;
begin
e:=1;
while(e<length(s))and(s[e+1]in nn)do inc(e);
while(c='d')and(e<length(s))and(s[e+1]=' ')do inc(e);
next:=e;
end;
procedure cal(c:char);
begin
repeat
if(c=')')and(f[ft]=chr(ord('(')-1))then
begin dec(ft);delete(s,1,min(2,length(s)));exit;end;
case f[ft]of
'+':n[nt-1]:=n[nt-1]+n[nt];
'-':n[nt-1]:=n[nt-1]-n[nt];
'*':n[nt-1]:=n[nt-1]*n[nt];
'/':n[nt-1]:=n[nt-1]div n[nt];
'%':n[nt-1]:=n[nt-1]mod n[nt];
end;
dec(nt);dec(ft);
until(fff[ord(c)]>fff[ord(f[ft])])or(ft=0);
inc(ft);f[ft]:=c;delete(s,1,min(2,length(s)));
end;
begin
ff:=['+','-','*','/','%','(',')'];
nn:=['0'..'9'];
fff[ord('+')]:=2;fff[ord('-')]:=2;
fff[ord('*')]:=5;fff[ord('/')]:=5;fff[ord('%')]:=5;
fff[ord('(')]:=9;fff[ord(')')]:=1;
fff[ord('(')-1]:=1;fff[ord(',')]:=0;
repeat
readln(s);
ft:=0;nt:=0;
repeat
if(s[1]in ff)then
if(s[1]='(')then
begin inc(ft);f[ft]:=chr(ord('(')-1);delete(s,1,min(2,length(s)));end
else if(fff[ord(s[1])]>fff[ord(f[ft])])or(ft=0)then
begin inc(ft);f[ft]:=s[1];delete(s,1,min(2,length(s)));end
else cal(s[1])
else begin val(copy(s,1,next('n')),t,i);inc(nt);n[nt]:=t;
delete(s,1,next('d'));end;
until s='';
cal(',');
writeln(n[1]);
until eof;
end.
爲什麽會有216錯誤