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>

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.
 
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.
 
double eval (std::map< std::string, double > *vars=0)
 Evalate the previously compiled expression using variables specified in vars.
 
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.
 

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.
 
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.
 

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.
 

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 .

Definition at line 107 of file shunting_yard.h.

Member Function Documentation

◆ cleanRPN()

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

◆ RPN_to_string()

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

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).