开发者

How can I parse a numeric operation containing +,-,*,/ and parentheses and return the result? [duplicate]

开发者 https://www.devze.com 2023-01-15 23:36 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Best algorithm for evaluating a mathematical expression?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Best algorithm for evaluating a mathematical expression?

I mean like when you enter this in Delphi:

var i : integer;
begin
  i=5 + 5 + (2 * (3 + 2)) + (1 * 4 + (1 - 3)开发者_Python百科)
end;

But I want a command that works in this way:

var i : integer; s:string;
begin
  s:='5 + 5 + (2 * (3 + 2)) + (1 * 4 + (1 - 3))';
  i:=ParseInt(s);
end;

Thanks in advance


If you want to implement this yourself, have a look at recursive descent parses and the shunting yard algorithm.


There are a number of free or inexpensive Math Parser components available for Delphi:

Foreval is open source, available as a DLL or delphi component.

TbcParser is $19.95 with source code.

0

精彩评论

暂无评论...
验证码 换一张
取 消