讨论 / 一个普通保护错误。。。没数组。没递归。
xf243658 2010-09-20 06:38:00
点我顶贴 收藏 删除
program RQEX_344;

var

s:array[1..3] of string;

st:string;

ch:char;

i,j,k,n,h1,h2:longint;

aa,bb:real;

procedure proce(tt:string;var sum:longint);

var

a,b,x:longint;ttt:string;

begin

sum:=0;

ttt:='';a:=0;

if tt[1]<>'-' then

begin

repeat

inc(a);

if tt[a] in ['0'..'9'] then ttt:=ttt+tt[a];

until (a=length(tt)) or (tt[a] in ['-','+']);

val(ttt,sum);

ttt:='';

end;

if tt[1]='-' then

begin

inc(a);

repeat

inc(a);

if tt[a] in ['0'..'9'] then ttt:=ttt+tt[a];

until (a=length(tt)) or (tt[a] in ['-','+']);

val(ttt,sum);

sum:=0-sum;

ttt:='';

end;

repeat

b:=a;

repeat

inc(a);

if tt[a] in ['0'..'9'] then ttt:=ttt+tt[a];

until (a=length(tt)) or (tt[a] in ['-','+']);

val(ttt,x);

ttt:='';

if tt[b]='+' then sum:=sum+x;

if tt[b]='-' then sum:=sum-x;

until a=length(tt);

end;

begin

readln(st);

n:=length(st);

for i:=1 to n do

begin

if st[i] in ['a'..'z'] then ch:=st[i];

if st[i]='=' then

begin

s[1]:=copy(st,1,i-1);

delete(st,1,i);

end;

end;

s[2]:=st;

i:=pos(ch,s[1]);

while (pos(ch,s[1])<>0)and(i>0) do

begin

i:=pos(ch,s[1]);

repeat

dec(i); inc(k);

until (i=1)or(s[1][i] in ['-','+']);

s[3]:=s[3]+copy(s[1],i,k);

if (k=1)and(s[1][i] in ['-','+']) then s[3]:=s[3]+'1';

delete(s[1],i,k+1);

k:=0;

end;

i:=pos(ch,s[2]);

while (pos(ch,s[2])<>0)and(i>0) do

begin

i:=pos(ch,s[2]);

repeat

dec(i); inc(k);

until (i=1)or(s[2][i] in ['-','+']);

if s[2][i]<>'-' then if s[2][i]='+' then begin s[2][i]:='-';s[3]:=s[3]+copy(s[2],i,k);end

else s[3]:=s[3]+'-'+copy(s[2],i,k)

else begin s[2][i]:='+';s[3]:=s[3]+copy(s[2],i,k); end;

if (k=1)and(s[2][i] in ['-','+']) then s[3]:=s[3]+'1';

delete(s[2],i,k+1);

k:=0;

end;

n:=length(s[1]);

for i:=1 to n do

begin

if s[1][i]='-' then s[1][i]:='+'

else if s[1][i]='+' then s[1][i]:='-';

end;

if s[1][1] in ['1'..'9'] then s[1]:='-'+s[1];

s[2]:=s[2]+s[1];

proce(s[2],h1);

proce(s[3],h2);

aa:=h1/h2;

if aa>-0.0005 then aa:=abs(aa);

writeln(ch,'=',aa:0:3);

readln

end.

查看更多回复
提交回复