//from
http://www.csdn.net/Expert/topic/418/418277.shtm
http://www.csdn.net/Expert/topic/418/418277.shtm
解决方案 »
- 请教:Application.messagebox和messagebox,showmessage,在一个onstayup的form中显示有区别吗?
- mysql blob字段读写
- 关于listview大量添加数据标题没了,又闪的问题!急
- Actionlist 和CheckBox在问题
- delphi7.0 编译工程错误提示 求解
- fastreport如何让设计页面中文化
- fastreport 多个page 页码显示问题?
- 急急!!在delphi的ActiveForm中怎样调用windows的api来关闭当前打开的浏览器窗口,在线等!!!
- △△△△△△HTML 帮助文件在Delphi中的调用问题???△△△△△△
- 伤心人渣来领分!
- 再次提问,如何读出数据库表中字段的类型????
- CSDN要玩完了如此下去谁还。。。。。满100结贴(Focus老鱼)
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 > 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到edit6里的数值加起来不就可以了吗?可以说说为什么???
to urchinjj(懒刺猬):公式是动态的,没有固定
什么公式是动态的?能不能具体解释一下?详细一点可以?俺不明白,什么里有公式?公式在哪里?不是把edit里的数值加起来吗??请解释??
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 }//请各位有空再测测
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 }//请各位有空再测测
(I1-I2)*I3/I4+I5
(I1+I2)+I3/I4+I5
(I1+I2)-I3*(I4+I5)
(I1-I2)-I3*(I4-I5)
var
I: Integer;
begin
for I := 0 to Memo1.Lines.Count - 1 do begin
ADOQuery1.SQL.Text := Format('SELECT %s AS 计算', [Memo1.Lines[I]]);
ADOQuery1.Open;
Memo1.Lines[I] := Memo1.Lines[I] + '=' + Calc(Memo1.Lines[I]) + '//' +
ADOQuery1.FieldByName('计算').AsString;
ADOQuery1.Close;
end;
end;
他的公式还可能是
(I1+I2)*I4+I5
(I1-I2)*I3/I4+I5
(I1+I2)-I3*(I4+I5)+I3*(I4+I5)
I1-I2
(I1+I2)*I4+I5
(I1-I2)*I3/I4+I5
(I1+I2)-I3*(I4+I5)+I3*(I4+I5)
I1-I2 我知道公式变化可以不同,我是想问,这些公式在哪里?是在edit1---edit6的text里吗?因为我认为text里的是数值,,我想问,什么叫计算公式?闲俺问的问题低级啊?那俺问问什么叫 词法分析可以吗?哼
应用在人事工资等领域
编辑框里保存的是变量的数值//是在edit1---edit6的text里吗?
I1对应Edit1.Text
I2对应Edit2.Text
I3对应Edit3.Text
I4对应Edit5.Text
I5对应Edit6.Text
没有具体数值只有变量的表达式//什么叫计算公式?
不是低级,只是要说清楚不太好表达//闲俺问的问题低级啊?
可以,但没有词法分析这么复杂//那俺问问什么叫 词法分析可以吗?嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻
嘻嘻嘻笑一笑嘻嘻嘻嘻
嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻哼 √
俺俺俺俺俺俺明白了俺俺俺俺俺俺俺
俺俺俺俺俺俺俺俺俺俺俺俺俺俺俺俺//shy
//blush
俺在写作业那,,嘿嘿