The following 3 examples demonstrate how variables can be
defined and implemented.
EXAMPLE A
Visual Basic
ucDefineVariable "pi = atn(1) * 4"
ucDefineVariable "MyVar = pi + 10"
MsgBox ucEval("3pi + MyVar / 2")
VC++ or C++ Builder
ucDefineVariable("pi = atn(1) * 4");
ucDefineVariable("MyVar = pi + 10");
cout << ucEval("3pi + MyVar / 2") << endl;
Delphi
ucDefineVariable('pi = atn(1) * 4');
ucDefineVariable('MyVar = pi + 10');
Edit1.Text := FloatToStr(ucEval('3pi + MyVar / 2'));
EXAMPLE B
Visual Basic
Dim x As Double, y as Double, Total As Double
Dim VariableX As Long, VariableY As Long
Dim ExpressionPtr As Long
VariableX = ucDefineVariable("x")
VariableY = ucDefineVariable("y")
ExpressionPtr = ucParse("x^2+y^2")
For x = 0 To 100
ucSetVariableValue VariableX, x
For y = 0 To 100
ucSetVariableValue VariableY, y
Total = Total + ucEvaluate(ExpressionPtr)
Next
Next
VC++ or C++ Builder
#include <iostream>
#include <conio.h>
#include "ucalc.cpp"
using namespace std;
int main()
{
long double x = 0, y = 0, Total = 0;
long VariableX, VariableY, ExpressionPtr;
VariableX = ucDefineVariable("x");
VariableY = ucDefineVariable("y");
ExpressionPtr = ucParse("x^2+y^2");
for(x = 0; x <= 100; x++)
{
ucSetVariableValue(VariableX, x);
for(y = 0; y <= 100; y++)
{
ucSetVariableValue(VariableY, y);
Total = Total + ucEvaluate(ExpressionPtr);
}
}
cout.precision(16);
cout << Total << endl;
return 0;
}
Delphi
var
x, y, Total: Extended;
VariableX, VariableY, ExpressionPtr: Longint;
begin
x := 0;
y := 0;
Total := 0;
VariableX := ucDefineVariable('x');
VariableY := ucDefineVariable('y');
ExpressionPtr := ucParse('x^2+y^2');
while x <= 100 do
begin
ucSetVariableValue(VariableX, x);
y := 0;
while y <= 100 do
begin
ucSetVariableValue(VariableY, y);
Total := Total + ucEvaluate(ExpressionPtr);
y := y + 1;
end;
x := x + 1;
end;
Edit1.Text := FloatToStr( Total );
end;
EXAMPLE C
Visual Basic
ucDefineFunction "InStr(start, text1$, text2$)", _
AddressOf ucalcInStr
ucDefineVariable "MyString = 'This is my string' "
ucDefineVariable "OtherString", "My 'other' string"
MsgBox ucEval("InStr(1, MyString, 'my')")
MsgBox ucEval("InStr(1, OtherString, 'str')")
Function ucalcInStr() As Double
ucalcInStr = InStr(ucParam(1), ucParamStr(2), _
ucParamStr(3))
End Function
VC++ or C++ Builder
#include <iostream>
#include <conio.h>
#include "ucalc.cpp"
using namespace std;
long double ucalcInStr();
int main()
{
ucDefineFunction("InStr(start, text1$, text2$)",
reinterpret_cast<long>(ucalcInStr));
ucDefineVariable("MyString = 'This is my string' ");
ucDefineVariable("OtherString", "My 'other' string");
cout << ucEval("InStr(1, MyString, 'my')") << endl;
cout << ucEval("InStr(1, OtherString, 'str')")
<< endl;
return 0;
}
long double ucalcInStr()
{
long StartPos;
long double ReturnValue;
char MainString[80], SubString[80];
char *StrPosition;
StartPos = ucParam[1];
strcpy(MainString, ucParamStr(2));
strcpy(SubString, ucParamStr(3));
StrPosition=strstr(MainString+StartPos-1, SubString);
if (StrPosition != NULL)
ReturnValue = StrPosition - MainString + 1;
else
ReturnValue = 0;
return ReturnValue;
}
Delphi
procedure TForm1.Button1Click(Sender: TObject);
begin
ucDefineFunction('InStr(start, text1$, text2$)',
Longword( @ucalcInStr ));
ucDefineVariable('MyString = "This is my string" ');
ucDefineVariable('OtherString', 'My "other" string');
Edit1.Text :=ucEvalStr('InStr(1,MyString,"my")');
Edit2.Text :=ucEvalStr('InStr(1,OtherString,"str")');
end;
function InStr(start: Longint; text1, text2: string):
Longint;
var
TmpString: string;
begin
TmpString := copy(text1,Start,Length(text1)-Start+1);
InStr := Pos(text2, TmpString)+Start-1;
end;
function ucalcInStr: Extended;
begin
ucalcInStr := InStr(Trunc(ucParam[1]), ucParamStr(2),
ucParamStr(3));
end;
Download
fastmath.zip (~145K)
