To provide a way for a program to use dynamic formulas / expressions at run-time. For instance you could read in a formula ( with variables ) from a config file then pass that formula ( variables and all ) to the Calculate Unit for processing. The Calculate unit will parse through the formula and preform the operations obeying all operator precedence rules ( Parenthesis, Power, Multiply, Divide, Modulus, Add, Subtract ). This allows for easy updates, should a formula definition change somewhere along the way. You can use the TStrings "memory" property to hold variables or formulas you want to use in your calculations. You may also override the OnFindVariableEvent to provide a custom way to search for variables ( i.e. from a database ). You could easily write your own Calculator application based on this Unit. Custom function processing has now been added to this very popular unit! Which means you can now write an Advanced Calculator with this unit or even your own spreadsheet application. Enjoy...
unit Calculate;
// *******************************************************
// *** Created : 08/02/2002 ***
// *** Last Update : 07/09/2003 ***
// *** Written By : Mason Liotta ***
// *** ***
// *** You are free to distribute/change/use this ***
// *** as long as the credits above remain intact. ***
// *** Please notify me of any changes that you make ***
// *** before you redistribute it though. I may want ***
// *** to use the changes too ! ***
// *** ***
// *******************************************************
//
// *******************************************************
// 08/09/2002 - Update by Clelson Luiz - Circular Reference Check
// 08/09/2002 - Update by Clelson Luiz - SetMemory procedure
// 07/09/2003 - Update by Mason Liotta - Custom Function Handling!
// *******************************************************
//
// To use this Unit simply instantiate an Object
// ( myObj = Tcalculate.Create(nil) ). Then use the public
// GetXXX methods provided to perform the calculation.
// Be sure that 'Calculate' is in your uses clause as well.
// You can also simply add this unit as a component to your
// palette and then just drop it on your form.
//
// Example : myObj.GetCustom('1 - (-2) + 13 * 500 / 8', '0.0');
// Returns : '815.5'; ( String Formatted to your specs. )
//
// Variables can also be used in a couple of ways.
// I have provided a public TStrings "Memory" list. You must
// add Strings in the 'name=value' form with no spaces on
// either side. You may also add a formula ( 'name=formula' ).
// That will be calculated each time the named variable is
// requested. A special variable is provided in the memory
// list that contains the answer for the last calculation.
// That variable can be used in subsequent calculations by
// using the name "_ANS" ( without the quotes ).
//
// Example : myObj.memory.Add('myVariable=2.65');
// myObj.GetInt('myVariable');
// Returns : 3; ( Integer )
//
// You can also handle the OnFindVariableEvent in your
// code to provide a custom way to search for the variable that
// is being requested. Simply set the 'value' parameter equal to
// the value that you want the variable named in the 'name'
// parameter to hold.
//
// ****** NEW FEATURE ( as of 07/09/2003 ) *********************
//
// Custom Function Processing
// --------------------------------------------------------------------
//
// Functions inside of your formulas will now be recognized!
// During processing, if the expression parser comes across an open
// parentheses where there is no delimiter character directly preeceding it,
// then it will assume it has entered a function. So an expression
// like "1 - sin(.5) / 2" will assume that "sin(.5)" is a function
// call. Since the "n" in "sin" is not a delimiter it assumes any characters
// before the last delimiter encountered is now the name of the function.
// In this case, the last delimiter encountered was the "-" so "sin" is
// now assumed to be the name of the function. All functions are currently
// customized by you the developer. When a function is found the parser
// will first build a TStringList of all the parameters ( which can be
// variables themselves ) and then It will call the OnFindFunctionEvent
// Handler. Passing in the name of the function, the parameters, and
// and value variable that you will set. The value variable is used as the
// return value of the function. If a function is found and the
// OnFindFunction Event has not been assigned, an Exception will be raised.
//
-----------INSTALLATION------------
You can use this as a stand alone Unit or install it as a component. Simply go to
Component->Install Component menu item and add the Calculate.PAS file to a new or
existing bpl... Make sure the .dcr file is in the same directory when you add it or you won't
get the icon on the component pallette.
-----------CHANGES SINCE THE 07/09/2003 VERSION--------------------------
- Custom Function Handling
- Fixed the FindFloat, does not throw an exception now everytime a
variable was encontered during the parsing. It was getting to be a pain during
debugging and there was a better way to do it anyway.
-----------CHANGES SINCE THE 08/06/2002 VERSION---------------------------
-----------CHANGES SINCE THE 08/02/2002 VERSION---------------------------
- Can now install as a component.
- Fixed the doCalc method, originally it made a couple calls to the FindFloat(right_side).
Once in the Divsion by zero section and once again in the operation section. This proved to be too
costly, especially if you were handling the OnFindVariable Event and checking a database for the
variables value.
- Added the _ANS variable that holds the answer from the last calculation in case you
want to use it in the next.
File List:
@PSC_ReadMe_747_7.txt
1Kb
CALCULATE.dcr
1Kb
Calculate.pas
18Kb
readme.txt
6Kb
Similar code
Mini-Calculator
(Popularity: ) : This example simply shows how to perform the 4 most basic mathematical functions there are: Multiplication, Division, Subtraction, and Addition. Binary Clock
(Popularity: ) : This is a simple binary clock, I got the idea from Linux's binary clock applet.
Polynomial
(Popularity: ) : Program to evaluates a uniform polynomial of one variable at the value x. Definition: f(x) = a0 x^0 + a1 ... Math Expression Evaluator
(Popularity: ) : The thing takes a string like ((Cos(Pi/2)+1)^(2+3)+1) and returns the result. It's an improved Delphi version of the award-winning VB ... Millercalc
(Popularity: ) : This is a simple calculator program that I made to help me learn the Delphi programming enviroment. It only does ...