Validate formulas

1

I have an array of operators [*, -, +, %] and another array for variables [FE, GE, PRD, RON] . In an input text, the user with both arrays can create formulas that is, FE + GE * RON for example.

How can I validate that the formula is well armed with operators and variables of both arrangements?

If someone has a suggestion it can be with JavaScript or C #

Beforehand, thank you very much

    
asked by ericardezp 24.11.2018 в 15:10
source

1 answer

0

You can make a replace of the symbols to then be able to make a split and then take everything except your arrangement

var arregloVariables = new string[] { "FE", "GE", "PRD", "RON" };
        var formula = "FE+GE*RONI";
        var stringSinoperadores = formula.Replace('*', ',').Replace('-', ',').Replace('+', ',').Replace('%', ',');
        var split = stringSinoperadores.Split(',');
        var res = split.Except(arregloVariables);

        Console.WriteLine(res.Any() ? "Formula incorrecta" : "Formula correcta");
    
answered by 29.11.2018 в 18:35