codeplea / tinyexpr

tiny recursive descent expression parser, compiler, and evaluation engine for math expressions
https://codeplea.com/tinyexpr
zlib License
1.61k stars 245 forks source link

Function to determine the number of variables in an expression #98

Closed aeonSolutions closed 1 year ago

aeonSolutions commented 1 year ago

This is not an issue but a feature improvement instead. The function below will determine the number of variables in a string expression:

    String var[8];        // {"a","h","pt","dt","p","m","t","u"}; or any other
    String signs[9];     // {"*","(", ")","+","=","-","/","%","^"};

    uint8_t  get_number_model_vars(String equation){
    uint8_t count =0;

    //String var[8]= {"a","h","pt","dt","p","m","t","u"};
    //                 0   1    2    3   4   5   6   7

    for(int i=0; i< 8; i++){
        uint8_t idx = equation.indexOf(var[i]);
        bool isSignLeft=false;
        bool isSignRight=false;

        if (idx>-1 && idx !=255){

            for(int j=0; j< 8; j++){                
                if(idx>0){
                    if (String(signs[j]) == String(equation[idx-1]))
                        isSignLeft=true;
                }else{
                    isSignLeft=true;
                }

                if( (idx + signs[j].length()) < (equation.length() - 1 ) ){
                    if (String(signs[j]) == String(equation[idx + signs[j].length()]))
                        isSignRight=true;
                }else{
                    isSignRight=true;
                }
            }

            if (isSignRight && isSignLeft){
                count++;
            }
        }
    } //end for

    return count;
}
codeplea commented 1 year ago

I have no idea what you're trying to do or why.

This is a C library, by the way.

aeonSolutions commented 1 year ago

Just leaving it here, in case anyone finds it useful with this library.