Class calculator (o2scl)¶
-
class
o2scl
::
calculator
¶ Evaluate a mathematical expression in a string.
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 .
- 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.
Public Functions
-
~calculator
()¶
-
calculator
()¶ Create an empty calculator object.
-
calculator
(const char *expr, const std::map<std::string, double> *vars = 0, bool debug = false, std::map<std::string, int> opPrec = opPrecedence)¶ Compile expression
expr
using variables specified invars
.
-
void
compile
(const char *expr, const std::map<std::string, double> *vars = 0, bool debug = false, std::map<std::string, int> opPrec = opPrecedence)¶ Compile expression
expr
using variables specified invars
and return an integer to indicate success or failure.
-
int
compile_nothrow
(const char *expr, const std::map<std::string, double> *vars = 0, bool debug = false, std::map<std::string, int> opPrec = opPrecedence)¶ Compile expression
expr
using variables specified invars
and return an integer to indicate success or failure.
-
double
eval
(const std::map<std::string, double> *vars = 0)¶ Evalate the previously compiled expression using variables specified in
vars
.
-
int
eval_nothrow
(const std::map<std::string, double> *vars, double &result)¶ Evalate the previously compiled expression using variables specified in
vars
.
-
std::string
RPN_to_string
()¶ Convert the RPN expression to a string.
- Note
This is mostly useful for debugging
-
TokenQueue_t
get_RPN
()¶ Get a copy of the RPN version.
- Warning
This copy contains raw pointers which are invalid if the changed.
-
std::vector<std::string>
get_var_list
()¶ Get the variable list.
Public Static Functions
-
double
calculate
(const char *expr, const std::map<std::string, double> *vars = 0, bool debug = false)¶ Compile and evaluate
expr
using definitions invars
.
-
int
calculate_nothrow
(const char *expr, const std::map<std::string, double> *vars, bool debug, double &result)¶ Compile and evaluate
expr
using definitions invars
and return an integer to indicate success or failure.
Private Members
-
TokenQueue_t
RPN
¶ The current expression in RPN.
Private Static Functions
-
std::map<std::string, int>
buildOpPrecedence
()¶ Build the operator precedence map.
-
bool
isvariablechar
(char c)¶ Return true if
is
a variable.
-
double
calculate
(TokenQueue_t RPN, const std::map<std::string, double> *vars = 0)¶ Compile and evaluate the expression in
RPN
using definitions invars
.
-
int
calculate_nothrow
(TokenQueue_t RPN, const std::map<std::string, double> *vars, double &result)¶ Compile and evaluate the expression in
RPN
using definitions invars
and return an integer to indicate success or failure.
-
void
cleanRPN
(TokenQueue_t &rpn)¶ Empty and free memory associated with
rpn
.- Note
This is called by the destructor to free the memory in RPN .
-
TokenQueue_t
toRPN
(const char *expr, const std::map<std::string, double> *vars, bool debug = false, std::map<std::string, int> opPrec = opPrecedence)¶ Convert the expression in
expr
to RPN.
-
int
toRPN_nothrow
(const char *expr, const 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.
Private Static Attributes
-
std::map<std::string, int>
opPrecedence
¶ A map denoting operator precedence.