提问前请先搜索或查看精华区。
http://www.csdn.net/Expert/topicview.asp?id=528425回复人: 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拷过来用。
此控件可以进行复杂的运算,如支持'(',组合运算等。甚至可以对整型数进行位运算。
http://www.csdn.net/Expert/topicview.asp?id=528425回复人: 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拷过来用。
此控件可以进行复杂的运算,如支持'(',组合运算等。甚至可以对整型数进行位运算。
解决方案 »
- 一个关于DBgrid 赋值问题
- 请问:在两个.pas(有form)文件中传递参数,除了在一个Tform类的public 中声明公有变量,并在另一个form的代码中调用赋值外,有没有别的
- 再用100分来问mxoutlookbarpro的问题,解决了再送200分都行,只是现在1个问题里最多只能给100分,上次问的也没有人答案。
- 关于DBGrid
- 送分:哪里有InstallShield Express8.0的下载?
- 如何让程序自动打开自己
- 请问哪里有关于windows消息机制的资料?
- 请问一个关于DELPHI+ACCESS数据库的程序运行问题?
- 我用rave中的TRvsystem做了一张报表,如何設置列印和預覽紙張為A5呢?
- 在Delphi中怎样调用API函数?(不许笑*^_^*)
- 如何选中dbgrid的某一行,比如说最后一行,不要用在记录集中循环的做法.
- 关于paintbox的问题!
http://kingron.myetang.com/zsfunc0k.htm
//计算表达式(输入:3*(6+5)-7,输出:26)***************************************
//==============================================================================
function Calculate(Expression: string): string;
//解决方案一:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
{
var vText: string;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function Reckon(mText: string): string;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
var i, CountLeft, CountRight: Integer;
Bool: Boolean;
begin
nLStr := '';
nCStr := '';
nRStr := '';
CountLeft := 0;
CountRight := 0;
Bool := true;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i := 1 to Length(mText) do
if Bool then
begin
if mText[i] = '(' then Inc(CountLeft)
else if mText[i] = ')' then Inc(CountRight);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if CountLeft = 0 then nLStr := nLStr + mText[i]
else if CountLeft > CountRight then nCStr := nCStr + mText[i]
else Bool := false;
end else nRStr := nRStr + mText[i];
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Delete(nCStr, 1, 1);
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var vLStr, vCStr, vRStr: string;
i, j, k, mTextLength: Integer;
begin
mTextLength := Length(mText);
if Pos('(', mText) > 0
then begin
Bracket(mText, vLStr, vCStr, vRStr);
Result := Reckon(vLStr + Reckon(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 := mTextLength;
if j = 0 then j := mTextLength;
k := Min(i, j);
vLStr := Copy(mText, 1, Pred(k));
vRStr := Copy(mText, Succ(k), mTextLength);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if i = k
then Result := FloatToStr(StrToFloat(Reckon(vLStr)) + StrToFloat(Reckon(vRStr)))
else Result := FloatToStr(StrToFloat(Reckon(vLStr)) - StrToFloat(Reckon(vRStr)))
end
else if (Pos('*', mText) > 0) or (Pos('/', mText) > 0)
then begin
i := Pos('*', mText);
j := Pos('/', mText);
if i = 0 then i := mTextLength;
if j = 0 then j := mTextLength;
k := Min(i, j);
vLStr := Copy(mText, 1, Pred(k));
vRStr := Copy(mText, Succ(k), mTextLength);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if i = k
then Result := FloatToStr(StrToFloat(Reckon(vLStr)) * StrToFloat(Reckon(vRStr)))
else Result := FloatToStr(StrToFloat(Reckon(vLStr)) / StrToFloat(Reckon(vRStr)))
end
else if Pos('_', mText) = 1
then Result := FloatToStr(-StrToFloat(Reckon(Copy(mText, 2, mTextLength))))
else Result := FloatToStr(StrToFloat(mText));
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var i, ExpLength: integer;
begin
vText := '';
ExpLength := Length(Expression);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i := 1 to ExpLength do
if (Expression[i] = '-') and (i < ExpLength) and (not (Expression[Succ(i)] in ['+', '-', '(', ')']))
then if (i = 1) or ((i > 1) and (Expression[Pred(i)] in ['*', '/']))
then vText := vText + '_'
else if ((i > 1) and (Expression[Pred(i)] in ['+', '-'])) or ((i < ExpLength) and (not (Expression[Succ(i)] in ['+', '-', '(', ')'])))
then vText := vText + '+_'
else vText := vText + Expression[i]
else vText := vText + Expression[i];
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Result := Reckon(vText);
end;
}
//解决方案二:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
var
vScript: Variant;
begin
vScript := CreateOleObject('ScriptControl');
vScript.Language := 'JavaScript';
Result := vScript.Eval(Expression);
end;