| [2510] | 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
|  | 2 | // Classe Evaluateur RPN (HP like) | 
|---|
|  | 3 | //           Mars 2004: Extrait de commander.cc | 
|---|
|  | 4 | // LAL-IN2P3/CNRS  DAPNIA/CEA | 
|---|
|  | 5 |  | 
|---|
|  | 6 | #ifndef RPNEVALUATOR_H_SEEN | 
|---|
|  | 7 | #define RPNEVALUATOR_H_SEEN | 
|---|
|  | 8 |  | 
|---|
|  | 9 | #include "machdefs.h" | 
|---|
|  | 10 | #include "pexceptions.h" | 
|---|
|  | 11 | #include <string> | 
|---|
|  | 12 | #include <vector> | 
|---|
|  | 13 | #include <stack> | 
|---|
|  | 14 |  | 
|---|
|  | 15 | namespace SOPHYA { | 
|---|
|  | 16 |  | 
|---|
| [2598] | 17 | /*! | 
|---|
|  | 18 | \ingroup SysTools | 
|---|
|  | 19 | \brief Exception class used by  RPNExpressionEvaluator | 
|---|
|  | 20 | \sa RPNExpressionEvaluator | 
|---|
|  | 21 | */ | 
|---|
| [2510] | 22 | class RPNExprException : public PException { | 
|---|
|  | 23 | public: | 
|---|
| [3586] | 24 | explicit RPNExprException(const char * m) throw() : PException(m) {} | 
|---|
|  | 25 | explicit RPNExprException(const string& m) throw() :  PException(m) {} | 
|---|
| [2510] | 26 | }; | 
|---|
|  | 27 |  | 
|---|
|  | 28 |  | 
|---|
|  | 29 | //! Class for evaluation of arithmetic expressions in RPN (Reverse Polish Notation) | 
|---|
|  | 30 | class RPNExpressionEvaluator { | 
|---|
|  | 31 | public: | 
|---|
|  | 32 | explicit RPNExpressionEvaluator(string const & sex); | 
|---|
|  | 33 | explicit RPNExpressionEvaluator(vector<string> & exe, int off=0); | 
|---|
|  | 34 | virtual ~RPNExpressionEvaluator(); | 
|---|
| [2598] | 35 | //! Return the stack top \c (x) as the result of the expression evaluation | 
|---|
| [2510] | 36 | virtual double Evaluate() const;  // Return the value of the stack top | 
|---|
| [2598] | 37 | //! Alias for Evaluate() | 
|---|
| [2510] | 38 | inline double Value() const { return Evaluate(); } | 
|---|
|  | 39 |  | 
|---|
| [2598] | 40 | private: | 
|---|
| [2510] | 41 | int EvalRPNExpr(vector<string> & args, int off=0); | 
|---|
|  | 42 | inline bool CheckStack(double& x) const | 
|---|
|  | 43 | { | 
|---|
|  | 44 | if (rpnstack_.empty()) return true; | 
|---|
|  | 45 | else { x = rpnstack_.top();  return false; } | 
|---|
|  | 46 | } | 
|---|
|  | 47 | inline bool CheckStack(double& x, double& y) | 
|---|
|  | 48 | { | 
|---|
|  | 49 | if (rpnstack_.size() < 2) return true; | 
|---|
|  | 50 | else { | 
|---|
|  | 51 | x = rpnstack_.top(); rpnstack_.pop(); | 
|---|
|  | 52 | y = rpnstack_.top();  return false; | 
|---|
|  | 53 | } | 
|---|
|  | 54 | } | 
|---|
|  | 55 |  | 
|---|
|  | 56 | void PrintStack(); | 
|---|
|  | 57 | int SumStack(double& sx, double& sx2); | 
|---|
|  | 58 | int ProductStack(double& px); | 
|---|
|  | 59 |  | 
|---|
|  | 60 | stack<double> rpnstack_; | 
|---|
|  | 61 | }; | 
|---|
|  | 62 |  | 
|---|
|  | 63 | } // namespace SOPHYA | 
|---|
|  | 64 |  | 
|---|
|  | 65 | /* end of ifdef  RPNEVALUATOR_H_SEEN */ | 
|---|
|  | 66 | #endif | 
|---|