The script contains a class, that can be used to solve mathematical and logical expressions, defined as string.
The script has two classes: cTokenBase and cToken (a member of CTokenBase). The cTokenBase class must be configured before its use (see below).
The following MQL5 mathematical functions are supported: abs, arccos, arcsin, arctan, ceil, cos, exp, floor, log, log10, max, min, mod, pow, rand, round, sin, sqrt, tan.
Logical and math operations: Â /, %, *, +, -, >, <, >=, <=, ==, !=, &&, ||.
The expression may contain numbers, including double numbers, user variables and user arrays. If you will use the user’s variables and arrays, you need to add functions, that returns the values of these variables and arrays. The functions should be added into the cTokenBase class.
The user variables and arrays are named with letters (the case isn’t important), the array elements are defined as follows: e[0], e[1], f[0], f[1].
Examples:
How to configure cTokenBase class
It’s neccessary to register the name of the user variables and arrays. It must be done in the UsersVariables() function. The list of user variables/arrays contain the names, separated by “;”.
      void UsersVariables() {         UserVariables="a;b;c;d"; // list of user's variables         UserArrays="e;f"; // list of user's arrays      }
2. You need to add a call of the corresponding function to the UserFunc(string FuncName) function:
      string UserFunc(string FuncName) {         if(FuncName=="a")return(a());         if(FuncName=="b")return(b());         if(FuncName=="c")return(c());         if(FuncName=="d")return(d());         Alert("Function for "+FuncName+" variable is not defined");         return("0");       }
3. Add functions for all of the user’s variables:
      string a() {         return("1");       }       string b() {         return("2");       }       string c() {         return("3");       }       string d() {         return("4");       }
4. Add call of the corresponding function (dependent on ArrName) to the UserArray(string ArrName,int aIndex) function. The index of array element is specified in aIndex variable.
      string UserArray(string ArrName,int aIndex){         if(ArrName=="e")return(e(aIndex));         if(ArrName=="f")return(f(aIndex));         Alert("Function for "+ArrName+" array is not defined");         return("0");       }
5. Add functions for each of the arrays:
      string e(int Index) {         string v[]={"1","2","3","4","5","6","7","8","9"};         return(v[Index]);       }       string f(int Index) {         string v[]={"10","20","30","40","50","60","70","80","90"};         return(v[Index]);       }
Use of the class
1. Declare an external input variable for the expresssion:
input string Expression="enter expression here";
2. Delcare a cToken class variable on the global level:
cToken token;
3. Initialize class with the expression.
  token.Init(Expression);
4. Call SolveExpression() method
double Value=token.SolveExpression();
The class can be used to solve several expressions, just use the several instances of cToken class:
cToken token1; cToken token2; cToken token3; cToken token4;
In this case all the class instances will use the same user variables and arrays (declared in cTokenBase class)
You can add your functions.
Adding new functions to cToken class
- Register a new function. It must be done in the Init() function.
- Add the calculation code to the SolveFunc(string Func,string & aRes[]) function. The function arguments are located in aRes[] array. The number of arguments isn’t limited.