MathTypeLibrary(libmath++) 0.0.3
|
00001 00002 // Math Type Library 00003 // $Id: calculator.h,v 1.1 2002/04/20 06:39:18 cparpart Exp $ 00004 // (defines the interface for the function calculator) 00005 // 00006 // Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net> 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Library General Public 00010 // License as published by the Free Software Foundation; either 00011 // version 2 of the License, or (at your option) any later version. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // Library General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Library General Public License 00019 // along with this library; see the file COPYING.LIB. If not, write to 00020 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00021 // Boston, MA 02111-1307, USA. 00023 #ifndef libmath_calculator_h 00024 #define libmath_calculator_h 00025 00026 #include <math++/visitor.h> 00027 #include <math++/error.h> 00028 00029 #include <string> 00030 #include <map> 00031 00032 namespace math { 00033 00034 template<class> class TFunction; 00035 template<class> class TLibrary; 00036 00041 class ECalcError : public EMath { 00042 public: 00043 ECalcError(const std::string& AReason) : EMath(AReason) {} 00044 }; 00045 00050 template<class T> 00051 class TCalculator : protected TNodeVisitor<T> { 00052 public: 00054 static T calculate(const TFunction<T>& AFunction, const T& AParam, 00055 const TLibrary<T>& ALibrary, unsigned ARecursionLimit = 64); 00056 00057 private: 00058 T FParam; 00059 const TLibrary<T>& FLibrary; 00060 std::map<std::string, unsigned> FRecursions; 00061 unsigned FLimit; 00062 T FResult; 00063 00064 private: 00066 TCalculator(const TFunction<T>& AFunction, const T& AParam, 00067 const TLibrary<T>& ALibrary, unsigned ALimit); 00068 00070 T calculate(const TNode<T> *AExpression); 00071 00072 virtual void visit(TNumberNode<T> *); 00073 virtual void visit(TSymbolNode<T> *); 00074 virtual void visit(TParamNode<T> *); 00075 00076 virtual void visit(TPlusNode<T> *); 00077 virtual void visit(TNegNode<T> *); 00078 00079 virtual void visit(TMulNode<T> *); 00080 virtual void visit(TDivNode<T> *); 00081 00082 virtual void visit(TPowNode<T> *); 00083 virtual void visit(TSqrtNode<T> *); 00084 00085 virtual void visit(TSinNode<T> *); 00086 virtual void visit(TCosNode<T> *); 00087 virtual void visit(TTanNode<T> *); 00088 virtual void visit(TLnNode<T> *); 00089 00090 virtual void visit(TFuncNode<T> *); 00091 virtual void visit(TIfNode<T> *); 00092 00093 virtual void visit(TEquNode<T> *); 00094 virtual void visit(TUnEquNode<T> *); 00095 virtual void visit(TGreaterNode<T> *); 00096 virtual void visit(TLessNode<T> *); 00097 virtual void visit(TGreaterEquNode<T> *); 00098 virtual void visit(TLessEquNode<T> *); 00099 }; 00100 00101 } // namespace math 00102 00103 #include <math++/calculator.tcc> 00104 00105 #endif