📄 readme.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>TFatExpression readme</title>
</head>
<body>
<font size="4">
<h2>TFatExpression 1.01</h2>
a component by <a href="mailto:gasper.kozak@email.si">Gasper Kozak</a>, July 2001<br>
<b>This component is completely free for any (ab)use. I do not promise any support or new updates.
The component probably includes bugs.</b> If you find any, please let me know.<br>
check for a new version on
<a href="http://www.delphipages.com/result.cfm?SR=TFatExpression&AO=and&RequestTimeout=500">
Delphi pages</a>.<br>
<hr>
new from 1.0<br>
* bug-fix: factorial operation (!) <br>
* bug-fix: operation processing order was messed-up (8-2-2 was 8, now it's 4) <br>
* added scientific notation support (1.23E-2 = 1.23 * 10^(-2)) <br>
<hr>
this is an expression-calculation component. Simply by passing an string-expression to it, you can evaluate
its value. It uses a very simple parser and a syntax-tree approach.<br>
<br>
FEATURES<br>
<b>operations</b>: <b>+ - * / ^ !</b><br>
<b>parenthesis</b>: <b>( )</b><br>
<b>functions</b>: <br>
In the TFatExpression component, there is a Functions: TStringList property, which holds a list of
user-defined functions. These can be entered at design-time, or assigned at run-time (see demo).
Functions without parameters can also serve as constants (see bellow for function Pi).<br>
the function format is as follows:<pre>
function = function_name [ "(" argument_name [";" argument_name ... ] ")" ] "=" expression
</pre>
valid function_name (and also argument_name) consists of:
<pre>
first character = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_
every next character = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$#@&0123456789
</pre>
here are a few examples of valid functions:<br>
<i>no parameter</i>: Pi=3.14 <br>
<i>one parameter</i>: f(t) = 4*t*t + 2*t + 1<br>
<i>one parameter</i>: sqr(x)=x*x<br>
<i>one parameter</i>: sqrt(x)=x^(1/2)<br>
see how functions can include each other<br>
<i>one parameter</i>: circle_area(radius)=Pi*sqr(radius)<br>
<i>two parameters</i>: add(x;y)=x + y <br>
<b>note:</b> in function definition and also in expression, multiple parameters must be separated
with a ";" character.<br>
When calculating, you would use some of these functions like this:<br>
FatExpression1.Text := 'sqr(2) + sqrt(16) + add(4; 4)'; <br>
in this case, function FatExpression1.Value should return 16</i>
<br>
<br>
Another type of function-implementing is available. If the component, while evaluating the expression,
comes along an unknown function, it generates an OnEvaluate event. It passes the function name, argument
count and argument list to the event, expecting the programer to provide it's result value. This can be
used for variables, arrays and constants that are application-dependant.<br>
Here is some code showing how you could make the "add(x;y)=x+x" function the other way, by not adding it
to the Functions property. <br>
The event handler first checks the Eval argument, and if is the right function (add), it returns a
result of "first argument plus second argument" to the Value. Argument list is zero-based, meaning the
first argument is Args[0], second is Args[1], ... and the last is Args[ArgCount - 1]. ArgCount tells
you how many arguments are in the Args array.<br>
<pre>
<b>procedure</b> TForm1.FatExpression1Evaluate(Sender: TObject; Eval: <b>String</b>;
Args: <b>array of</b> Double; ArgCount: Integer; <b>var</b> Value: Double);
<b>begin</b>
<b>if</b> UpperCase(Eval) = 'ADD' <b>then</b>
Value := Args[0] + Args[1];
<b>end;</b>
</pre>
This code is equal to writing "add(x;y)=x+y" function in the Functions property, although it is probably
faster, since it is compiled and optimized by Delphi. There is also one big advantage using this approach.
it has a dynamic number of arguments. <br>
Imagine this:<br>
you add this function to the FatExpression1.Functions property: "add(a;b;c;d;e;f;g;h)=a+b+c+d+e+f+g+h".<br>
In expression, you can pass less or more arguments (than there are specified in the function-definition)
to the function-call, for example:<br>
FatExpression1.Text := 'add(1;2;3)'; <i>// returns 6</i><br>
FatExpression1.Text := 'add(1;1;1;1;1;1;1;1;1;1;1;1;1)'; <i>// returns 8, because the function definition
only includes 8 parameters</i><br>
If you pass less arguments, than the parser expects, the missing parameter are treated as zeros.
<br>
As you can see in demo, I handle the ADD function through OnEvaluate event. This way, I can take care of any
number of arguments passed to the function-call:
<pre>
<b>var</b> I: Integer;
<b>if</b> UpperCase(Eval) = 'ADD' <b>then</b>
<b>for</b> I := 0 <b>to</b> ArgCount - 1 <b>do</b>
Value := Value + Args[I];
</pre>
This code will add any number of arguments and return the value. So now, a call to <br>
FatExpression1.Text := 'add(1;1;1;1;1;1;1;1;1;1;1;1;1)';<br>
returns 13.
<br><br>
Function-calls in expression can also be nested within each other:<br>
add(add(1; 1); add(1; 1))<br>
add(add(add(add(1); 1); 1); 1))<br>
add(add(add(add(4))))<br>
All these functions return 4.
<br>
<hr>
<br>
<b>properties</b><br>
<ul>
<li>
EvaluateOrder: TEvaluateOrder = (eoInternalFirst, eoEventFirst)<br>
Sets the order of evaluating functions. eoInternalFirst means the Functions property is checked
first. If the function is found, it is evaluated and no event is triggered. If the function is not
found, the event is triggered.
</li>
<li>
Functions: TStringList<br>
A list of functions in specified format.
</li>
<li>
Text: String<br>
The mathematical expression in string. This cannot be an equation.
</li>
<li>
Value: Double<br>
Returns the value of "Text", using Functions property and OnEvaluate event for evaluating
unknown functions.
</li>
</ul>
<b>events</b><br>
<ul>
<li>
OnEvaluate(Sender: TObject; Eval: <b>String</b>; Args: <b>array of</b> Double; ArgCount: Integer; <b>var</b> Value: Double; <b>var</b> Done: Boolean)<br>
Eval is the name of the evaluated function. Args is the array of values, presenting the called function's
arguments. ArgCount is number of elements in Args. Args is zero-based array. The Value argument is
the result value that should be filled. If you handle the function and return the result, you
should set Done to TRUE.
</li>
</ul>
<br><br>
<hr>
so... this is the basic info. For more, you should see demo, dig into the source or email me
(in this order).
<hr align="left" width="50">
eof
<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -