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);
答案 0 :(得分:4)
这可以通过中缀到后缀表达式评估来实现,你可以在http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm上阅读更多内容