How can I parse a numeric operation containing +,-,*,/ and parentheses and return the result? [duplicate]
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.
精彩评论