Skip to content
Jens01 edited this page Sep 17, 2016 · 6 revisions

How to use :

var
 MP : TMathParser;
 R  : Double;
begin
    MP := TMathParser.Create;
    try
      MP.Expression := '((4+5)6)7 + Min(3, 4, 5)';
      R             := MP.ParserResult;
      if MP.Error.IsNoError then
        write(R.ToString)
      else
        write(MP.Error.ToString);
    finally
      MP.Free;
    end;
    Readln;
end;

How to use variables:

var
 MP : TMathParser;
 R  : Double;
begin
    MP := TMathParser.Create;
    try
      MP.Expression     := 'a + b';
      MP.Variables['A'] := 5;
      MP.Variables['b'] := 3;
      R                 := MP.ParserResult;
      if MP.Error.IsNoError then
        Writeln(R.ToString)
      else
        Writeln(MP.Error.ToString);

      // change variable :
      MP.Variables['A'] := 7;
      Writeln(MP.ParserResult.ToString);
    finally
      MP.Free;
    end;
    Readln;
end;

How to use dynamic variables:

var
 MP : TMathParser;
 R  : Double;
begin
    MP := TMathParser.Create;
    try
      MP.Expression := 'a + b';

      MP.Variables['A'] := function: Double
        begin
          Result := 3 + 3
        end;

      MP.Variables['b'] := function: Double
        begin
          Result := 4
        end;

      R := MP.ParserResult;
      if MP.Error.IsNoError then
        Writeln(R.ToString)
      else
        Writeln(MP.Error.ToString);
    finally
      MP.Free;
    end;
    Readln;
end;
Clone this wiki locally