TTMath  0.9.4
 C++ bignum library
Data Structures | Public Member Functions | Data Fields
ttmath::Parser< ValueType > Class Template Reference

Mathematical parser. More...

#include <ttmathparser.h>

Collaboration diagram for ttmath::Parser< ValueType >:
Collaboration graph
[legend]

Data Structures

struct  Item
 

Public Member Functions

bool GetValueOfUserDefinedVariable (const std::string &variable_name, ValueType &result)
 
bool GetValueOfFunctionLocalVariable (const std::string &variable_name, ValueType &result)
 
ValueType GetValueOfVariable (const std::string &variable_name)
 
 Parser ()
 
Parser< ValueType > & operator= (const Parser< ValueType > &p)
 
 Parser (const Parser< ValueType > &p)
 
void SetBase (int b)
 
void SetDegRadGrad (int angle)
 
void SetStopObject (const volatile StopCalculating *ps)
 
void SetVariables (const Objects *pv)
 
void SetFunctions (const Objects *pf)
 
void SetGroup (int g)
 
void SetComma (int c, int c2=0)
 
void SetParamSep (int s)
 
ErrorCode Parse (const char *str)
 
ErrorCode Parse (const std::string &str)
 
ErrorCode Parse (const wchar_t *str)
 
ErrorCode Parse (const std::wstring &str)
 
bool Calculated ()
 
void InitCGamma ()
 

Data Fields

std::vector< Itemstack
 

Detailed Description

template<class ValueType>
class ttmath::Parser< ValueType >

Mathematical parser.

let x will be an input string meaning an expression for converting:

x = [+|-]Value[operator[+|-]Value][operator[+|-]Value]... where: an operator can be: ^ (pow) (the heighest priority)

(mul) (or multiplication without an operator – short mul) / (div) (* and / have the same priority)

< (lower than) > (greater than) <= (lower or equal than) >= (greater or equal than) == (equal) != (not equal) (all above logical operators have the same priority)

&& (logical and)

|| (logical or) (the lowest priority)

short mul: if the second Value (Var below) is either a variable or function there might not be an operator between them, e.g. "[+|-]Value Var" is treated as "[+|-]Value * Var" and the multiplication has the same priority as a normal multiplication: 4x = 4 * x 2^3m = (2^3)* m 6h^3 = 6 * (h^3) 2sin(pi) = 2 * sin(pi) etc.

Value can be: constant e.g. 100, can be preceded by operators for changing the base (radix): [#|&]

- hex

& - bin sample: #10 = 16 &10 = 2 variable e.g. pi another expression between brackets e.g (x) function e.g. sin(x)

for example a correct input string can be: "1" "2.1234" "2,1234" (they are the same, by default we can either use a comma or a dot) "1 + 2" "(1 + 2) * 3" "pi" "sin(pi)" "(1+2)*(2+3)" "log(2;1234)" there's a semicolon here (not a comma), we use it in functions for separating parameters "1 < 2" (the result will be: 1) "4 < 3" (the result will be: 0) "2+x" (of course if the variable 'x' is defined) "4x+10" "#20+10" = 32 + 10 = 42 "10 ^ -&101" = 10 ^ -5 = 0.00001 "8 * -&10" = 8 * -2 = -16 etc.

we can also use a semicolon for separating any 'x' input strings for example: "1+2;4+5" the result will be on the stack as follows: stack[0].value=3 stack[1].value=9

Definition at line 138 of file ttmathparser.h.

Constructor & Destructor Documentation

§ Parser() [1/2]

template<class ValueType>
ttmath::Parser< ValueType >::Parser ( )

the default constructor

Definition at line 2527 of file ttmathparser.h.

2527  : default_stack_size(100)
2528 {
2529  pstop_calculating = 0;
2530  puser_variables = 0;
2531  puser_functions = 0;
2532  pfunction_local_variables = 0;
2533  base = 10;
2534  deg_rad_grad = 1;
2535  error = err_ok;
2536  group = 0;
2537  comma = '.';
2538  comma2 = ',';
2539  param_sep = 0;
2540 
2541  CreateFunctionsTable();
2542  CreateVariablesTable();
2543  CreateMathematicalOperatorsTable();
2544 }

§ Parser() [2/2]

template<class ValueType>
ttmath::Parser< ValueType >::Parser ( const Parser< ValueType > &  p)

the copying constructor

Definition at line 2582 of file ttmathparser.h.

References ttmath::Parser< ValueType >::operator=().

2582  : default_stack_size(p.default_stack_size)
2583 {
2584  operator=(p);
2585 }
Parser< ValueType > & operator=(const Parser< ValueType > &p)

Member Function Documentation

§ Calculated()

template<class ValueType>
bool ttmath::Parser< ValueType >::Calculated ( )

this method returns true is something was calculated (at least one mathematical operator was used or a function or variable) e.g. true if the string to Parse() looked like this: "1+1" "2*3" "sin(5)"

if the string was e.g. "678" the result is false

Definition at line 2752 of file ttmathparser.h.

2753 {
2754  return calculated;
2755 }

§ GetValueOfFunctionLocalVariable()

template<class ValueType>
bool ttmath::Parser< ValueType >::GetValueOfFunctionLocalVariable ( const std::string &  variable_name,
ValueType &  result 
)

this method returns the value of a local variable of a function

Definition at line 618 of file ttmathparser.h.

Referenced by ttmath::Parser< ValueType >::GetValueOfVariable().

619 {
620  if( !pfunction_local_variables )
621  return false;
622 
623  typename FunctionLocalVariables::const_iterator i = pfunction_local_variables->find(variable_name);
624 
625  if( i == pfunction_local_variables->end() )
626  return false;
627 
628  result = i->second;
629 
630 return true;
631 }

§ GetValueOfUserDefinedVariable()

template<class ValueType>
bool ttmath::Parser< ValueType >::GetValueOfUserDefinedVariable ( const std::string &  variable_name,
ValueType &  result 
)

this method returns the user-defined value of a variable

Definition at line 598 of file ttmathparser.h.

References ttmath::Objects::GetValue().

Referenced by ttmath::Parser< ValueType >::GetValueOfVariable().

599 {
600  if( !puser_variables )
601  return false;
602 
603  const char * string_value;
604 
605  if( puser_variables->GetValue(variable_name, &string_value) != err_ok )
606  return false;
607 
608  result = RecurrenceParsingVariablesOrFunction(true, variable_name, string_value);
609  calculated = true;
610 
611 return true;
612 }
ErrorCode GetValue(const std::string &name, std::string &value) const

§ GetValueOfVariable()

template<class ValueType>
ValueType ttmath::Parser< ValueType >::GetValueOfVariable ( const std::string &  variable_name)

this method returns the value of a variable from variables' table

we make an object of type ValueType then call a method which sets the correct value in it and finally we'll return the object

Definition at line 640 of file ttmathparser.h.

References ttmath::Abs(), ttmath::ACos(), ttmath::ACosh(), ttmath::ACot(), ttmath::ACoth(), ttmath::ASin(), ttmath::ASinh(), ttmath::Misc::AssignString(), ttmath::ATan(), ttmath::ATanh(), ttmath::Conv::base, ttmath::Ceil(), ttmath::Misc::CharToDigit(), ttmath::Conv::comma, ttmath::Conv::comma2, ttmath::Cos(), ttmath::Cosh(), ttmath::Cot(), ttmath::Coth(), ttmath::DegToDeg(), ttmath::DegToGrad(), ttmath::DegToRad(), ttmath::Exp(), ttmath::Factorial(), ttmath::Floor(), ttmath::Gamma(), ttmath::Objects::GetValueAndParam(), ttmath::Parser< ValueType >::GetValueOfFunctionLocalVariable(), ttmath::Parser< ValueType >::GetValueOfUserDefinedVariable(), ttmath::GradToDeg(), ttmath::GradToRad(), ttmath::Conv::group, ttmath::Ln(), ttmath::Log(), ttmath::Mod(), ttmath::RadToDeg(), ttmath::RadToGrad(), ttmath::Root(), ttmath::Round(), ttmath::Sgn(), ttmath::Sin(), ttmath::Sinh(), ttmath::SkipFraction(), ttmath::Sqrt(), ttmath::Tan(), and ttmath::Tanh().

641 {
642 ValueType result;
643 
644  if( GetValueOfFunctionLocalVariable(variable_name, result) )
645  return result;
646 
647  if( GetValueOfUserDefinedVariable(variable_name, result) )
648  return result;
649 
650 
651  typename std::map<std::string, pfunction_var>::iterator i =
652  variables_table.find(variable_name);
653 
654  if( i == variables_table.end() )
655  Error( err_unknown_variable );
656 
657  (result.*(i->second))();
658  calculated = true;
659 
660 return result;
661 }
bool GetValueOfFunctionLocalVariable(const std::string &variable_name, ValueType &result)
Definition: ttmathparser.h:618
bool GetValueOfUserDefinedVariable(const std::string &variable_name, ValueType &result)
Definition: ttmathparser.h:598

§ InitCGamma()

template<class ValueType>
void ttmath::Parser< ValueType >::InitCGamma ( )

initializing coefficients used when calculating the gamma (or factorial) function this speed up the next calculations you don't have to call this method explicitly these coefficients will be calculated when needed

Definition at line 2764 of file ttmathparser.h.

References ttmath::CGamma< ValueType >::InitAll().

2765 {
2766  cgamma.InitAll();
2767 }

§ operator=()

template<class ValueType>
Parser<ValueType>& ttmath::Parser< ValueType >::operator= ( const Parser< ValueType > &  p)

the assignment operator

Definition at line 2550 of file ttmathparser.h.

Referenced by ttmath::Parser< ValueType >::Parser().

2551 {
2552  pstop_calculating = p.pstop_calculating;
2553  puser_variables = p.puser_variables;
2554  puser_functions = p.puser_functions;
2555  pfunction_local_variables = 0;
2556  base = p.base;
2557  deg_rad_grad = p.deg_rad_grad;
2558  error = p.error;
2559  group = p.group;
2560  comma = p.comma;
2561  comma2 = p.comma2;
2562  param_sep = p.param_sep;
2563 
2564  /*
2565  we don't have to call 'CreateFunctionsTable()' etc.
2566  we can only copy these tables
2567  */
2568  functions_table = p.functions_table;
2569  variables_table = p.variables_table;
2570  operators_table = p.operators_table;
2571 
2572  visited_variables = p.visited_variables;
2573  visited_functions = p.visited_functions;
2574 
2575 return *this;
2576 }

§ Parse() [1/4]

template<class ValueType>
ErrorCode ttmath::Parser< ValueType >::Parse ( const char *  str)

the main method using for parsing string

Definition at line 2682 of file ttmathparser.h.

2683 {
2684  stack_index = 0;
2685  pstring = str;
2686  error = err_ok;
2687  calculated = false;
2688 
2689  stack.resize( default_stack_size );
2690 
2691  try
2692  {
2693  Parse();
2694  }
2695  catch(ErrorCode c)
2696  {
2697  error = c;
2698  calculated = false;
2699  }
2700 
2701  NormalizeStack();
2702 
2703 return error;
2704 }
std::vector< Item > stack
Definition: ttmathparser.h:296

§ Parse() [2/4]

template<class ValueType>
ErrorCode ttmath::Parser< ValueType >::Parse ( const std::string &  str)

the main method using for parsing string

Definition at line 2710 of file ttmathparser.h.

2711 {
2712  return Parse(str.c_str());
2713 }

§ Parse() [3/4]

template<class ValueType>
ErrorCode ttmath::Parser< ValueType >::Parse ( const wchar_t *  str)

the main method using for parsing string

Definition at line 2721 of file ttmathparser.h.

References ttmath::Misc::AssignString().

2722 {
2723  Misc::AssignString(wide_to_ansi, str);
2724 
2725 return Parse(wide_to_ansi.c_str());
2726 
2727  // !! wide_to_ansi clearing can be added here
2728 }
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72

§ Parse() [4/4]

template<class ValueType>
ErrorCode ttmath::Parser< ValueType >::Parse ( const std::wstring &  str)

the main method using for parsing string

Definition at line 2734 of file ttmathparser.h.

2735 {
2736  return Parse(str.c_str());
2737 }

§ SetBase()

template<class ValueType>
void ttmath::Parser< ValueType >::SetBase ( int  b)

the new base of mathematic system default is 10

Definition at line 2592 of file ttmathparser.h.

2593 {
2594  if( b>=2 && b<=16 )
2595  base = b;
2596 }

§ SetComma()

template<class ValueType>
void ttmath::Parser< ValueType >::SetComma ( int  c,
int  c2 = 0 
)

setting the main comma operator and the additional comma operator the additional operator can be zero (which means it is not used) default are: '.' and ','

Definition at line 2660 of file ttmathparser.h.

2661 {
2662  comma = c;
2663  comma2 = c2;
2664 }

§ SetDegRadGrad()

template<class ValueType>
void ttmath::Parser< ValueType >::SetDegRadGrad ( int  angle)

the unit of angles used in: sin,cos,tan,cot,asin,acos,atan,acot 0 - deg 1 - rad (default) 2 - grad

Definition at line 2605 of file ttmathparser.h.

2606 {
2607  if( angle >= 0 && angle <= 2 )
2608  deg_rad_grad = angle;
2609 }

§ SetFunctions()

template<class ValueType>
void ttmath::Parser< ValueType >::SetFunctions ( const Objects pf)

this method sets the new table of user-defined functions if you don't want any other functions just put zero value into the 'puser_functions' variable

(you can have only one table at the same time)

Definition at line 2639 of file ttmathparser.h.

2640 {
2641  puser_functions = pf;
2642 }

§ SetGroup()

template<class ValueType>
void ttmath::Parser< ValueType >::SetGroup ( int  g)

setting the group character default zero (not used)

Definition at line 2649 of file ttmathparser.h.

2650 {
2651  group = g;
2652 }

§ SetParamSep()

template<class ValueType>
void ttmath::Parser< ValueType >::SetParamSep ( int  s)

setting an additional character which is used as a parameters separator the main parameters separator is a semicolon (is used always)

this character is used also as a global separator

Definition at line 2673 of file ttmathparser.h.

2674 {
2675  param_sep = s;
2676 }

§ SetStopObject()

template<class ValueType>
void ttmath::Parser< ValueType >::SetStopObject ( const volatile StopCalculating ps)

this method sets a pointer to the object which tell us whether we should stop calculations

Definition at line 2615 of file ttmathparser.h.

2616 {
2617  pstop_calculating = ps;
2618 }

§ SetVariables()

template<class ValueType>
void ttmath::Parser< ValueType >::SetVariables ( const Objects pv)

this method sets the new table of user-defined variables if you don't want any other variables just put zero value into the 'puser_variables' variable

(you can have only one table at the same time)

Definition at line 2627 of file ttmathparser.h.

2628 {
2629  puser_variables = pv;
2630 }

Field Documentation

§ stack

template<class ValueType>
std::vector<Item> ttmath::Parser< ValueType >::stack

stack on which we're keeping the Items

at the end of parsing we'll have the result here the result don't have to be one value, it can be more than one if we have used a semicolon in the global space e.g. such input string "1+2;3+4" will generate a result: stack[0].value=3 stack[1].value=7

you should check if the stack is not empty, because if there was a syntax error in the input string then we do not have any results on the stack

Definition at line 296 of file ttmathparser.h.


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