level 11
祝世界和平º
楼主
uses crt;
const max=200;
var
s:string;
k:longint;
function plus(n1,n2:string):string;
var
a,b,d:array[1..max] of integer;
lena,lenb,lenc,i,x:longint;
q:string;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
lena:=length(n1);
for i:=1 to lena do
a[lena-i+1]:=ord(n1[i])-ord('0');
lenb:=length(n2);
for i:=1 to lenb do
b[lenb-i+1]:=ord(n2[i])-ord('0');
x:=0;
if lena>lenb then
lenc:=lena
else
lenc:=lenb;
For i:=1 to lenc do
begin
d[i]:=a[i]+b[i]+x;
x:=d[i] div 10;
d[i]:=d[i] mod 10;
end;
if x>0 then
inc(lenc);
d[i+1]:=x;
plus:='';
for i:=lenc downto 1 do
begin
str(d[i],q);
plus:=plus+q;
end;
plus:=plus;
end;
FUNCTION minus(n1,n2:string):string;
var
a,b:array[1..max] of longint;
n,q:string;
lena,lenb,lenc,i,j:longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
minus:='';
if (length(n1)<length(n2)) or (length(n1)=length(n2)) and (n1<n2) then
begin
n:=n1;
n1:=n2;
n2:=n;
minus:='-';
end;
lena:=length(n1);
lenb:=length(n2);
for i:=1 to lena do
a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do
b[lenb-i+1]:=ord(n2[i])-ord('0');
i:=1;
while i<=lena do
begin
if a[i]<b[i] then
begin
a[i]:=a[i]+10;
a[i+1]:=a[i+1]-1;
end;
a[i]:=a[i]-b[i];
i:=i+1;
end;
while(a[i]=0) and (i>1) do
dec(i);
for j:=i downto 1 do
begin
str(a[j],q);
minus:=minus+q;
end;
end;
FUNCTION times(n1,n2:string):string;
var
k1,k2,i,j,n0,h:longint;
code:integer;
b:boolean;
answer:array[0..max]of longint;
q:string;
begin
if(n1='0')or(n2='0')then
times:='0'
else
begin
n0:=0;
fillchar(answer,sizeof(answer),0);
for i:=1 to length(n1) do
for j:=1 to length(n2) do
begin
val(n1[i],k1,code);
val(n2[j],k2,code);
answer[i+j-1]:=k1*k2+answer[i+j-1];
for h:=i+j-1 downto 1 do
begin
answer[h-1]:=answer[h-1]+(answer[h] div 10);
answer[h]:=answer[h] mod 10;
end;
end;
b:=false;
times:='';
if answer[0]>0 then
begin
str(answer[0],q);
times:=times+q;
end;
for j:=1 to length(n1)+length(n2)-1 do
begin
str(answer[j],q);
times:=times+q;
end;
for i:=1 to n0 do
times:=times+'0';
end;
end;
FUNCTION division(n1,n2:string):string;
var
a,d:array[1..max] of 0..9;
lena,i,j,x,b,j1:longint;
code:integer;
q,y:string;
begin
lena:=length(n1);
for i:=1 to lena do
a[i]:=ord(n1[i])-ord('0');
for i:=1 to 5 do
a[lena+i]:=0;
lena:=lena+5;
val(n2,b,code);
if b=0 then begin
writeln('Error:Dividing 0 or dividing number is too big.');
exit('--');
end;
x:=0;
for i:=1 to lena do
begin
d[i]:=(x*10+a[i]) div b;
x:=(x*10+a[i]) mod b;
end;
j:=1;
while(d[j]=0)and(j<lena-5)do
inc(j);
division:='';
for i:=j to lena-5 do
begin
str(d[i],q);
division:=division+q;
end;
{[}j1:=lena;
while(d[j1]=0)and(j1>lena-5)do
dec(j1);
if j1>lena-5 then
division:=division+'.';
for i:=lena-4 to j1 do
begin
str(d[i],q);
division:=division+q;
end;{]}
{str(x,y);
if x>0 then
division:=division+'......'+y; }
end;
procedure search(s:string;c:longint;var s1,s2:string;var a,b:longint);
var pc,sc:longint;
begin
for pc:= c-1 downto 1 do
if ((s[pc]<'0') or (s[pc]>'9'))
and (s[pc]<>'x') then break;
if pc=1 then dec(pc);
for sc:= c+1 to length(s) do
if ((s[sc]<'0') or (s[sc]>'9'))
and (s[sc]<>'x') then break;
if sc=length(s) then inc(sc);
s1:=copy(s,pc+1,c-pc-1);
if s1[1]='x' then s1[1]:='-';
s2:=copy(s,c+1,sc-c-1);
if s2[1]='x' then s2[1]:='-';
a:=pc;b:=sc;
end;
procedure deal(var s:string);
var
pk,sk:longint;
pc,c,sc:longint;
an,s1,s2,lins:string;
begin
if s[1]='-' then s[1]:='x';
if pos('(',s)>0 then
for pk:= length(s) downto 1 do
if s[pk]='(' then begin
for sk:= pk+1 to length(s) do
if s[sk]=')' then break;
if s[pk+1]='-' then begin
s[pk+1]:='x';
delete(s,pk,1);
delete(s,sk,1);
end else begin
lins:=copy(s,pk+1,sk-pk-1);
deal(lins);
delete(s,pk,sk-pk+1);
insert(lins,s,pk);
end;
end;
{chengfang}
c:=pos('*',s);
pc:=pos('/',s);
while not((c=0) and (pc=0)) do
begin
if (pc<c) and (pc>0) or (c=0) then c:=pc;
search(s,c,s1,s2,pc,sc);
case s[c] of
'*':begin
if ((s1[1]='-')and (s2[1]='-'))
or ((s1[1]<>'-') and (s2[1]<>'-'))then
begin
if s1[1]='-' then begin
delete(s1,1,1);
delete(s2,1,1);
end;
an:=times(s1,s2);
end else begin
if s1[1]='-' then delete(s1,1,1)
else delete(s2,1,1);
an:='-'+times(s1,s2);
end;
end;
'/':begin
if ((s1[1]='-')and (s2[1]='-'))
or ((s1[1]<>'-') and (s2[1]<>'-'))then
begin
if s1[1]='-' then begin
delete(s1,1,1);
delete(s2,1,1);
end;
an:=division(s1,s2);
end else begin
if s1[1]='-' then delete(s1,1,1)
else delete(s2,1,1);
an:='-'+division(s1,s2);
end;
if pos('--',an)>0 then an:='';
end;
end;
delete(s,pc+1,sc-pc-1);
insert(an,s,pc+1);
c:=pos('*',s);
pc:=pos('/',s);
end;
c:=pos('+',s);
pc:=pos('-',s);
while not((c=0) and (pc<=1)) do
begin
if (pc<c) and (pc>0) or (c=0) then c:=pc;
search(s,c,s1,s2,pc,sc);
case s[c] of
'+':begin
if (s1[1]='-')and (s2[1]='-') then
begin
delete(s1,1,1);
delete(s2,1,1);
an:='-'+plus(s1,s2);
end
else
if s1[1]='-' then
begin
delete(s1,1,1);
an:=minus(s2,s1);
end
else
if s2[1]='-' then
begin
delete(s2,1,1);
an:=minus(s1,s2);
end
else an:=plus(s1,s2);
end;
'-':begin
if (s1[1]='-')and (s2[1]='-') then
begin
delete(s1,1,1);
delete(s2,1,1);
an:=minus(s2,s1);
end
else if (s1[1]='-') then
begin
delete(s1,1,1);
an:='-'+plus(s1,s2);
end
else if (s2[1]='-') then
begin
delete(s2,1,1);
an:=plus(s1,s2);
end
else an:=minus(s1,s2);
end;
end;
delete(s,pc+1,sc-pc-1);
insert(an,s,pc+1);
c:=pos('+',s);
pc:=pos('-',s);
end;
if s[1]='-' then s[1]:='x';
end;
begin //main
clrscr;
while true do
begin
readln(s);
if (s='clear') or (s='cls') or (s='clrscr') then clrscr
else
if (s='exit') then exit
else begin
writeln('=......');
deal(s);
if s[1]='x' then s[1]:='-';
if s<>'' then writeln('=',s);
writeln;
writeln;
writeln;
end;
end;
end.
2013年11月10日 14点11分
1
const max=200;
var
s:string;
k:longint;
function plus(n1,n2:string):string;
var
a,b,d:array[1..max] of integer;
lena,lenb,lenc,i,x:longint;
q:string;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
lena:=length(n1);
for i:=1 to lena do
a[lena-i+1]:=ord(n1[i])-ord('0');
lenb:=length(n2);
for i:=1 to lenb do
b[lenb-i+1]:=ord(n2[i])-ord('0');
x:=0;
if lena>lenb then
lenc:=lena
else
lenc:=lenb;
For i:=1 to lenc do
begin
d[i]:=a[i]+b[i]+x;
x:=d[i] div 10;
d[i]:=d[i] mod 10;
end;
if x>0 then
inc(lenc);
d[i+1]:=x;
plus:='';
for i:=lenc downto 1 do
begin
str(d[i],q);
plus:=plus+q;
end;
plus:=plus;
end;
FUNCTION minus(n1,n2:string):string;
var
a,b:array[1..max] of longint;
n,q:string;
lena,lenb,lenc,i,j:longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
minus:='';
if (length(n1)<length(n2)) or (length(n1)=length(n2)) and (n1<n2) then
begin
n:=n1;
n1:=n2;
n2:=n;
minus:='-';
end;
lena:=length(n1);
lenb:=length(n2);
for i:=1 to lena do
a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do
b[lenb-i+1]:=ord(n2[i])-ord('0');
i:=1;
while i<=lena do
begin
if a[i]<b[i] then
begin
a[i]:=a[i]+10;
a[i+1]:=a[i+1]-1;
end;
a[i]:=a[i]-b[i];
i:=i+1;
end;
while(a[i]=0) and (i>1) do
dec(i);
for j:=i downto 1 do
begin
str(a[j],q);
minus:=minus+q;
end;
end;
FUNCTION times(n1,n2:string):string;
var
k1,k2,i,j,n0,h:longint;
code:integer;
b:boolean;
answer:array[0..max]of longint;
q:string;
begin
if(n1='0')or(n2='0')then
times:='0'
else
begin
n0:=0;
fillchar(answer,sizeof(answer),0);
for i:=1 to length(n1) do
for j:=1 to length(n2) do
begin
val(n1[i],k1,code);
val(n2[j],k2,code);
answer[i+j-1]:=k1*k2+answer[i+j-1];
for h:=i+j-1 downto 1 do
begin
answer[h-1]:=answer[h-1]+(answer[h] div 10);
answer[h]:=answer[h] mod 10;
end;
end;
b:=false;
times:='';
if answer[0]>0 then
begin
str(answer[0],q);
times:=times+q;
end;
for j:=1 to length(n1)+length(n2)-1 do
begin
str(answer[j],q);
times:=times+q;
end;
for i:=1 to n0 do
times:=times+'0';
end;
end;
FUNCTION division(n1,n2:string):string;
var
a,d:array[1..max] of 0..9;
lena,i,j,x,b,j1:longint;
code:integer;
q,y:string;
begin
lena:=length(n1);
for i:=1 to lena do
a[i]:=ord(n1[i])-ord('0');
for i:=1 to 5 do
a[lena+i]:=0;
lena:=lena+5;
val(n2,b,code);
if b=0 then begin
writeln('Error:Dividing 0 or dividing number is too big.');
exit('--');
end;
x:=0;
for i:=1 to lena do
begin
d[i]:=(x*10+a[i]) div b;
x:=(x*10+a[i]) mod b;
end;
j:=1;
while(d[j]=0)and(j<lena-5)do
inc(j);
division:='';
for i:=j to lena-5 do
begin
str(d[i],q);
division:=division+q;
end;
{[}j1:=lena;
while(d[j1]=0)and(j1>lena-5)do
dec(j1);
if j1>lena-5 then
division:=division+'.';
for i:=lena-4 to j1 do
begin
str(d[i],q);
division:=division+q;
end;{]}
{str(x,y);
if x>0 then
division:=division+'......'+y; }
end;
procedure search(s:string;c:longint;var s1,s2:string;var a,b:longint);
var pc,sc:longint;
begin
for pc:= c-1 downto 1 do
if ((s[pc]<'0') or (s[pc]>'9'))
and (s[pc]<>'x') then break;
if pc=1 then dec(pc);
for sc:= c+1 to length(s) do
if ((s[sc]<'0') or (s[sc]>'9'))
and (s[sc]<>'x') then break;
if sc=length(s) then inc(sc);
s1:=copy(s,pc+1,c-pc-1);
if s1[1]='x' then s1[1]:='-';
s2:=copy(s,c+1,sc-c-1);
if s2[1]='x' then s2[1]:='-';
a:=pc;b:=sc;
end;
procedure deal(var s:string);
var
pk,sk:longint;
pc,c,sc:longint;
an,s1,s2,lins:string;
begin
if s[1]='-' then s[1]:='x';
if pos('(',s)>0 then
for pk:= length(s) downto 1 do
if s[pk]='(' then begin
for sk:= pk+1 to length(s) do
if s[sk]=')' then break;
if s[pk+1]='-' then begin
s[pk+1]:='x';
delete(s,pk,1);
delete(s,sk,1);
end else begin
lins:=copy(s,pk+1,sk-pk-1);
deal(lins);
delete(s,pk,sk-pk+1);
insert(lins,s,pk);
end;
end;
{chengfang}
c:=pos('*',s);
pc:=pos('/',s);
while not((c=0) and (pc=0)) do
begin
if (pc<c) and (pc>0) or (c=0) then c:=pc;
search(s,c,s1,s2,pc,sc);
case s[c] of
'*':begin
if ((s1[1]='-')and (s2[1]='-'))
or ((s1[1]<>'-') and (s2[1]<>'-'))then
begin
if s1[1]='-' then begin
delete(s1,1,1);
delete(s2,1,1);
end;
an:=times(s1,s2);
end else begin
if s1[1]='-' then delete(s1,1,1)
else delete(s2,1,1);
an:='-'+times(s1,s2);
end;
end;
'/':begin
if ((s1[1]='-')and (s2[1]='-'))
or ((s1[1]<>'-') and (s2[1]<>'-'))then
begin
if s1[1]='-' then begin
delete(s1,1,1);
delete(s2,1,1);
end;
an:=division(s1,s2);
end else begin
if s1[1]='-' then delete(s1,1,1)
else delete(s2,1,1);
an:='-'+division(s1,s2);
end;
if pos('--',an)>0 then an:='';
end;
end;
delete(s,pc+1,sc-pc-1);
insert(an,s,pc+1);
c:=pos('*',s);
pc:=pos('/',s);
end;
c:=pos('+',s);
pc:=pos('-',s);
while not((c=0) and (pc<=1)) do
begin
if (pc<c) and (pc>0) or (c=0) then c:=pc;
search(s,c,s1,s2,pc,sc);
case s[c] of
'+':begin
if (s1[1]='-')and (s2[1]='-') then
begin
delete(s1,1,1);
delete(s2,1,1);
an:='-'+plus(s1,s2);
end
else
if s1[1]='-' then
begin
delete(s1,1,1);
an:=minus(s2,s1);
end
else
if s2[1]='-' then
begin
delete(s2,1,1);
an:=minus(s1,s2);
end
else an:=plus(s1,s2);
end;
'-':begin
if (s1[1]='-')and (s2[1]='-') then
begin
delete(s1,1,1);
delete(s2,1,1);
an:=minus(s2,s1);
end
else if (s1[1]='-') then
begin
delete(s1,1,1);
an:='-'+plus(s1,s2);
end
else if (s2[1]='-') then
begin
delete(s2,1,1);
an:=plus(s1,s2);
end
else an:=minus(s1,s2);
end;
end;
delete(s,pc+1,sc-pc-1);
insert(an,s,pc+1);
c:=pos('+',s);
pc:=pos('-',s);
end;
if s[1]='-' then s[1]:='x';
end;
begin //main
clrscr;
while true do
begin
readln(s);
if (s='clear') or (s='cls') or (s='clrscr') then clrscr
else
if (s='exit') then exit
else begin
writeln('=......');
deal(s);
if s[1]='x' then s[1]:='-';
if s<>'' then writeln('=',s);
writeln;
writeln;
writeln;
end;
end;
end.