在android studio中制作一个没有优先级且输入1/3*3等于1的计算器
时间: 2024-02-03 15:15:00 浏览: 134
Android计算器(Android studio)
5星 · 资源好评率100%
首先,我们需要在 Android Studio 中创建一个新的项目。然后,在 activity_main.xml 文件中添加一个 TextView 用于显示计算器的结果和一些 Button 用于输入数字和运算符。
接下来,在 MainActivity.java 文件中实现计算器的逻辑。我们可以使用一个 Stack 来存储数字和运算符,并且使用一个变量来保存当前的数字。当用户输入数字时,我们将其添加到当前数字的末尾。当用户输入运算符时,我们将当前数字入栈,并将运算符入栈。当用户按下“=”按钮时,我们从栈中取出数字和运算符,按照运算符的优先级进行计算,并将结果入栈。最后,我们将栈中所有数字相加得到计算器的最终结果。
以下是 MainActivity.java 的代码实现:
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView display;
private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private Button buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEquals;
private int currentNumber;
private Stack<Integer> numbers;
private Stack<Character> operators;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.display);
button0 = findViewById(R.id.button0);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
button6 = findViewById(R.id.button6);
button7 = findViewById(R.id.button7);
button8 = findViewById(R.id.button8);
button9 = findViewById(R.id.button9);
buttonAdd = findViewById(R.id.buttonAdd);
buttonSubtract = findViewById(R.id.buttonSubtract);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonDivide = findViewById(R.id.buttonDivide);
buttonEquals = findViewById(R.id.buttonEquals);
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonAdd.setOnClickListener(this);
buttonSubtract.setOnClickListener(this);
buttonMultiply.setOnClickListener(this);
buttonDivide.setOnClickListener(this);
buttonEquals.setOnClickListener(this);
numbers = new Stack<>();
operators = new Stack<>();
currentNumber = 0;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button0:
handleDigit(0);
break;
case R.id.button1:
handleDigit(1);
break;
case R.id.button2:
handleDigit(2);
break;
case R.id.button3:
handleDigit(3);
break;
case R.id.button4:
handleDigit(4);
break;
case R.id.button5:
handleDigit(5);
break;
case R.id.button6:
handleDigit(6);
break;
case R.id.button7:
handleDigit(7);
break;
case R.id.button8:
handleDigit(8);
break;
case R.id.button9:
handleDigit(9);
break;
case R.id.buttonAdd:
handleOperator('+');
break;
case R.id.buttonSubtract:
handleOperator('-');
break;
case R.id.buttonMultiply:
handleOperator('*');
break;
case R.id.buttonDivide:
handleOperator('/');
break;
case R.id.buttonEquals:
handleEquals();
break;
}
}
private void handleDigit(int digit) {
currentNumber = currentNumber * 10 + digit;
display.setText(String.valueOf(currentNumber));
}
private void handleOperator(char operator) {
numbers.push(currentNumber);
operators.push(operator);
currentNumber = 0;
}
private void handleEquals() {
numbers.push(currentNumber);
int result = 0;
while (!numbers.isEmpty()) {
int number = numbers.pop();
if (operators.isEmpty()) {
result += number;
} else {
char operator = operators.pop();
switch (operator) {
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
break;
}
}
}
display.setText(String.valueOf(result));
currentNumber = 0;
}
}
```
注意,这个计算器没有考虑优先级,因此输入 1/3*3 的结果将是 1,而不是 3。如果需要考虑优先级,需要在处理运算符时进行相应的判断。
阅读全文