MathExpression_frMain.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit MathExpression_frMain;
  26. interface
  27. uses
  28. System.SysUtils,
  29. System.StrUtils,
  30. System.Math,
  31. System.Classes,
  32. System.Rtti,
  33. FMX.Types,
  34. FMX.StdCtrls,
  35. FMX.Edit,
  36. FMX.Controls,
  37. FMX.Controls.Presentation,
  38. FMX.Grid.Style,
  39. FMX.Grid,
  40. FMX.ScrollBox,
  41. FMX.Dialogs,
  42. FMX.Forms,
  43. BrookHandledClasses,
  44. BrookMathExpression;
  45. type
  46. TfrMain = class(TForm)
  47. lbExpression: TLabel;
  48. edExpression: TEdit;
  49. lbVariables: TLabel;
  50. gdVariables: TStringGrid;
  51. clName: TStringColumn;
  52. clValue: TStringColumn;
  53. btCalculate: TButton;
  54. BrookMathExpression1: TBrookMathExpression;
  55. procedure btCalculateClick(Sender: TObject);
  56. function BrookMathExpression1Extension(ASender: TObject;
  57. AExtension: TBrookMathExpressionExtension): Double;
  58. procedure BrookMathExpression1Error(ASender: TObject;
  59. AError: TBrookMathExpressionError);
  60. procedure FormCreate(Sender: TObject);
  61. end;
  62. var
  63. frMain: TfrMain;
  64. implementation
  65. {$R *.fmx}
  66. procedure TfrMain.FormCreate(Sender: TObject);
  67. begin
  68. FormatSettings.DecimalSeparator := '.';
  69. edExpression.Text := Concat('$(sum, $1 + $2), ', Double.ToString(1.2), ' + ',
  70. Double.ToString(3.4), ' + mysum(foo, bar) / mymult(foo, bar) + sum(1.7, 2.46)');
  71. BrookMathExpression1.Extensions.AddStrings(['mysum', 'mymult']);
  72. gdVariables.Cells[0, 0] := 'foo';
  73. gdVariables.Cells[0, 1] := 'bar';
  74. gdVariables.Cells[1, 0] := Double.ToString(1.2);
  75. gdVariables.Cells[1, 1] := Double.ToString(3.4);
  76. ActiveControl := btCalculate;
  77. end;
  78. procedure TfrMain.BrookMathExpression1Error(ASender: TObject;
  79. AError: TBrookMathExpressionError);
  80. begin
  81. ActiveControl := edExpression;
  82. edExpression.SelStart := Pred(AError.Near);
  83. ShowMessageFmt('Error: %s', [AError.Message]);
  84. end;
  85. function TfrMain.BrookMathExpression1Extension(ASender: TObject;
  86. AExtension: TBrookMathExpressionExtension): Double;
  87. begin
  88. if AExtension.HasArgs then
  89. case IndexText(AExtension.Ident, ['mysum', 'mymult']) of
  90. 0: Exit(AExtension.Args[0] + AExtension.Args[1]);
  91. 1: Exit(AExtension.Args[0] * AExtension.Args[1]);
  92. end;
  93. Result := NaN;
  94. end;
  95. procedure TfrMain.btCalculateClick(Sender: TObject);
  96. var
  97. I: Integer;
  98. N, V: string;
  99. begin
  100. if not BrookMathExpression1.Active then
  101. BrookMathExpression1.Open;
  102. BrookMathExpression1.Clear;
  103. for I := 0 to Pred(gdVariables.RowCount) do
  104. begin
  105. N := gdVariables.Cells[0, I];
  106. V := gdVariables.Cells[1, I];
  107. if (not N.IsEmpty) and (not V.IsEmpty) then
  108. BrookMathExpression1.SetVariable(N, V.ToDouble);
  109. end;
  110. if BrookMathExpression1.Compile(edExpression.Text) then
  111. ShowMessageFmt('Result: %f', [BrookMathExpression1.Evaluate]);
  112. end;
  113. end.