在一个过程中我定义了一个变量 JJ(进价),LSJ(零价) 。
在一个EDIT里面允许用户设置LSJ和JJ的计算公式,例如 LSJ=JJ*1.15
当用户录入JJ时按照EDIT的公式计算出零价。应该如何写代码阿,LSJ=EDIT.TEXT ????解决了就结贴!
在一个EDIT里面允许用户设置LSJ和JJ的计算公式,例如 LSJ=JJ*1.15
当用户录入JJ时按照EDIT的公式计算出零价。应该如何写代码阿,LSJ=EDIT.TEXT ????解决了就结贴!
s:=edit1.text;
lsj:=strtoint(copy(s,pos('*',s),length(s)-pos('*',s)))*jj
LSJ=JJ*1.15*GYL/1.2+JJ*10%
这样的公式怎样算出LSJ呢?
这个控件网上多的是,找一下就有了
1.ScriptControl
uses
ComObj.....
function Eval(expStr: string): string;
var
js: OleVariant;
begin
js := CreateOleObject('ScriptControl');
js.Language := 'JavaScript';
Result := js.Eval(expStr);
js := Unassigned;
end;Eval('((123+112)*12+21*(4562-60))*3/7 + 50')=41776.57142857142. 用数据库, select
'Select '+Edit1.text + ' as AValue';
AduQuery.Open;3.
uses
Math,Sysutils; procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
function Calc(mText: string): string;implementationprocedure 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 := Calc(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 ['+', '-']) then
vText := vText + '+_'
else vText := vText + mText[I]
else vText := vText + mText[I];
Result := fCalc(vText);
end; { Calc }
end.