MathExpression_frMain.pas 3.5 KB

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