Arithmetic operations on string value

时间:2018-01-27 04:58:35

标签: java android android-studio calculator arithmetic-expressions

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);

1 个答案:

答案 0 :(得分:4)

这可以通过中缀到后缀表达式评估来实现,你可以在http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm上阅读更多内容