-2

I am trying Calculator code and design link https://i.stack.imgur.com/KrzRD.png

If my input = 2 * 4 + 6 - 10 / 2 entered in EDIT TEXT

How can i perform arithmetic operations on below part of my code???

 buttonEqual.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            s1 = (edt1.getText() + "");
            ArrayList<String> buffer = new ArrayList<String>();
            String temp = "";
            for (int j = 0; j < s1.length(); j++) {
                if (Character.isDigit(s1.charAt(j)) || s1.charAt(j) == '.') {
                    char c1 = s1.charAt(j);
                    temp = temp + String.valueOf(c1);                                                                 
                }
                if (s1.charAt(j) == '+' || s1.charAt(j) == '-' || s1.charAt(j) == '*' || s1.charAt(j) == '/') {
                    buffer.add(temp);                            
                    temp = "";
                    buffer.add(String.valueOf(str.charAt(j)));
                }
                buffer.add(temp);
shyam
  • 1,084
  • 12
  • 20
  • 1
    Simple version: 1. Split string into array of tokens. 2. Convert numeric tokens into integers or floats. 3. Using a switch statement, switch on the operator tokens and use the switch branches to discriminate the operations. 4. Use Java operators to perform addition, subtraction, etc. (But it gets complicated if you want operator precedence, brackets, etc) – Stephen C Jan 27 '18 at 05:11
  • More complicated version: find and read Q&As on implementing an expression language in Java. – Stephen C Jan 27 '18 at 05:12
  • Can u pls tell the code @StephenC – shyam Jan 27 '18 at 05:17
  • 1
    If you want code, then Google for it. This is not a free coding service. – Stephen C Jan 27 '18 at 05:28
  • 1
    k i will do it thanks for your response@StephenC – shyam Jan 27 '18 at 05:32

1 Answers1

4

This can be achieved with infix to postfix expression evaluation , you can read more this on http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm

Abdullah Khan
  • 649
  • 5
  • 11