计算机表达式函数,一个新算法的表达式求值的函数

一个新算法的表达式求值的函数

来源:发布时间:2009-09-17

else

begin

for t:=i-1 downto 1 do

begin

if not is123(s[t]) then

begin

result:=strtofloat(copy(s,t+1,i-1-t));

exit;

end;

if t=1 then result:=strtofloat(leftstr(s,i-1));

end;

end;

end;

function rightnum(s:string;i:integer):double ;

var

t,L:integer;

begin

L:=length(s);

if s[i+1]=\'(\' then

begin

for t:=i+2 to L do

if s[t]=\')\' then

begin

result:=strtofloat(copy(s,i+2,t-i-2));

exit;

end;

end

else

begin

for t:=i+1 to L do

begin

if not is123(s[t]) then

begin

result:=strtofloat(copy(s,i+1,t-i-1));

exit;

end;

if t=L then result:=strtofloat(rightstr(s,L-i));

end;

end;

end;

/

function leftsigh(s:string;i:integer):integer ;

var

t,L:integer;

begin

L:=length(s);

if s[i-1]=\')\' then

begin

for t:=i-1 downto 1 do

if s[t]=\'(\' then

begin

result:=t;

exit;

end;

end

else

begin

for t:=i-1 downto 1 do

begin

if not is123(s[t]) then

begin

result:=t+1;

exit;

end;

if t=1 then result:=1;

end;

end;

end;

function rightsigh(s:string;i:integer):integer ;

var

t,L:integer;

begin

L:=length(s);

if s[i+1]=\'(\' then

begin

for t:=i+2 to L do

if s[t]=\')\' then

begin

result:=t;

exit;

end;

end

else

begin

for t:=i+1 to L do

begin

if not is123(s[t]) then

begin

result:=t-1;

exit;

end;

if t=L then result:=L;

end;

end;

end;

function nomulti(s:string):string ;

var

i,L,le,ri:integer;

j,k:double ;

begin

s:=nospace(s);

result:=s;

L:=length(s);

i:=firstsignex(s,\'*\');

if (i=0) or (s[i]<>\'*\') then exit;

le:=leftsigh(s,i);

j:=leftnum(s,i);

k:=rightnum(s,i);

ri:=rightsigh(s,i);

file://if ii

if j*k>=0 then

result:=nomulti(leftstr(s,le-1)+floattostr(j*k)+rightstr(s,L-ri))

else

result:=nomulti(leftstr(s,le-1)+\'(\'+floattostr(j*k)+\')\'+rightstr(s,L-ri))

end; function nodiv(s:string):string ;

var

i,L,le,ri:integer;

j,k:double ;

begin

s:=nospace(s);

result:=s;

L:=length(s);

i:=firstsignex(s,\'/\');

if (i=0) or (s[i]<>\'/\') then exit;

le:=leftsigh(s,i);

j:=leftnum(s,i);

k:=rightnum(s,i);

ri:=rightsigh(s,i);

if j/k>=0 then

result:=nodiv(leftstr(s,le-1)+floattostr(j/k)+rightstr(s,L-ri))

else

result:=nodiv(leftstr(s,le-1)+\'(\'+floattostr(j/k)+\')\'+rightstr(s,L-ri))

end;

function noadd(s:string):string ;

var

i,L,le,ri:integer;

j,k:double ;

begin

s:=nospace(s);

result:=s;

L:=length(s);

i:=firstsignex(s,\'+\');

if (i=0) or (s[i]<>\'+\') then exit;

le:=leftsigh(s,i);

j:=leftnum(s,i);

k:=rightnum(s,i);

ri:=rightsigh(s,i);

if j+k>=0 then

result:=noadd(leftstr(s,le-1)+floattostr(j+k)+rightstr(s,L-ri))

else

result:=noadd(leftstr(s,le-1)+\'(\'+floattostr(j+k)+\')\'+rightstr(s,L-ri))

end;

function nosub(s:string):string ;

var

i,L,le,ri:integer;

j,k:double ;

begin

s:=nospace(s);

result:=s;

L:=length(s);

i:=firstMinussignEX(s);

if (i=0) or (s[i]<>\'-\') then exit;

le:=leftsigh(s,i);

j:=leftnum(s,i);

k:=rightnum(s,i);

ri:=rightsigh(s,i);

if j-k>=0 then

result:=nosub(leftstr(s,le-1)+floattostr(j-k)+rightstr(s,L-ri))

else

result:=nosub(leftstr(s,le-1)+\'(\'+floattostr(j-k)+\')\'+rightstr(s,L-ri))

end;

function alltoone(s:string):string ;

begin

s:=nomulti(s);

s:=nodiv(s);

s:=noadd(s);

s:=nosub(s);

result:=s;

end;

function myexpress(s:string):string;

var

c,j,L:integer;

le,ri,al,substr,s0:string;

tryit:double;

begin

s:=nospace(s);

s0:=s;

L:=length(s);

if (s[1]<>\'(\') or (s[L]<>\')\') then

s:=\'(\'+s+\')\';

if (s[1]=\'(\') and (s[L]=\')\') and((s[2]=\'-\') or (isminus(s,L))) then

s:=\'(\'+s+\')\';

L:=length(s);

j:=firstJ(s);

c:=firstc(s,j);

if (j1) and (j>c) then

begin

substr:=copy(s,c+1,j-c-1);

file://le:=leftstr(s,c-1);

file://ri:= rightstr(s,L-j);

le:=leftstr(s,c-1);

le:=rightstr(le,length(le)-1);

ri:= rightstr(s,L-j);

ri:=leftstr(ri,length(ri)-1);

file://showmessage(substr);

al:=alltoone(substr);

file://showmessage(le+al+ri);

result:=myexpress(le+al+ri);

end

else

result:=alltoone(s0);

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

Edit2.Text:=myexpress(edit1.text);

end;

end.

【纠错】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值