0

I worked on a simple calculator app that does not pile up an expression like google calculator. Instead it stores the result in an operand when an operation button is pressed and so it does not follow BODMAS precedence rules. Like if I perform 2 + 2 / 2 I get 4 as the result whereas the result should be 3 as the division is performed first Is there a way to take input as a whole expression and then evaluate it collectively? Are there any predefined classes that I can use that are included in java?

The code for the onClickListeners of operators and calculation performing function is:

View.OnClickListener opListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Button b = (Button) view;
                String op = b.getText().toString();
                String value = newNumber.getText().toString();
                try {
                    Double doubleValue = Double.valueOf(value);
                    performOperation(doubleValue, op);
                } catch (NumberFormatException e) {
                    newNumber.setText("");
                }
                pendingOperation = op;
                displayOperation.setText(pendingOperation);
            }
        };


private void performOperation(Double value, String operation) {
        if (operand1 == null) {
            operand1 = value;
        } else {
            if (pendingOperation.equals("=")) {
                pendingOperation = operation;
            }

            switch (pendingOperation) {
                case "=":
                    operand1 = value;
                    break;
                case "/":
                    if (value == 0) {
                        operand1 = 0.0;
                        Toast.makeText(this,"Cannot Divide By Zero, Resetting value to zero",Toast.LENGTH_LONG).show();
                    } else {
                        operand1 /= value;
                    }
                    break;
                case "*":
                    operand1 *= value;
                    break;
                case "+":
                    operand1 += value;
                    break;
                case "-":
                    operand1 -= value;
                    break;
            }
        }
        result.setText(operand1.toString());
        newNumber.setText("");
    }
Jugal Mistry
  • 160
  • 1
  • 9
  • yeh i suggest using `ScriptEngine` and `ScriptEngineManager` which i think the android libraries don't have this package you should add this package to your project to use it runs javascript functions and returning the result you can do it easily by js, just write the expression in the string and it runs during sometime and it returns the result – Azhy Jul 22 '18 at 16:15
  • 1
    Possible duplicate of [Evaluating a math expression given in string form](https://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form) – ricky3350 Jul 22 '18 at 16:35
  • Also, if you're willing to take the time to write your own algorithm, I'd suggest taking a look at the [shunting yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm), which is designed to do this. – ricky3350 Jul 22 '18 at 16:37
  • I'm probably thinking of making an algorithm myself. – Jugal Mistry Jul 22 '18 at 16:39
  • please provide some of your code , where you do the calculations , this normally shouldnt happen – Reza Jul 22 '18 at 16:41

0 Answers0