delphi中如何实现表达式计算?
即我想在一个Edit1中输入:1+(2*3-4/2)*8之类表达式,
我怎么计算其结果?
如果表达式中带变量呢,如:1+(2*A-4/B)*C 怎么做?谢谢!
即我想在一个Edit1中输入:1+(2*3-4/2)*8之类表达式,
我怎么计算其结果?
如果表达式中带变量呢,如:1+(2*A-4/B)*C 怎么做?谢谢!
解决方案 »
- Delphi+Vss中的SQL问题?
- 用IIdTCPServer和TIdTCPClient控件实现文件传输功能,还有一点问题没有解决,大家进来看看!
- 如何在程序中控制另外一个软件的操作???
- 高分请教:DBGRID中的PICKLIST问题:
- 窗体设计器背景颜色的问题。请各位高手援助!!
- DELPHI新手上路,请教哪里可以看到有关打印和DELPHI与WORD间操作的相关文档!!
- 如何得到整型变量的地址
- delphi有没有类似java的BufferedImage
- 怎样设置windows任务栏的自动隐藏属性?分不够再加!UP也有分!
- how to make:超级解霸的动画图标?(空)
- 关于编码的问题:请问BIG5和GB这两种编码的范围各是多少!
- 在sqlserver 里isnull(SUM(account_gain.gainnum),0) 这样写 在access里怎么写 还有?
http://www.remobjects.com/page.asp?id={9A30A672-62C8-4131-BA89-EEBBE7E302E6}
如果不想研究编译原理,还是建议用控件(D版我也见有人问过,不过好像没有人完整写出来过)
http://www.delphiun.com/down_view.asp?id=3059
http://www.tomore.com/1/35356.html
如果没有,我就用这个了。
外国那个不用注册吧。我下了,安装了,还没研究明白怎么用。
但谁能告诉我,这样的控件怎么用?
才能计算出1+(2*3-4/2)*8之类表达式或带变量的表达式?
如果要支持变量,就一定是script了,用我上面的,里面有demo(不过很复杂)
unit cmpEvaluator;// Copyright (c) Colin Wilson 1997. All rights reserved.
// E-mail to [email protected]
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TExpressionEvaluator = class(TComponent)
private
fExpression : string; pos : Integer;
ch : char; function GetChar : char;
function GetNonWhitespace : char;
procedure SkipWhitespace; function CalcAddition : extended;
function CalcMultiplication : extended;
function CalcPower : extended;
function CalcSignedTerm : extended;
function CalcTerm : extended;
function CalcNumber : extended;
function CalcExpressionInBrackets : extended; protected
public
function Calculate : extended;
property Position : Integer read pos;
published
property Expression : string read fExpression write fExpression;
end;procedure Register;implementationfunction TExpressionEvaluator.GetChar : char;
begin
if pos < Length (Expression) then
begin
ch := Expression [pos + 1];
Inc (pos)
end
else ch := #0;
result := ch;
end;function TExpressionEvaluator.GetNonWhitespace : char;
begin
GetChar;
SkipWhitespace;
result := ch;
end;procedure TExpressionEvaluator.SkipWhitespace;
begin
if ch in [' ', #9] then
repeat
GetChar
until not (ch in [' ', #9])
end;function TExpressionEvaluator.CalcAddition : extended;
begin
result := CalcMultiplication;
while True do
case ch of
'+' : result := result + CalcMultiplication;
'-' : result := result - CalcMultiplication;
else break
end
end;function TExpressionEvaluator.CalcMultiplication : extended;
begin
result := CalcPower;
while True do
case ch of
'*' : result := result * CalcPower;
'/' : result := result / CalcPower;
else break
end
end;function TExpressionEvaluator.CalcPower : extended;
begin
result := CalcSignedTerm;
while True do
case ch of
'^' : result := exp (ln (result) * CalcSignedTerm);
else break
end
end;function TExpressionEvaluator.CalcSignedTerm : extended;
begin
case GetNonWhitespace of
'+' : result := CalcSignedTerm;
'-' : result := -CalcSignedTerm;
else result := CalcTerm
end
end;function TExpressionEvaluator.CalcTerm : extended;
begin
case ch of
'0'..'9' : result := CalcNumber;
'(' : result := CalcExpressionInBrackets;
else
raise Exception.CreateFmt ('Syntax error in expression at position %d', [pos + 1])
end
end;function TExpressionEvaluator.CalcNumber : extended;
var
s : string;
begin
result := 0;
s := '';
while ch in ['0'..'9'] do
begin
s := s + ch;
GetChar;
if ch = '.' then
begin
repeat
s := s + ch;
GetChar
until not (ch in ['0'..'9']);
break
end
end;
result := StrToFloat (s);
SkipWhitespace
end;function TExpressionEvaluator.CalcExpressionInBrackets : extended;
begin
result := CalcAddition;
if ch = ')' then
GetNonWhitespace
else
raise Exception.CreateFmt ('Mismatched parentheses at position %d', [pos + 1])
end;function TExpressionEvaluator.Calculate : extended;
begin
pos := 0;
result := CalcAddition;
if ch <> #0 then
raise Exception.CreateFmt ('Unexpected end of expression at position %d', [pos + 1]);
end;procedure Register;
begin
RegisterComponents('Samples', [TExpressionEvaluator]);
end;end.
谢谢