0

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))
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

Community
  • 1
  • 1
Javid
  • 39
  • 5
  • This is a basic problem in compiler/interpreter methods, though you don't need the full tool kit. Check [Learning to write a compiler](http://stackoverflow.com/questions/1669/) for an exhaustive list of resource. I'd like to recomend the Crenshaw tutorial for this problem, though you will have to adapt the compiler to a interpreter. – dmckee --- ex-moderator kitten Sep 11 '10 at 14:34

2 Answers2

2

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

IVlad
  • 43,099
  • 13
  • 111
  • 179
1

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.

lkessler
  • 19,819
  • 36
  • 132
  • 203