source: Sophya/trunk/SophyaLib/SysTools/rpneval.h@ 3321

Last change on this file since 3321 was 2598, checked in by ansari, 21 years ago

Documentation (ajoutee ou completee) pour les classes du module SysTools - Reza 11 Aout 2004

File size: 1.7 KB
Line 
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
15namespace SOPHYA {
16
17/*!
18 \ingroup SysTools
19 \brief Exception class used by RPNExpressionEvaluator
20 \sa RPNExpressionEvaluator
21*/
22class RPNExprException : public PException {
23 public:
24 explicit RPNExprException(const char * m) : PException(m) {}
25 explicit RPNExprException(const string& m) : PException(m) {}
26};
27
28
29//! Class for evaluation of arithmetic expressions in RPN (Reverse Polish Notation)
30class RPNExpressionEvaluator {
31public:
32 explicit RPNExpressionEvaluator(string const & sex);
33 explicit RPNExpressionEvaluator(vector<string> & exe, int off=0);
34 virtual ~RPNExpressionEvaluator();
35//! Return the stack top \c (x) as the result of the expression evaluation
36 virtual double Evaluate() const; // Return the value of the stack top
37//! Alias for Evaluate()
38 inline double Value() const { return Evaluate(); }
39
40private:
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
Note: See TracBrowser for help on using the repository browser.