MathExpression_frMain.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 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. {$MODE DELPHI}
  27. interface
  28. uses
  29. SysUtils,
  30. StrUtils,
  31. Math,
  32. Classes,
  33. StdCtrls,
  34. Grids,
  35. Dialogs,
  36. Forms,
  37. BrookMathExpression;
  38. type
  39. TfrMain = class(TForm)
  40. BrookMathExpression1: TBrookMathExpression;
  41. btCalculate: TButton;
  42. edExpression: TEdit;
  43. gdVariables: TStringGrid;
  44. lbExpression: TLabel;
  45. lbVariables: TLabel;
  46. procedure BrookMathExpression1Error(Sender: TObject;
  47. AError: TBrookMathExpressionError);
  48. function BrookMathExpression1Extension(Sender: TObject;
  49. AExtension: TBrookMathExpressionExtension): Double;
  50. procedure btCalculateClick(Sender: TObject);
  51. procedure FormCreate(Sender: TObject);
  52. end;
  53. var
  54. frMain: TfrMain;
  55. implementation
  56. {$R *.lfm}
  57. { TfrMain }
  58. procedure TfrMain.FormCreate(Sender: TObject);
  59. begin
  60. FormatSettings.DecimalSeparator := '.';
  61. edExpression.Text := Concat('$(sum, $1 + $2), ', Double(1.2).ToString, ' + ',
  62. Double(3.4).ToString, ' + mysum(foo, bar) / mymult(foo, bar) + sum(1.7, 2.46)');
  63. BrookMathExpression1.Extensions.AddStrings(['mysum', 'mymult']);
  64. gdVariables.Cells[0, 1] := 'foo';
  65. gdVariables.Cells[0, 2] := 'bar';
  66. gdVariables.Cells[1, 1] := Double(1.2).ToString;
  67. gdVariables.Cells[1, 2] := Double(3.4).ToString;
  68. ActiveControl := btCalculate;
  69. end;
  70. procedure TfrMain.BrookMathExpression1Error(Sender: TObject;
  71. AError: TBrookMathExpressionError);
  72. begin
  73. ActiveControl := edExpression;
  74. edExpression.SelStart := Pred(AError.Near);
  75. ShowMessageFmt('Error: %s', [AError.Message]);
  76. end;
  77. function TfrMain.BrookMathExpression1Extension(Sender: TObject;
  78. AExtension: TBrookMathExpressionExtension): Double;
  79. begin
  80. if AExtension.HasArgs then
  81. case IndexText(AExtension.Ident, ['mysum', 'mymult']) of
  82. 0: Exit(AExtension.Args[0] + AExtension.Args[1]);
  83. 1: Exit(AExtension.Args[0] * AExtension.Args[1]);
  84. end;
  85. Result := NaN;
  86. end;
  87. procedure TfrMain.btCalculateClick(Sender: TObject);
  88. var
  89. I: Integer;
  90. N, V: string;
  91. begin
  92. if not BrookMathExpression1.Active then
  93. BrookMathExpression1.Open;
  94. BrookMathExpression1.Clear;
  95. for I := gdVariables.FixedRows to Pred(gdVariables.RowCount) do
  96. begin
  97. N := gdVariables.Cells[0, I];
  98. V := gdVariables.Cells[1, I];
  99. if (not N.IsEmpty) and (not V.IsEmpty) then
  100. BrookMathExpression1.SetVariable(N, V.ToDouble);
  101. end;
  102. if BrookMathExpression1.Compile(edExpression.Text) then
  103. ShowMessageFmt('Result: %f', [BrookMathExpression1.Evaluate]);
  104. end;
  105. end.