home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / Equation Evaluator / CEquation ƒ / EquEval.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-14  |  3.5 KB  |  105 lines  |  [TEXT/MPCC]

  1. /* Some of you may choose to define TYPE as a "float" instead... */
  2. #define TYPE            double          /* Type of numbers to work with */
  3.  
  4. #define VARLEN          15              /* Max length of variable names */
  5. #define MAXVARS         50              /* Max user-defined variables */
  6. #define TOKLEN          30              /* Max token length */
  7.  
  8. #define VAR             1
  9. #define DEL             2
  10. #define NUM             3
  11.  
  12.  
  13. typedef struct
  14. {
  15.    char name[VARLEN + 1];               /* Variable name */
  16.    TYPE value;                          /* Variable value */
  17. } VARIABLE;
  18.  
  19. //    April 1995    RMD-    made C++ compatible
  20. typedef float (*FunctionPtr)(float, float, float);
  21.  
  22. typedef struct
  23. {
  24.    char*        name;                    /* Function name */
  25.    short        args;                    /* Number of arguments to expect */
  26.    FunctionPtr  func;                     /* Pointer to function */
  27. //   TYPE  (*func)();                     /* Pointer to function */
  28. } FUNCTION;
  29.  
  30. /* The following macros are ASCII dependant, no EBCDIC here! */
  31. #define iswhite(c)  (c == ' ' || c == '\t')
  32. #define isnumer(c)  ((c >= '0' && c <= '9') || c == '.')
  33. #define isalpha(c)  ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') \
  34.                     || c == '_')
  35. #define isdelim(c)  (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' \
  36.                     || c == '^' || c == '(' || c == ')' || c == ',' || c == '=')
  37.  
  38. /* Codes returned from the evaluator */
  39. #define E_OK           0        /* Successful evaluation */
  40. #define E_SYNTAX       1        /* Syntax error */
  41. #define E_UNBALAN      2        /* Unbalanced parenthesis */
  42. #define E_DIVZERO      3        /* Attempted division by zero */
  43. #define E_UNKNOWN      4        /* Reference to unknown variable */
  44. #define E_MAXVARS      5        /* Maximum variables exceeded */
  45. #define E_BADFUNC      6        /* Unrecognised function */
  46. #define E_NUMARGS      7        /* Wrong number of arguments to funtion */
  47. #define E_NOARG        8        /* Missing an argument to a funtion */
  48. #define E_EMPTY        9        /* Empty expression */
  49.  
  50.  
  51. /*************************************************************************
  52. **                                                                       **
  53. ** PROTOTYPES FOR CUSTOM MATH FUNCTIONS                                  **
  54. **                                                                       **
  55.  *************************************************************************/
  56. #ifndef M_PI
  57. #define M_PI    3.14159265358979323846
  58. #endif
  59. #ifndef M_E
  60. #define M_E     2.71828182845904523536
  61. #endif
  62.  
  63. double    deg( double x );
  64. double    rad( double x );
  65.  
  66. //    April 1995    RMD-    Add ANSI prototypes
  67. class CEquation {
  68. public:
  69.     CEquation();
  70.     ~CEquation();
  71.     Boolean        Evaluate( char* e, TYPE* result, short* a );
  72.     void        ClearAllVars(void);
  73.     void        ListVariables(void);
  74.     void        ListConstants(void);
  75.     void        ReportError( void );
  76.  
  77. protected:
  78.     void        Parse(void);
  79.     short        GetSymbol( char* s, TYPE* v );
  80.     Boolean        ClearVar( char* name );
  81.     Boolean        GetValue( char* name, TYPE* value );
  82.     Boolean        SetValue( char* name, TYPE* value );
  83.     Boolean        Level1( TYPE* r );
  84.     void        Level2( TYPE* r );
  85.     void        Level3( TYPE* r );
  86.     void        Level4( TYPE* r );
  87.     void        Level5( TYPE* r );
  88.     void        Level6( TYPE* r );
  89.  
  90.     char*        mEquation[256];            //    
  91.     char*          mSource;                  //  Pointer to the expression
  92.     char           mToken[TOKLEN + 1];        //  Holds the current token
  93.     short        mTokenType;                //    Type of the current token
  94.     VARIABLE       mVars[MAXVARS];           //    Array for user-defined variables
  95.  
  96.     static        FUNCTION    Funcs[];
  97.     static        VARIABLE    Consts[];
  98.     static        char*        ErrMsgs[];
  99.  
  100.     short        mErrorCode;
  101.     char*        mErrorFrom;
  102. };
  103.  
  104.  
  105.