Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
o2scl::calculator Class Reference

Evaluate a mathematical expression in a string. More...

#include <shunting_yard.h>

Detailed Description

This is based on Brandon Amos' code at https://github.com/bamos/cpp-expression-parser in turn based on Jesse Brown's code at http://www.daniweb.com/software-development/cpp/code/427500/calculator-using-shunting-yard-algorithm .

The original code has been modified for use in O2scl .

Idea for Future:
Add functions atan2, cot, csc, ceil, floor, int, max, min, and maybe if?
Warning
The nothrow() functions are a naive attempt at more detailed error handling than Amos' original code. I have tried to make sure they don't create any memory leaks, but I have not fully tested this.

Definition at line 119 of file shunting_yard.h.

Public Member Functions

 calculator ()
 Create an empty calculator object.
 
 calculator (const char *expr, std::map< std::string, double > *vars=0, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
 Compile expression expr using variables specified in vars. More...
 
void compile (const char *expr, std::map< std::string, double > *vars=0, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
 Compile expression expr using variables specified in vars and return an integer to indicate success or failure.
 
int compile_nothrow (const char *expr, std::map< std::string, double > *vars=0, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
 Compile expression expr using variables specified in vars and return an integer to indicate success or failure.
 
double eval (std::map< std::string, double > *vars=0)
 Evalate the previously compiled expression using variables specified in vars. More...
 
int eval_nothrow (std::map< std::string, double > *vars, double &result)
 Evalate the previously compiled expression using variables specified in vars. More...
 
std::string RPN_to_string ()
 Convert the RPN expression to a string. More...
 

Static Public Member Functions

static double calculate (const char *expr, std::map< std::string, double > *vars=0, bool debug=false)
 Compile and evaluate expr using definitions in vars. More...
 
static int calculate_nothrow (const char *expr, std::map< std::string, double > *vars, bool debug, double &result)
 Compile and evaluate expr using definitions in vars and return an integer to indicate success or failure.
 

Static Private Member Functions

static std::map< std::string, int > buildOpPrecedence ()
 Build the operator precedence map.
 
static bool isvariablechar (char c)
 Return true if is a variable.
 
static double calculate (TokenQueue_t RPN, std::map< std::string, double > *vars=0)
 Compile and evaluate the expression in RPN using definitions in vars. More...
 
static int calculate_nothrow (TokenQueue_t RPN, std::map< std::string, double > *vars, double &result)
 Compile and evaluate the expression in RPN using definitions in vars and return an integer to indicate success or failure. More...
 
static void cleanRPN (TokenQueue_t &rpn)
 Empty and free memory associated with rpn. More...
 
static TokenQueue_t toRPN (const char *expr, std::map< std::string, double > *vars, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
 Convert the expression in expr to RPN.
 
static int toRPN_nothrow (const char *expr, std::map< std::string, double > *vars, bool debug, std::map< std::string, int > opPrec, TokenQueue_t &queue2)
 Convert the expression in expr to RPN and return an integer to indicate success or failure. More...
 

Private Attributes

TokenQueue_t RPN
 The current expression in RPN.
 

Static Private Attributes

static std::map< std::string, int > opPrecedence
 A map denoting operator precedence.
 

Constructor & Destructor Documentation

◆ calculator()

o2scl::calculator::calculator ( const char *  expr,
std::map< std::string, double > *  vars = 0,
bool  debug = false,
std::map< std::string, int >  opPrec = opPrecedence 
)

Member Function Documentation

◆ calculate() [1/2]

static double o2scl::calculator::calculate ( const char *  expr,
std::map< std::string, double > *  vars = 0,
bool  debug = false 
)
static

◆ calculate() [2/2]

static double o2scl::calculator::calculate ( TokenQueue_t  RPN,
std::map< std::string, double > *  vars = 0 
)
staticprivate

◆ calculate_nothrow()

static int o2scl::calculator::calculate_nothrow ( TokenQueue_t  RPN,
std::map< std::string, double > *  vars,
double &  result 
)
staticprivate

◆ cleanRPN()

static void o2scl::calculator::cleanRPN ( TokenQueue_t rpn)
staticprivate
Note
This is called by the destructor to free the memory in RPN .

◆ eval()

double o2scl::calculator::eval ( std::map< std::string, double > *  vars = 0)

◆ eval_nothrow()

int o2scl::calculator::eval_nothrow ( std::map< std::string, double > *  vars,
double &  result 
)

◆ RPN_to_string()

std::string o2scl::calculator::RPN_to_string ( )
Note
This is mostly useful for debugging

◆ toRPN_nothrow()

static int o2scl::calculator::toRPN_nothrow ( const char *  expr,
std::map< std::string, double > *  vars,
bool  debug,
std::map< std::string, int >  opPrec,
TokenQueue_t queue2 
)
staticprivate

The documentation for this class was generated from the following file:

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).