//////////////////////////////////////////////
引:四则运算函数
function sum(Value:String):string;
const
a = '+';
b = '-';
c = '*';
d = '/';
ea= '(';
eb= ')';
function GetToken(Value:String):String;
var
P1,P2:Integer;
begin
Result := '';
P1 := Pos(ea,Value);
if P1 <> 0 then
begin
P2 := Pos(eb,Value);
Result := Copy(Value,P1+1,P2-P1-1);
end;
end;
function GetResult(Value:String):String;
var
P1,P2,P3,P4,i,nStart,nEnd,P:Integer;
N1,N2 : Real;
Tmp : String;
IsFuShu : Boolean;
begin
IsFuShu := False;
N1 := 0;
N2 := 0;
P := 0;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
while (P1<>0)or(P2<>0)or(P3<>0)or(P4<>0) do
begin
if (P3<>0) and (P4=0) then
P := P3;
if (P3=0) and (P4<>0)then
P := P4;
if (P3<>0) and (P4<>0) and (P4>P3) then
P := P3;
if (P3<>0) and (P4<>0) and (P4<P3) then
P := P4;
if (P1<>0) and (P2=0) then
P := P1;
if (P1 =0) and (P2<>0)then
P := P2;
if (P1<>0) and (P2<>0) and (P2>P1) then
P := P1;
if (P1<>0) and (P2<>0) and (P1>P2) then
P := P2;
if P <> 0 then
begin
for i := P-1 downto 1 do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=ea)or(i=1) then
begin
if i<>1 then
N1 := StrToFloat(Copy(Value,i+1,P-i-1))
else
N1 := StrToFloat(Copy(Value,1,P-1));
Break;
end;
end;
if i<>1 then
nStart := i+1
else nStart := 1;
for i := P+1 to Length(Value) do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=eb)or(i=Length(Value)) then
begin
if i<>Length(Value) then
N2 := StrToFloat(Copy(Value,P+1,i-P-1))
else
N2 := StrToFloat(Copy(Value,P+1,i));
Break;
end;
end;
if i=Length(Value) then
nEnd := i+1
else
nEnd := i;
if P=P1 then
Tmp := FloatToStr(N1*N2);
if P=P2 then
Tmp := FloatToStr(N1 / N2);
if P=P3 then
Tmp := FloatToStr(N1+N2);
if P=P4 then
begin
if N1<N2 then
begin
IsFuShu := Not IsFuShu;
Tmp := FloatToStr(N2-N1);
end;
if N1>N2 then
Tmp := FloatToStr(N1-N2);
end;
if nEnd <> 1 then
Delete(Value,nStart,nEnd-nStart)
else
Delete(Value,nStart,nEnd);
Insert(Tmp,Value,nStart);
end;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
end;
if IsFuShu then
Result := '-'+Value
else
Result := Value;
end;
var
S : String;
P1,P2 : Integer;
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
while GetToken(Value) <> '' do
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
S:=GetToken(Value);
S:=GetResult(S);
Delete(Value,P1,P2-P1+1);
Insert(S,Value,P1);
end;
Result := GetResult(Value);
end;
引:四则运算函数
function sum(Value:String):string;
const
a = '+';
b = '-';
c = '*';
d = '/';
ea= '(';
eb= ')';
function GetToken(Value:String):String;
var
P1,P2:Integer;
begin
Result := '';
P1 := Pos(ea,Value);
if P1 <> 0 then
begin
P2 := Pos(eb,Value);
Result := Copy(Value,P1+1,P2-P1-1);
end;
end;
function GetResult(Value:String):String;
var
P1,P2,P3,P4,i,nStart,nEnd,P:Integer;
N1,N2 : Real;
Tmp : String;
IsFuShu : Boolean;
begin
IsFuShu := False;
N1 := 0;
N2 := 0;
P := 0;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
while (P1<>0)or(P2<>0)or(P3<>0)or(P4<>0) do
begin
if (P3<>0) and (P4=0) then
P := P3;
if (P3=0) and (P4<>0)then
P := P4;
if (P3<>0) and (P4<>0) and (P4>P3) then
P := P3;
if (P3<>0) and (P4<>0) and (P4<P3) then
P := P4;
if (P1<>0) and (P2=0) then
P := P1;
if (P1 =0) and (P2<>0)then
P := P2;
if (P1<>0) and (P2<>0) and (P2>P1) then
P := P1;
if (P1<>0) and (P2<>0) and (P1>P2) then
P := P2;
if P <> 0 then
begin
for i := P-1 downto 1 do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=ea)or(i=1) then
begin
if i<>1 then
N1 := StrToFloat(Copy(Value,i+1,P-i-1))
else
N1 := StrToFloat(Copy(Value,1,P-1));
Break;
end;
end;
if i<>1 then
nStart := i+1
else nStart := 1;
for i := P+1 to Length(Value) do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=eb)or(i=Length(Value)) then
begin
if i<>Length(Value) then
N2 := StrToFloat(Copy(Value,P+1,i-P-1))
else
N2 := StrToFloat(Copy(Value,P+1,i));
Break;
end;
end;
if i=Length(Value) then
nEnd := i+1
else
nEnd := i;
if P=P1 then
Tmp := FloatToStr(N1*N2);
if P=P2 then
Tmp := FloatToStr(N1 / N2);
if P=P3 then
Tmp := FloatToStr(N1+N2);
if P=P4 then
begin
if N1<N2 then
begin
IsFuShu := Not IsFuShu;
Tmp := FloatToStr(N2-N1);
end;
if N1>N2 then
Tmp := FloatToStr(N1-N2);
end;
if nEnd <> 1 then
Delete(Value,nStart,nEnd-nStart)
else
Delete(Value,nStart,nEnd);
Insert(Tmp,Value,nStart);
end;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
end;
if IsFuShu then
Result := '-'+Value
else
Result := Value;
end;
var
S : String;
P1,P2 : Integer;
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
while GetToken(Value) <> '' do
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
S:=GetToken(Value);
S:=GetResult(S);
Delete(Value,P1,P2-P1+1);
Insert(S,Value,P1);
end;
Result := GetResult(Value);
end;
解决方案 »
- adoquery的filter中单个字符怎么过滤
- 求一CRC16的算法
- 如果我不加控制 ,线程1开始后又运行线程1,是不是自己本身就能等自己完成上一次后再开始?
- 请问这两段代码问题出在哪里了(有关Indy的)
- 刚用delphi2005做了个小程序,有要源代码的吗
- 为什么最小化时任务栏上没有应用程序的代码不行呢?
- 求助!TBitmap尺寸过大,出现'out of system resources'问题
- 急急急 老大们救我!!!
- classname:='winamp vl.x'是什么意思?
- 怎么样在Delphi6中设置DLL的断点调试?
- 哪位高手给出一个文件分割的小程序?
- 各位,近来看看这个数据恢复的问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
引:四则运算函数
function sum(Value:String):string;
const
a = '+';
b = '-';
c = '*';
d = '/';
ea= '(';
eb= ')';
function GetToken(Value:String):String;
var
P1,P2:Integer;
begin
Result := '';
P1 := Pos(ea,Value);
if P1 <> 0 then
begin
P2 := Pos(eb,Value);
Result := Copy(Value,P1+1,P2-P1-1);
end;
end;
function GetResult(Value:String):String;
var
P1,P2,P3,P4,i,nStart,nEnd,P:Integer;
N1,N2 : Real;
Tmp : String;
IsFuShu : Boolean;
begin
IsFuShu := False;
N1 := 0;
N2 := 0;
P := 0;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
while (P1<>0)or(P2<>0)or(P3<>0)or(P4<>0) do
begin
if (P3<>0) and (P4=0) then
P := P3;
if (P3=0) and (P4<>0)then
P := P4;
if (P3<>0) and (P4<>0) and (P4>P3) then
P := P3;
if (P3<>0) and (P4<>0) and (P4<P3) then
P := P4;
if (P1<>0) and (P2=0) then
P := P1;
if (P1 =0) and (P2<>0)then
P := P2;
if (P1<>0) and (P2<>0) and (P2>P1) then
P := P1;
if (P1<>0) and (P2<>0) and (P1>P2) then
P := P2;
if P <> 0 then
begin
for i := P-1 downto 1 do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=ea)or(i=1) then
begin
if i<>1 then
N1 := StrToFloat(Copy(Value,i+1,P-i-1))
else
N1 := StrToFloat(Copy(Value,1,P-1));
Break;
end;
end;
if i<>1 then
nStart := i+1
else nStart := 1;
for i := P+1 to Length(Value) do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=eb)or(i=Length(Value)) then
begin
if i<>Length(Value) then
N2 := StrToFloat(Copy(Value,P+1,i-P-1))
else
N2 := StrToFloat(Copy(Value,P+1,i));
Break;
end;
end;
if i=Length(Value) then
nEnd := i+1
else
nEnd := i;
if P=P1 then
Tmp := FloatToStr(N1*N2);
if P=P2 then
Tmp := FloatToStr(N1 / N2);
if P=P3 then
Tmp := FloatToStr(N1+N2);
if P=P4 then
begin
if N1<N2 then
begin
IsFuShu := Not IsFuShu;
Tmp := FloatToStr(N2-N1);
end;
if N1>N2 then
Tmp := FloatToStr(N1-N2);
end;
if nEnd <> 1 then
Delete(Value,nStart,nEnd-nStart)
else
Delete(Value,nStart,nEnd);
Insert(Tmp,Value,nStart);
end;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
end;
if IsFuShu then
Result := '-'+Value
else
Result := Value;
end;
var
S : String;
P1,P2 : Integer;
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
while GetToken(Value) <> '' do
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
S:=GetToken(Value);
S:=GetResult(S);
Delete(Value,P1,P2-P1+1);
Insert(S,Value,P1);
end;
Result := GetResult(Value);
end;
http://www.playicq.com/dispdoc.asp?id=1221
一个可以在程序中动态编译数学表达式的编译器类