3
336764
Unregistered / Unconfirmed
GUEST, unregistred user!
下边是文章原文,能成功编译成dll,但是我不会在程序中使用。。。真失败。帮忙一下。谢谢 。<br><br>一个DLL,用来接收函数表达式和变量值,并返回函数值<br>如: result:= fun('sin(x)*exp(x)',0.1);<br><br><br>library fun;<br>uses<br> sharemem,<br> SysUtils,<br> StdCtrls,<br> math;<br>function rapidstrtofun(s:string;x:double):double;<br>var<br> posi:array[1..50]of integer;<br> data:array[1..50]of double;<br> mulsign,plusign:array[1..50]of char;<br> muldev,addmin:array[1..50]of integer;<br> {point to the position of data,index of data}<br> n,n2,n3:integer;<br> len,i,j:integer;<br> s1:string;<br>begin<br> s:=lowercase(s);<br> len:=length(s);<br> if len=1 then<br> begin<br> if s='x' then begin result:=x;exit;end;<br> result:=strtofloat(s);exit;<br> end;<br> n:=0;{number of operater=n2+n3}<br> n2:=0;{number of * and /}<br> n3:=0;{number of + and -}<br> {find out the operater}<br> for i:=2 to len do begin<br> case s of<br> '*':<br> begin<br> inc;inc(n2);mulsign[n2]:='*';muldev[n2]:=n;<br> posi[n]:=i;<br> end;<br> '/':<br> begin<br> inc;inc(n2);mulsign[n2]:='/';muldev[n2]:=n;<br> posi[n]:=i;<br> end;<br> '+':<br> if (s[i-1]<>'e')=true then<br> begin<br> inc;inc(n3);plusign[n3]:='+';addmin[n3]:=n;<br> posi[n]:=i;<br> end;<br> '-':<br> if (s[i-1]<>'e')=true then<br> begin<br> inc;inc(n3);plusign[n3]:='-';addmin[n3]:=n;<br> posi[n]:=i;<br> end;<br> end;<br> end;<br> {pick out the data}<br> if n=0 then begin result:=strtofloat(s);exit;end;<br> s1:='';<br> for i:=1 to posi[1]-1 do begin<br> s1:=s1+s;<br> end;<br><br> if s1='x' then data[1]:=x<br> else if s1='-x' then data[1]:=-x<br> else data[1]:=strtofloat(s1);<br><br> for i:=1 to n-1 do begin<br> s1:='';<br> for j:=posi+1 to posi[i+1]-1 do begin<br> s1:=s1+s[j];<br> end;<br> if s1='x' then data:=x<br> else data[i+1]:=strtofloat(s1);<br> end;<br> s1:='';<br> for i:=posi[n]+1 to len do begin<br> s1:=s1+s;<br> end;<br> if s1='x' then data[n+1]:=x<br> else data[n+1]:=strtofloat(s1);<br> {calculate * and /}<br> for i:=1 to n2 do begin<br> if mulsign='*' then<br> data[muldev+1]:=data[muldev]*data[muldev+1]<br> else<br> data[muldev+1]:=data[muldev]/data[muldev+1];<br> end;<br> for i:=1 to n3-1 do begin<br> if plusign='+' then<br> data[addmin[1]]:=data[addmin[1]]+data[addmin[i+1]]<br> else<br> data[addmin[1]]:=data[addmin[1]]-data[addmin[i+1]];<br> end;<br> if n3=0 then result:=data[n2+1]<br> else result:=data[1]+data[n+1];<br>end;<br><br>function brackets(s:string;x:double):double;register;<br>var<br> s0,s1:string;<br> time:integer;<br> i,j,k,order,l0:integer;<br> med:double;<br> med1,value2:double;<br> value1:integer;<br> count:array[1..1000]of integer;<br> judge:boolean;<br> Label line1;<br>begin<br> line1: order:=pos('((',s);<br> if(order>0)then begin<br> insert('1*',s,order+1);<br> goto line1;<br> end;<br> time:=0;<br> s0:=s;<br> judge:=false;<br> l0:=length(s);<br> for i:=1 to l0 do<br> begin<br> if(s='(')then time:=time+1;<br> end;<br> if(time=0)then judge:=true;<br> if(time>0)then<br> begin<br> for i:=1 to time do<br> begin<br> j:=0;<br> for order:=1 to l0 do<br> begin<br> if((s[order]='(')=true)or((s[order]=')')=true)then<br> begin<br> j:=j+1;<br> count[j]:=order;<br> end;<br> end;<br> for k:=1 to j-1 do<br> begin<br> if(((s[count[k]]='(')=true)and<br> ((s[count[k+1]]=')')=true))then<br> begin<br> order:=count[k]; {keep the location of ( }<br> break;<br> end;<br> end;<br> if(order=1)then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> s1:=floattostr(med);<br> s:=s0;<br> delete(s,count[k],count[k+1]-count[k]+1);<br> insert(s1,s,count[k]);<br> s0:=s;<br> l0:=length(s);<br> end<br> else<br> begin<br> if(((s[count[k]-1]='*')=true)or((s[count[k]-1]='/')=true)or<br> ((s[count[k]-1]='+')=true)or((s[count[k]-1]='-')=true))then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k],count[k+1]-count[k]+1);<br> insert(s1,s,count[k]);<br> s0:=s;<br> l0:=length(s);<br> continue;<br> end;<br><br> if(s[count[k]-1]='p')then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> med:=exp(med);<br> s1:=floattostr(med);<br> s:=s0;<br> delete(s,count[k]-3,count[k+1]-count[k]+4);<br> insert(s1,s,count[k]-3);<br> s0:=s;<br> l0:=length(s);<br> continue;<br> end;<br><br> if(s[count[k]-1]='g')then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> s:=s0;<br> if(s[count[k]-2]='l')then begin<br> med:=Log10(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-2,count[k+1]-count[k]+3);<br> insert(s1,s,count[k]-2);<br> s0:=s;<br> l0:=length(s);<br> end<br> else begin<br> if(s[count[k]-2]='t')then begin<br> delete(s,count[k]-2,l0-count[k]+3);<br> if((s='')=false)then begin<br> if(s[count[k]-3]='c')then<br> begin<br> med:=cotan(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-3,count[k+1]-count[k]+4);<br> insert(s1,s,count[k]-3);<br> s0:=s;<br> l0:=length(s);<br> end<br> else begin<br> med:=tan(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-2,count[k+1]-count[k]+3);<br> insert(s1,s,count[k]-2);<br> s0:=s;<br> l0:=length(s);<br> end;<br> end<br> else<br> begin<br> med:=tan(med);<br> s:=s0;<br> s1:=floattostr(med);<br><br> delete(s,count[k]-2,count[k+1]-count[k]+3);<br> insert(s1,s,count[k]-2);<br> s0:=s;<br> l0:=length(s);<br> end;<br> end;<br> end;<br> continue;<br> end;<br> if(s[count[k]-1]='^')then begin<br> value1:=pos(',',s);<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,value1);<br> value2:=rapidstrtofun(s,x);<br> s:=s0;<br> delete(s,value1,l0-value1+1);<br> delete(s,1,count[k]);<br> med1:=rapidstrtofun(s,x);<br> med:=power(med1,value2);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-1,count[k+1]-count[k]+2);<br> insert(s1,s,count[k]-1);<br> s0:=s;<br> l0:=length(s);<br> continue;<br> end;<br> if(s[count[k]-1]='s')then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> med:=cos(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-3,count[k+1]-count[k]+4);<br> insert(s1,s,count[k]-3);<br> s0:=s;<br> l0:=length(s);<br> continue;<br> end;<br> if(s[count[k]-1]='h')then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> s:=s0;<br> if(s[count[k]-2]='s')then begin<br> med:=cosh(med);<br> end<br> else begin<br> med:=sinh(med);<br> end;<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-4,count[k+1]-count[k]+5);<br> insert(s1,s,count[k]-4);<br> s0:=s;<br> l0:=length(s);<br> continue;<br> end;<br> if(s[count[k]-1]='t')then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> med:=sqrt(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-4,count[k+1]-count[k]+5);<br> insert(s1,s,count[k]-4);<br> s0:=s;<br> l0:=length(s);<br> continue;<br> end;<br> if(s[count[k]-1]='r')then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> med:=sqr(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-3,count[k+1]-count[k]+4);<br> insert(s1,s,count[k]-3);<br> s0:=s;<br> l0:=length(s);<br> continue;<br> end;<br> if(s[count[k]-1]='n')then begin<br> delete(s,count[k+1],l0-count[k+1]+1);<br> delete(s,1,count[k]);<br> med:=rapidstrtofun(s,x);<br> s:=s0;<br> if(s[count[k]-2]='l')then begin<br> med:=ln(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-2,count[k+1]-count[k]+3);<br> insert(s1,s,count[k]-2);<br> s0:=s;<br> l0:=length(s);<br> end<br> else begin<br> med:=sin(med);<br> s:=s0;<br> s1:=floattostr(med);<br> delete(s,count[k]-3,count[k+1]-count[k]+4);<br> insert(s1,s,count[k]-3);<br> s0:=s;<br> l0:=length(s);<br> end;<br> continue;<br> end;<br> end;<br> end;<br> brackets:=rapidstrtofun(s,x);<br> end;<br><br> if(judge=true)then brackets:=rapidstrtofun(s,x);<br>end;<br> exports<br> brackets name 'fun';<br>begin<br>end.