Class calc_utf8 (o2scl)

O2scl : Class List

class o2scl::calc_utf8

Evaluate a mathematical expression stored in a UTF8 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 .

Todo

In class calc_utf8:

  • Future: Add functions atan2, cot, csc, ceil, floor, max, min,

and maybe if?

  • Future: There is some code duplication across the

functions, especially with regard to conversion between UTF8 and char32, which could be removed.

Token types

std::map<std::string, int> op_precedence

A map denoting operator precedence.

token_queue_t RPN

The current expression in RPN.

static const int token_none = 0
static const int token_op = 1
static const int token_var = 2
static const int token_num = 3
std::map<std::string, int> build_op_precedence()

Build the operator precedence map.

bool is_variable_char(const char32_t c)

Return true if is a variable.

int calc_RPN_nothrow(token_queue_t RPN, const std::map<std::u32string, double> *vars, double &result)

Compile and evaluate the expression in RPN using definitions in vars and return an integer to indicate success or failure.

void cleanRPN(token_queue_t &rpn)

Empty and free memory associated with rpn.

Note

This is called by the destructor to free the memory in RPN .

int toRPN_nothrow(const std::u32string &expr, const std::map<std::u32string, double> *vars, std::map<std::string, int> opPrec, token_queue_t &queue2)

Convert the expression in expr to RPN and return an integer to indicate success or failure.

calc_utf8()

Create an empty calc_utf8 object.

calc_utf8(const std::u32string &expr, const std::map<std::u32string, double> *vars = 0)

Compile expression expr using variables specified in vars.

~calc_utf8()

Evaluate functions

int verbose

Verbosity parameter.

double eval_char32(const std::map<std::u32string, double> *vars = 0)

Evalate the previously compiled expression using variables specified in vars.

int eval_char32_nothrow(const std::map<std::u32string, double> *vars, double &result)

Evalate the previously compiled expression using variables specified in vars.

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.

Compile and evaluate

double calculate(const std::u32string &expr, const std::map<std::u32string, double> *vars = 0)

Compile and evaluate expr using definitions in vars.

double calculate(const std::string &expr, const std::map<std::string, double> *vars = 0)

Compile and evaluate expr using definitions in vars.

int calculate_nothrow(const std::u32string &expr, const std::map<std::u32string, double> *vars, double &result)

Compile and evaluate expr using definitions in vars and return an integer to indicate success or failure.

int calculate_nothrow(const std::string &expr, const std::map<std::string, double> *vars, double &result)

Compile and evaluate expr using definitions in vars and return an integer to indicate success or failure.

Compile functions

void compile(const std::u32string &expr, const std::map<std::u32string, double> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

int compile_nothrow(const std::u32string &expr, const std::map<std::u32string, double> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

void compile(const std::string &expr, const std::map<std::string, double> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

int compile_nothrow(const std::string &expr, const std::map<std::string, double> *vars = 0)

Compile expression expr using variables specified in vars and return an integer to indicate success or failure.

Other functions

std::string RPN_to_string()

Convert the RPN expression to a string.

Note

This is mostly useful for debugging

token_queue_t get_RPN()

Get a copy of the RPN version.

Warning

This copy contains raw pointers which are invalid if changed.

std::vector<std::u32string> get_var_list()

Get the variable list.

Protected Types

typedef std::queue<token_base*> token_queue_t

A typedef for a queue of tokens for o2scl::calculator.

Protected Attributes

rng r
template<class T>
class token32 : public o2scl::calc_utf8::token_base

Token class for o2scl::calc_utf8.

Public Functions

inline token32(T t, int typex)

Create a token of type type with value t.

Public Members

T val

The actual value stored.

struct token_base

Token base data type for o2scl::calc_utf8.

Subclassed by o2scl::calc_utf8::token32< T >

Public Functions

inline virtual ~token_base()

Public Members

int type

The token type.