Home
News
Products
     uCalc Fast Math Parser
     uCalc Language Builder
     Windows Calculator
     DOS Calculator
Downloads
Message Forums
Satisfaction Survey
FAQ
Contact
Privacy Statement
Testimonials
Purchase

Misc. callback functions
 

This sample demonstrates one type of function callback syntax.  For other types of function callbacks, see Sample #3, and Sample #5.

Visual Basic

Private Sub Form_Load()
   ucDefineFunction "Area()", AddressOf Area
   ucDefineFunction "Average()", AddressOf Average
End Sub

Private Sub Command1_Click()
   Expression$ = InputBox("Enter an expression", , _
      "average(area(3,6), 4, 8) + 5")
   MsgBox ucEval(Expression$)

   If ucError Then MsgBox ucErrorMessage
End Sub

Function Area() As Double
   L = ucParam(1)  ' // Length
   W = ucParam(2)  ' // Width
    
   Area = L * W

   ucErrorData(0) = "AREA"
   If L < 0 Or W < 0 Then ucError = 13
      ' // Incorrect argument value
   If ucParamCount <> 2 Then ucError = 12
      ' // Invalid number of arguments
End Function

Function Average() As Double
   For x = 1 To ucParamCount
      Total = Total + ucParam(x)
   Next
   Average = Total / ucParamCount
End Function

VC++ or C++ Builder

#include <iostream>
#include <conio.h>
#include "ucalc.cpp"

using namespace std;

long double Area();
long double Average();

int main()
{
  char Expression[80];

  ucDefineFunction("Area()", 
     reinterpret_cast<long>(Area));
  ucDefineFunction("Average()",
     reinterpret_cast<long>(Average));

  cout << "Enter an expression such as:" << endl;
  cout << "average(area(3,6),4,8)+5" << endl;

  cin.getline(Expression, 80, '\n');
  cout << ucEval(Expression) << endl;

  if(ucError())
    cout << ucErrorMessage() << endl;

  return 0;
}

long double Area()
{
  long double L;  // Length
  long double W;  // Width

  L = ucParam[1];
  W = ucParam[2];

  ucSetErrorData(0, "AREA");
  if(L < 0 || W < 0)
    ucSetError = 13; // Invalid argument value

  if(ucParamCount != 2)
    ucSetError = 12; // Invalid number of parameters

  return L * W;
}

long double Average()
{
  long x = 0;
  long double Total = 0;

  for(x = 1; x <= ucParamCount; x++)
    Total = Total + ucParam[x];

  return Total / ucParamCount;
}

Delphi

procedure TForm1.Button1Click(Sender: TObject);
var
  Expression: string;
  i: Integer;
begin
  ucDefineFunction('Area()', Longword( @Area ));
  ucDefineFunction('Average()', Longword( @Average ));

  Expression := InputBox('', 'Enter an expression:',
                         'average(area(3,6),4,8)+5');
  Edit1.Text := FloatToStr( ucEval(Expression) );

  if ucError <> 0 then
    i := Application.MessageBox(ucErrorMessage,
                                'Error', MB_OK);
end;

function Area: Extended;
var
  L, W: Extended;
begin
  L := ucParam[1];  { Length }
  W := ucParam[2];  { Width }

  ucSetErrorData(0, 'AREA');
  if (L < 0) or (W < 0) then
    ucSetError := 13; { Incorrect argument value }
  if ucParamCount <> 2 then
    ucSetError := 12; { Invalid number of parameters }

  Area := L * W;
end;

function Average: Extended;
var
  x: Longint;
  Total: Extended;
begin
  Total := 0;
  for x := 1 to ucParamCount do
        Total := Total + ucParam[x];
  Average := Total / ucParamCount;
end;

 


 

Download fastmath.zip (~145K)






|Home| |News| |Products| | uCalc Fast Math Parser| | uCalc Language Builder| | Windows Calculator| | DOS Calculator| |Downloads| |Message Forums| |Satisfaction Survey| |FAQ| |Contact| |Privacy Statement| |Testimonials| |Purchase|


Copyright © 2006 by Daniel Corbier