Recent

Author Topic: Printing formulas  (Read 14836 times)

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #15 on: April 29, 2012, 11:04:19 pm »
I'm using 2.4.2. I searched for "QuickEvaluate" as text in everything under Symbolic, but you are right, it isn't there. :( This function seems great and being part of FPC, I think it may be easily used in the spreadsheet component.

picstart

  • Full Member
  • ***
  • Posts: 236
Re: Printing formulas
« Reply #16 on: April 30, 2012, 01:53:27 pm »
Nothing profound just another example or two
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  symbolic;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
         ans:double;
         a,b:integer;
    Expr    : TExpression;
    SymVars : TStringList;
    I       : Longint;
    VarName : TStringList;
    Eval    : TEvaluator;
    Vars    : Array[0..1] OF ArbFloat;


begin
 // detailed way to calc and add a new constant PI
 // ex sin(PI/6) or sin(30)=0.5  since 2PI is 360
 VarName:=TStringList.Create;  //// create stringlist space for vars
 Expr:=TExpression.Create(Edit1.text); /// get the typed in expression


 Eval:=TEvaluator.Create(VarName,Expr); /// create the object to be evaluated
 SymVars:=Expr.SymbolicValueNames; /// parse for symbols
 //// if pi or PI or Pi then assign the const value
  if Symvars.IndexOf('PI')<>-1 then Eval.SetConstant('PI',3.14159265359);
  if Symvars.IndexOf('pi')<>-1 then Eval.SetConstant('pi',3.14159265359);
  if Symvars.IndexOf('Pi')<>-1 then Eval.SetConstant('Pi',3.14159265359);
 label2.Caption:=Format('eval=%g',[Eval.Evaluate([])]); /// evaluate

 Eval.Free;
 Expr.Free;
 SymVars.Free;
 {
 /// easy way to calc expression
 /// ex quickevaluate('5+(5-2)*(10+43)',[],[]);
 ans:= quickevaluate(Edit1.Text,[],[]);

 label2.Caption:=Format('ans=%g',[ans]);
 }
 {
 //  More complex but still easy ex a:=3; b:=5;
 //  ans:=quickEvaluate('(5+A+10)*B',['A','B'],[a,b])
/// Note if expression had another variable 'C" then pass values as  ['A','B','C']      [a,b,c]
 // label2.Caption:=Format('ans=%g',[ans]);
 }

end;


end.

 

TinyPortal © 2005-2018