MathTypeLibrary(libmath++)  0.0.3
calculator.h
1 // Math Type Library
3 // $Id: calculator.h,v 1.1 2002/04/20 06:39:18 cparpart Exp $
4 // (defines the interface for the function calculator)
5 //
6 // Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public License
19 // along with this library; see the file COPYING.LIB. If not, write to
20 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
23 #ifndef libmath_calculator_h
24 #define libmath_calculator_h
25 
26 #include <math++/visitor.h>
27 #include <math++/error.h>
28 
29 #include <string>
30 #include <map>
31 
32 namespace math {
33 
34 template<class> class TFunction;
35 template<class> class TLibrary;
36 
41 class ECalcError : public EMath {
42 public:
43  ECalcError(const std::string& AReason) : EMath(AReason) {}
44 };
45 
50 template<class T>
51 class TCalculator : protected TNodeVisitor<T> {
52 public:
54  static T calculate(const TFunction<T>& AFunction, const T& AParam,
55  const TLibrary<T>& ALibrary, unsigned ARecursionLimit = 64);
56 
57 private:
58  T FParam;
59  const TLibrary<T>& FLibrary;
60  std::map<std::string, unsigned> FRecursions;
61  unsigned FLimit;
62  T FResult;
63 
64 private:
66  TCalculator(const TFunction<T>& AFunction, const T& AParam,
67  const TLibrary<T>& ALibrary, unsigned ALimit);
68 
70  T calculate(const TNode<T> *AExpression);
71 
72  virtual void visit(TNumberNode<T> *);
73  virtual void visit(TSymbolNode<T> *);
74  virtual void visit(TParamNode<T> *);
75 
76  virtual void visit(TPlusNode<T> *);
77  virtual void visit(TNegNode<T> *);
78 
79  virtual void visit(TMulNode<T> *);
80  virtual void visit(TDivNode<T> *);
81 
82  virtual void visit(TPowNode<T> *);
83  virtual void visit(TSqrtNode<T> *);
84 
85  virtual void visit(TSinNode<T> *);
86  virtual void visit(TCosNode<T> *);
87  virtual void visit(TTanNode<T> *);
88  virtual void visit(TLnNode<T> *);
89 
90  virtual void visit(TFuncNode<T> *);
91  virtual void visit(TIfNode<T> *);
92 
93  virtual void visit(TEquNode<T> *);
94  virtual void visit(TUnEquNode<T> *);
95  virtual void visit(TGreaterNode<T> *);
96  virtual void visit(TLessNode<T> *);
97  virtual void visit(TGreaterEquNode<T> *);
98  virtual void visit(TLessEquNode<T> *);
99 };
100 
101 } // namespace math
102 
103 #include <math++/calculator.tcc>
104 
105 #endif
static T calculate(const TFunction< T > &AFunction, const T &AParam, const TLibrary< T > &ALibrary, unsigned ARecursionLimit=64)
calculates the functions result using given values.