Question: I've got a home automation system working in Java, and I want to add simple math capabilities such as addition, subtraction, multiplication, division, roots, and powers.

At the system current state, it can convert a phrase into tags, as shown in the following examples:

example:

```
Phrase: "what is one hundred twenty two to the power of seven"
Tagged: {QUESTION/math} {NUMBER/122} {MATH/pwr} {NUMBER/7}

```

example:

```
Phrase: "twenty seven plus pi 3 squared"
Tagged: {NUMBER/27} {MATH/add} {NUMBER/3.14159} {MATH/multiply} {MATH/pwr} {NUMBER/2}

```

This example could be just as easily converted to something like this:

```
27 + 3.14159 * 3^2

```

Each tag is an object that can be queried for it value.

**Edit: Specific question:**

So now I need a way to read that group of tags as an equation, and return a numerical result. As a last resort I could use google or wolfram alpha, but that will be slower, and I'm trying to keep the automation system completely self contained.

If you would like to see the entire source, [here it is in github.](https://github.com/Sprakle/HomeAutomation/tree/master/HomeAutomation)
Note that I have not committed the last few few changes, so some of the math related things I gave examples will not work.

Answer: 
After some more googleing (I didn't know the name for what I was doing at first) I found someone who has done something similar already:

<http://www.objecthunter.net/exp4j/>

Edit: Finished:
<https://github.com/Sprakle/HomeAutomation/blob/master/src/net/sprakle/homeAutomation/interpretation/module/modules/math/Math.java>
What you need is a parsing method that will build an equation and return an answer from your text. I'll take a single line and walk through making such a method which you will then need to work out for yourself. Note that this is just a general idea and that some languages other than Java may be more suitable for this kind of operation.

{QUESTION/math} {NUMBER/122} {MATH/pwr} {NUMBER/7}

```
//let's assume that the preprocessor reduces the input to an array that is something like this:
// [122, pwr, 7] (this is a very common construction in a language like LISP).

public static int math(string[] question){

   while(i < question.length){
      if(question[i] == "pwr"){
           return pow(Integer.parseInt(question[i-1]), Integer.parseInt(question[i+1]);
       }
       i++;
   }
  return 0;
}

```

Basically what you'll need is a nice way of going from infix to prefix notation with a little bit of string to whatever conversions.

There are likely nicer structures for doing this than what I've produced above, but something like this should get you going.