回复人: li_zhifu(东北人) ( ) 信誉:100 2002-2-16 20:02:45 得分:0
唉,你们都是怎么了,这个问题M$已经有了一个解决方案了。在Win2K下在Delphi中Import ActiveX Control,选Microsoft Script Control 1.0,安装,在应用程序中
ScriptControl1.Language:='JavaScript';
ShowMessage(ScriptControl1.Eval('2*3+5'));
就可以了。
在Win98中可以把Win2K下的msscript.ocx拷过来用。
此控件可以进行复杂的运算,如支持'(',组合运算等。甚至可以对整型数进行位运算。
唉,你们都是怎么了,这个问题M$已经有了一个解决方案了。在Win2K下在Delphi中Import ActiveX Control,选Microsoft Script Control 1.0,安装,在应用程序中
ScriptControl1.Language:='JavaScript';
ShowMessage(ScriptControl1.Eval('2*3+5'));
就可以了。
在Win98中可以把Win2K下的msscript.ocx拷过来用。
此控件可以进行复杂的运算,如支持'(',组合运算等。甚至可以对整型数进行位运算。
解决方案 »
- delphi7连接Access数据库问题
- mysql+DELPHI执行SQL脚本
- 从TGraphicControl继承写edit控件的问题
- 判断字符串是否已在TStrings列里已存在?
- c/s結構中主從表的處理方案,請有經驗的兄弟,給出您的解決方法.高分回報
- !!!善分!预祝我妈妈明天手术成功!!!
- 如何得到系统所有进程窗体的句柄(为了使用sendmessage函数)
- 菜鸟用delphi和sql server 2000做程序,我想读取sql server服务器上的当前日期和时间,怎么办啊?
- 《Delphi参考手册》(大家一起来see!)
- SOCKET编程的入门??
- 谁有可以直接编写安装文件的工具???
- delphi送分问题,不来看是SB啊!!!
http://www.csdn.net/develop/Read_Article.asp?Id=12464
比如
select (2*3+5) as aaa from table
那么query.fieldbyname('aaa').asstring 就等于11 了
http://kingron.myetang.com/zsfunc0k.htm(*//
标题:计算表达式
说明:加、减、乘、除及括号;请大家多多测试
设计:Zswang
日期:2002-01-26
支持:[email protected]
//*)///////Begin Source
uses
Math;procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
var
L, R: Integer;
I: Integer;
B: Boolean;
begin
nLStr := '';
nCStr := '';
nRStr := '';
B := True;
L := 0;
R := 0;
for I := 1 to Length(mText) do
if B then begin
if mText[I] = '(' then
Inc(L)
else if mText[I] = ')' then
Inc(R);
if L = 0 then
nLStr := nLStr + mText[I]
else if L > R then
nCStr := nCStr + mText[I]
else B := False;
end else nRStr := nRStr + mText[I];
Delete(nCStr, 1, 1);
end; { Bracket }function Calc(mText: string): string;
var
vText: string; function fCalc(mText: string): string;
var
vLStr, vCStr, vRStr: string;
I, J, K, L: Integer;
begin
L := Length(mText);
if Pos('(', mText) > 0 then begin
Bracket(mText, vLStr, vCStr, vRStr);
Result := fCalc(vLStr + fCalc(vCStr) + vRStr);
end else if (Pos('+', mText) > 0) or (Pos('-', mText) > 0) then begin
I := Pos('+', mText);
J := Pos('-', mText);
if I = 0 then I := L;
if J = 0 then J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) + StrToFloat(fCalc(vRStr)))
else Result := FloatToStr(StrToFloat(fCalc(vLStr)) - StrToFloat(fCalc(vRStr)))
end else if (Pos('*', mText) > 0) or (Pos('/', mText) > 0) then begin
I := Pos('*', mText);
J := Pos('/', mText);
if I = 0 then I := L;
if J = 0 then J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) * StrToFloat(fCalc(vRStr)))
else Result := FloatToStr(StrToFloat(fCalc(vLStr)) / StrToFloat(fCalc(vRStr)))
end else if Pos('_', mText) = 1 then
Result := FloatToStr(-StrToFloat(fCalc(Copy(mText, 2, L))))
else Result := FloatToStr(StrToFloat(mText));
end;
var
I, L: Integer;
begin
vText := '';
L := Length(mText);
for I := 1 to L do
if (mText[I] = '-') and (I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')'])) then
if (I = 1) or ((I > 1) and (mText[Pred(I)] in ['*', '/'])) then
vText := vText + '_'
else if ((I > 1) and (mText[Pred(I)] in ['+', '-'])) or
((I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')']))) then
vText := vText + '+_'
else vText := vText + mText[I]
else vText := vText + mText[I];
Result := fCalc(vText);
end; { Calc }
///////End Source///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := Calc(Edit2.Text);
end;
///////End Demo