LISTING 1. Header File for FuncEval Object /***************************************** * * * FuncEval.hpp * * * * Definition file for FuncEval class. * * * * Implementation in FuncEval.cpp. * * * ****************************************** * * * Copyright 1995 Randy C. Finch * * * *****************************************/ #ifndef CLASS_FUNCEVAL #define CLASS_FUNCEVAL /*-------------- INCLUDES ----------------*/ #include #include #include #include #include #include /*---------------- DEFINES -----------------*/ /* Variable Types */ typedef unsigned char UCHAR; typedef unsigned int UINT; typedef unsigned long ULONG; /* Errors */ #define MISPLACEDOP 1 /* Misplaced operator */ #define ILLEGALCHAR 2 /* Illegal character */ #define ILLEGALEXP 3 /* Illegal exponent */ #define ILLEGALFUNC 4 /* Illegal function */ #define MISSINGOP 5 /* Missing operator */ #define MISSINGOPRP 6 /* Missing operator or right parenthesis */ #define MISSINGLP 7 /* Missing left parenthesis */ #define MISSINGRP 8 /* Missing right parenthesis */ #define MISSINGPARM 9 /* Missing parameter */ #define LONEDECIMAL 10 /* Lone decimal point */ #define EXTRADECIMAL 11 /* Extra decimal point */ #define EXTRAE 12 /* Extra E in exponent */ #define STACKUNDERFLOW 13 /* Stack underflow */ #define STACKOVERFLOW 14 /* Stack overflow */ #define TOOMANYCONST 15 /* Too many constants in function */ /* Constants in formula */ #define NUMSYM 128 /* Number of constants allowed in function */ #define SYMBASE 128 /* Base value for constants symbols */ /* Stack size */ #define STACKSIZE 256 /* Stack size */ /* Functions in formula */ #define SIN 1 /* Symbol for sine */ #define COS 2 /* Symbol for cosine */ #define TAN 3 /* Symbol for tangent */ #define ASIN 4 /* Symbol for arcsine */ #define ACOS 5 /* Symbol for arccosine */ #define ATAN 6 /* Symbol for arctangent */ #define SINH 7 /* Symbol for hyperbolic sine */ #define COSH 8 /* Symbol for hyperbolic cosine */ #define TANH 9 /* Symbol for hyperbolic tangent */ #define EXP 10 /* Symbol for exponential */ #define SQRT 11 /* Symbol for square root */ #define LN 12 /* Symbol for natural logarithm */ #define LOG 13 /* Symbol for logarithn base 10 */ /* Logical stuff */ #define TRUE 1 /* Symbol for true condition */ #define FALSE 0 /* Symbol for false condition */ /*----------- STRUCTURE DEFINITIONS ----------*/ struct CharStackType { UCHAR c[STACKSIZE]; long top; }; typedef struct CharStackType CharStack; struct NumStackType { double n[STACKSIZE]; long top; }; typedef struct NumStackType NumStack; /*------------ CLASS DEFINITION -------------*/ class FuncEval { public: FuncEval(); // Constructor double Evaluate(double x, double y); int Convert(UCHAR *FunctionString); int GetSyntaxErrNum(); char *GetSyntaxErrMessage(); int GetErrPosition(); private: int SyntaxErr; char *ErrMessage; int ErrPosition; CharStack cstack; NumStack nstack; double Constants[NUMSYM]; UCHAR CurConstant; UCHAR fstr[512]; UCHAR NewExpr[256]; char CharInStr(UCHAR *s, UCHAR c); void Deposit(double num); void Substitute(UCHAR symb, UCHAR *ptr, ULONG len); void RemoveSpaces(UCHAR *str); void AddZero(UCHAR *ptr); UCHAR CPop(); char CPush(UCHAR c); UCHAR CTopOfStack(); double NPop(); void NPush(double n); char IsFunction(UCHAR c); char IsSymbol(UCHAR c); char Precedence(UCHAR c1, UCHAR c2); UCHAR *CheckSyntax(UCHAR *str); char ConvertConstants(UCHAR *str); void ConvertFunctions(UCHAR *str); char InfixToPostfix(UCHAR *str); double Calculate(UCHAR s, double n2, double n1); }; #endif