A Simple Math evaluator lazarus / 8-Other ---------------------------------------------------------------------- BenJones · 2011-08-12 09:01 Hi this is my simple math evaluator I been working on since last week. it only basic at the moment can do things like 2 + 2 * 3 does not support brackets yet but am working on it. hope to include it soon in my little scripting lanuage project I want to make. Comments welcome. Hope you find it of some use. How to use start a new project and add this code. Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     procedure Button1Click(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } function isOp(aSign: string): boolean; begin   Result := False;   if aSign = '+' then     Result := True;   if aSign = '-' then     Result := True;   if aSign = '*' then     Result := True;   if aSign = '/' then     Result := True; end; function GetToken(expression: string; out Counter: integer): variant; var   aChar: string;   sData: string; begin   repeat     //Get Char     aChar := expression[Counter];     //Skip white space     if aChar = ' ' then       aChar := '';     //Check if operator     if isOp(aChar) then     begin       //Exit from loop       break;     end     else     begin       //Build String       sData := sData + aChar;       //Inc Counter       Inc(Counter);     end;   until Counter > Length(expression);   //Return result   Result := StrToInt(sData); end; function Eval(expression: string): variant; var   aChar: string;   aOperator: string;   Ret: variant;   iCount: integer; begin   iCount := 1;   while iCount < Length(expression) do   begin     //Get char     aChar := expression[iCount];     if isOp(aChar) then     begin       //Get operator       aOperator := Copy(expression, iCount, 1);       //INC Counter       Inc(iCount);     end;     //Check for operators     if aOperator = '' then     begin       Ret := Trim(GetToken(expression, iCount));     end;     //Do Add     if aOperator = '+' then     begin       Ret := Ret + GetToken(expression, iCount);     end;     //Add Subtract     if aOperator = '-' then     begin       Ret := Ret - GetToken(expression, iCount);     end;     //Do Times     if aOperator = '*' then     begin       Ret := Ret * GetToken(expression, iCount);     end;     //Do Divide     if aOperator = '/' then     begin       Ret := Ret / GetToken(expression, iCount);     end;   end;   //Return result   Result := Ret; end; Add a command button then add this code. Code: [Select]self.Text := Eval('5 + 5 + 5 + 5 * 2 - 1 / 2'); // Returns 19.5 ---------------------------------------------------------------------- marcov · 2011-08-12 10:46 Have a look at unit symbolic :-) ---------------------------------------------------------------------- Leledumbo · 2011-08-12 14:07 QuoteHave a look at unit symbolic :-)The unit is good, but it misses documentation... ---------------------------------------------------------------------- marcov · 2011-08-12 17:07 Quote from: Leledumbo on August 12, 2011, 04:07:42 pmQuoteHave a look at unit symbolic :-)The unit is good, but it misses documentation... There are examples. ---------------------------------------------------------------------- Leledumbo · 2011-08-12 22:11 Yes, that's where I learned how to use it besides the unit source code. ---------------------------------------------------------------------- marcov · 2011-08-14 13:55 Quote from: Leledumbo on August 13, 2011, 12:11:23 amYes, that's where I learned how to use it besides the unit source code. I added a routine that simplifies basic usage to trunk (2.7.1) Basically you can do  quickevaluate('5+(5-2)*(10+43)',[],[]) and it will return an extended value or throw an exception on error. Variables are also possible. An example for the new functionality was added called easyevalexample