在Tedit框内输入计算公式,回车后,框内的值变为计算后的数值。即实现计算功能?请问应如何实现?
解决方案 »
- 如果实现光标一直停留在一个输入框?
- 请问如何在richedit里将光标附近的字符串取出来?
- 半夜了,有人在吗?先装了express grid5再expressbar 5.1.3 报错,装不了,怎么办,急????
- 祝大家生蛋快乐!快乐的同时,帮个忙......
- 请问 如下sql语句
- DEPHI好还是JAVA好?????
- "Destination := Source" 与 "Destination.Assign(Source)"的区别
- 给大家讲一个笑话,并且问一个数据库问题。
- 我想样一个按钮在MOUSE砰到它的时候就随机的走到别的地方,总之MOUSE永运砰不到它! 有什么好的算法实现!
- DELPHI是什么?
- 打印问题
- 为什么可以用abs这种名字当变量名
//表达式计算
//====================================================================
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 > 1) and (mText[Pred(I)] = ')') and (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 }
例: edit1.text := calc(5*7+1);
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
vScript: Variant;
str:string;
begin
if key = #13 then
begin
str:=Edit1.Text;
vScript:=CreateOleObject('ScriptControl');
vScript.Language := 'JavaScript';
str:=vScript.Eval(str);
Edit1.Text := str;
end;
end;
用ScriptControl比较简单,但功能有限