Kurt Acosta - 6 months ago 22

Android Question

I am currently building an Android app that has a calculator that not only shows the result, but also shows how it reached that result?

Is there any library or any way that I could show a step by step computation for the result of the code below?

`int a = 5;`

int b = 6

int c = 7;

int d = 8;

int result = a + (b * c) / d;

edit: By the way, it's a calculator for physics so I have lots of formulas. I'm using exp4j to parse a string formula as an expression. Here's a sample

`//For formula velocity = (finalVelocity - initialVelocity) / time`

String formula1 = "(finalVelocity - initialVelocity) / time";

Double result1 = new ExpressionBuilder(formula)

.variables("finalVelocity", "initialVelocity", "time")

.build()

.setVariable("finalVelocity", 4);

.setVariable("initialVelocity", 2);

.setVariable("time", 2)

.evaluate();

//Sample Output

//velocity = (4 - 2) / 2

//velocity = 2 /2

//velocity = 1

//For formula finalVelocity = (velocity * time) + initialVelocity

String formula2 = "(velocity * time) + initialVelocity";

Double result12 = new ExpressionBuilder(formula)

.variables("velocity", "time" "initialVelocity")

.build()

.setVariable("velocity", 4);

.setVariable("time", 2)

.setVariable("initialVelocity", 0)

.evaluate();

//Sample Output

//finalVelocity = (4 * 2) + 0

//finalVelocity = 8 + 0

//finalVelocity = 8

With many formulas, I'm trying to eliminate printing each step per formula. I'm trying to find a way to have a function that would print the steps for any formula.

Answer

Considering you will be using *BODMAS* to solve that problem, you could consider simply printing each step:

```
int a = 5;
int b = 6;
int c = 7;
int d = 8;
int ans = (b * c);
System.out.println(ans)
ans /= d;
System.out.println(ans)
ans += a;
System.out.println(ans)
```

To do this, you can build a function which searches for brackets first and solves the equations in the according to *BODMAS(Brackets first, then Division, then Multiplication, then Addition, and finally Subtraction)*.

Considering you take the equation as a string, you first search for (), then solve `/`

and print answers followed by `*`

, `+`

and `-`

.