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 |
|
---|
17 | //! Exception class used by RPNExpressionEvaluator
|
---|
18 |
|
---|
19 | class RPNExprException : public PException {
|
---|
20 | public:
|
---|
21 | explicit RPNExprException(const char * m) : PException(m) {}
|
---|
22 | explicit RPNExprException(const string& m) : PException(m) {}
|
---|
23 | };
|
---|
24 |
|
---|
25 |
|
---|
26 | //! Class for evaluation of arithmetic expressions in RPN (Reverse Polish Notation)
|
---|
27 | class RPNExpressionEvaluator {
|
---|
28 | public:
|
---|
29 | explicit RPNExpressionEvaluator(string const & sex);
|
---|
30 | explicit RPNExpressionEvaluator(vector<string> & exe, int off=0);
|
---|
31 | virtual ~RPNExpressionEvaluator();
|
---|
32 | virtual double Evaluate() const; // Return the value of the stack top
|
---|
33 | inline double Value() const { return Evaluate(); }
|
---|
34 |
|
---|
35 | protected:
|
---|
36 | int EvalRPNExpr(vector<string> & args, int off=0);
|
---|
37 | inline bool CheckStack(double& x) const
|
---|
38 | {
|
---|
39 | if (rpnstack_.empty()) return true;
|
---|
40 | else { x = rpnstack_.top(); return false; }
|
---|
41 | }
|
---|
42 | inline bool CheckStack(double& x, double& y)
|
---|
43 | {
|
---|
44 | if (rpnstack_.size() < 2) return true;
|
---|
45 | else {
|
---|
46 | x = rpnstack_.top(); rpnstack_.pop();
|
---|
47 | y = rpnstack_.top(); return false;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | void PrintStack();
|
---|
52 | int SumStack(double& sx, double& sx2);
|
---|
53 | int ProductStack(double& px);
|
---|
54 |
|
---|
55 | stack<double> rpnstack_;
|
---|
56 | };
|
---|
57 |
|
---|
58 | } // namespace SOPHYA
|
---|
59 |
|
---|
60 | /* end of ifdef RPNEVALUATOR_H_SEEN */
|
---|
61 | #endif
|
---|