Android: Is there a way how to assign math formula from res/values/formulas.xml to variable?

StackOverflow https://stackoverflow.com/questions/13517569

  •  01-12-2021
  •  | 
  •  

Pergunta

I need to assign a formula like square perimeter (4*a) which i have stored in resource xml as string, to a value in my activity. So, it should look like:

Activity:

int a = (formula from xml)

And XML:

<resources>

<string name="square_perimeter">4*a</string>

</resources>

Is there a way how to do that ?

Foi útil?

Solução

What is [34 20 2A 20 31 36]?

It is what a computer will see when it encounters the string "4 * 16". And just like you, when given only that, it does not know why those cryptic numbers should be equivalent to 64.

So to really evaluate that, a computer / the algorithm would start by separating the input into chunks. Let's assume your expression must be separated by whitespace (4*4 is illegal, 4 * 4 is okay). Based on that knowledge we can split the expression into [34], [2A], [31 36].

  • [34] stands for the character 4 which is a number => that part means 4
  • [2A] stands for the character * which is a mathematical operator => we need to multiply the part before this with the part after this.
  • [31 36] stands for 1 and 6, both are numbers. => calculate 1 * 10 + 6 => it's a 16.

We know at that point that a multiplication has to be done and that we have 4 and 16, so 4*16 can be calculated and we have the result.

If you just need very simple formulas that follow strict rules, like only "[numbers] [*/+-] [numbers]" then writing a small parser for that is quite simple.

But math gets complex pretty quickly and already "10 + 5 * 2" requires that you don't add 10 + 5 if you evaluate from left to right because it would be the wrong result.

The amount of code one needs to write to evaluate complex expression with variables like "5a^2 * sin(a) +1" is pretty big therefore. It needs to cover all the mathematical rules and all the special cases.


Back to your problem: If you really want to use text formulas, look for a library that can do the ugly stuff for you. Evaluating a math expression given in string form for example mentions some.

If you don't need to have the formula as text you could maybe do it like this:

You put names for methods in the xml database and implement the solution for that method in your app. The user selects a method by it's name and you call the right method based on that name. The disadvantage is that all your formulas must be known so you can write a piece of code that can solve it.

Very basic example:

public int calculate(String method, int value1, int value2) {
    if ("square".equals(method)) {
        return value1 * value1;
    } else if ("add".equals(method)) {
        return value1 + value2;
    } else if ("substract".equals(method)) {
        return value1 - value2;
    } else {
        return 0;
    }
}

The values used in the formula need to come somewhere too, I don't know how you plan to do that.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top