next up previous
Next: Program Trees Up: Implementation in C++ Previous: Overview

Operators and Variables

There are currently two types of operators that have been implemented: unary and binary operators. The structures are defined as follows:

typedef struct Binary_Func {
    Val (*eval)(Val,Val);               // the function pointer
    char *sign;                         // the infix operation sign
};

typedef struct Unary_Func {
    Val (*eval)(Val);                   // function pointer
    char *sign;                         // the prefix operation sign
};
Val is a typedef that can change based on the problem being solved. *sign is a string that is used to print program trees in a legible fashion; this string is placed between the operands for binary operators, and before the operand for unary operators. . For example, this is the definition of the addition operator:
// function for adding 2 values
Val add(Val a, Val b)
{
    return (a+b);
}
// the character sign for adding
char add_sign[] = " + ";
// the binary function representation
Binary_Func Add = {add, add_sign};
And, this is the definition for the absolute value function:
// absolute value function
Val abs(Val a)
{
    if (a < (Val)0)
      return (-a);
    else
      return a;
}
char abs_sign[] = "ABS";
Unary_Func Abs = {abs, abs_sign};
A list of the operators availble to the population of trees is defined in the operators module, and used during the initial population generation while creating random trees.

Variables are defined in the application module. The variable structure contains a pointer to a Val type, along with a string that is used for printing. Structure definition:

// the structure of variables (used for variable terminals).  This allows
// for printing as well
typedef struct Variable {
    Val *var;                   // pointer to the variable's value
    char *name;                 // the variable's name
};