1

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 ?

Aenorr
  • 15
  • 1
  • 4
  • 3
    If you write a parser that knows what `"4*a"` means yes. Otherwise no. A String is just a bunch of numbers as far as it concerns your app. If you have a limited amount of formulas, name them and map them to some kind implementation that can execute the calculation - like http://sourcemaking.com/design_patterns/strategy/java/1 – zapl Nov 22 '12 at 17:32
  • The `XmlPullParser` understands xml, i.e. that `` is a node named "tag" and that it has an attribute named "attribute" with the value "stuff". But it does not know what those values mean. If you want to evaluate arbitrary mathematical expressions you need something like http://tapas4web.blogspot.de/2011/05/mathematical-expression-parser-in-java.html – zapl Nov 22 '12 at 17:54
  • So basicaly what I have to do is an application with math formulas, that uses XML as a database for all forumulas, and those are executed when user need to solve for example square perimeter. So are there other ways how to solve this problem ? – Aenorr Nov 22 '12 at 17:56
  • If you really need to store the formulas as text in xml, no – zapl Nov 22 '12 at 17:59
  • Aw ok, will try to find other solution. Anyway, thanks for your time. – Aenorr Nov 22 '12 at 18:01

1 Answers1

-1

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.

Community
  • 1
  • 1
zapl
  • 63,179
  • 10
  • 123
  • 154