android studio计算器能同时输出表达式和结果的代码
时间: 2023-07-28 15:18:23 浏览: 83
下面是一个简单的 Android Studio 计算器应用程序的代码,它可以同时输出表达式和结果:
布局文件:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="30sp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5"
android:padding="16dp">
<Button
android:id="@+id/button_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="C" />
<Button
android:id="@+id/button_divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="/" />
<Button
android:id="@+id/button_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="*" />
<Button
android:id="@+id/button_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="-" />
<Button
android:id="@+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="7" />
<Button
android:id="@+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="8" />
<Button
android:id="@+id/button_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="9" />
<Button
android:id="@+id/button_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="+" />
<Button
android:id="@+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="4" />
<Button
android:id="@+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="5" />
<Button
android:id="@+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="6" />
<Button
android:id="@+id/button_equals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="2"
android:text="=" />
<Button
android:id="@+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="1" />
<Button
android:id="@+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="2" />
<Button
android:id="@+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="3" />
<Button
android:id="@+id/button_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="2"
android:layout_rowWeight="1"
android:text="0" />
<Button
android:id="@+id/button_decimal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="." />
</GridLayout>
</LinearLayout>
```
Java 代码:
```
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
private TextView output;
private Button button_clear, button_divide, button_multiply, button_subtract, button_add,
button_7, button_8, button_9, button_4, button_5, button_6, button_1, button_2,
button_3, button_0, button_decimal, button_equals;
private StringBuilder expression = new StringBuilder(); // 存储表达式
private Stack<Double> numbers = new Stack<>(); // 存储数字
private Stack<Character> operators = new Stack<>(); // 存储运算符
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = findViewById(R.id.output);
button_clear = findViewById(R.id.button_clear);
button_divide = findViewById(R.id.button_divide);
button_multiply = findViewById(R.id.button_multiply);
button_subtract = findViewById(R.id.button_subtract);
button_add = findViewById(R.id.button_add);
button_7 = findViewById(R.id.button_7);
button_8 = findViewById(R.id.button_8);
button_9 = findViewById(R.id.button_9);
button_4 = findViewById(R.id.button_4);
button_5 = findViewById(R.id.button_5);
button_6 = findViewById(R.id.button_6);
button_1 = findViewById(R.id.button_1);
button_2 = findViewById(R.id.button_2);
button_3 = findViewById(R.id.button_3);
button_0 = findViewById(R.id.button_0);
button_decimal = findViewById(R.id.button_decimal);
button_equals = findViewById(R.id.button_equals);
// 添加清除按钮的点击事件
button_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression = new StringBuilder();
numbers.clear();
operators.clear();
output.setText("");
}
});
// 添加运算符按钮的点击事件
button_divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expression.length() == 0) {
expression.append("0");
}
char lastChar = expression.charAt(expression.length() - 1);
if (lastChar == '+' || lastChar == '-' || lastChar == '*' || lastChar == '/') {
expression.deleteCharAt(expression.length() - 1);
}
expression.append("/");
output.setText(expression.toString());
}
});
button_multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expression.length() == 0) {
expression.append("0");
}
char lastChar = expression.charAt(expression.length() - 1);
if (lastChar == '+' || lastChar == '-' || lastChar == '*' || lastChar == '/') {
expression.deleteCharAt(expression.length() - 1);
}
expression.append("*");
output.setText(expression.toString());
}
});
button_subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expression.length() == 0) {
expression.append("0");
}
char lastChar = expression.charAt(expression.length() - 1);
if (lastChar == '+' || lastChar == '-' || lastChar == '*' || lastChar == '/') {
expression.deleteCharAt(expression.length() - 1);
}
expression.append("-");
output.setText(expression.toString());
}
});
button_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expression.length() == 0) {
expression.append("0");
}
char lastChar = expression.charAt(expression.length() - 1);
if (lastChar == '+' || lastChar == '-' || lastChar == '*' || lastChar == '/') {
expression.deleteCharAt(expression.length() - 1);
}
expression.append("+");
output.setText(expression.toString());
}
});
// 添加数字按钮的点击事件
button_0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("0");
output.setText(expression.toString());
}
});
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("1");
output.setText(expression.toString());
}
});
button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("2");
output.setText(expression.toString());
}
});
button_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("3");
output.setText(expression.toString());
}
});
button_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("4");
output.setText(expression.toString());
}
});
button_5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("5");
output.setText(expression.toString());
}
});
button_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("6");
output.setText(expression.toString());
}
});
button_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("7");
output.setText(expression.toString());
}
});
button_8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("8");
output.setText(expression.toString());
}
});
button_9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expression.append("9");
output.setText(expression.toString());
}
});
button_decimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expression.length() == 0) {
expression.append("0");
}
char lastChar = expression.charAt(expression.length() - 1);
if (lastChar == '+' || lastChar == '-' || lastChar == '*' || lastChar == '/') {
expression.append("0");
}
expression.append(".");
output.setText(expression.toString());
}
});
// 添加等于按钮的点击事件
button_equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String exp = expression.toString();
if (TextUtils.isEmpty(exp)) {
return;
}
// 处理负数
exp = exp.replaceAll("\\(-", "(0-");
// 将表达式转换为后缀表达式
String[] tokens = exp.split("(?<=[-+*/()])|(?=[-+*/()])");
for (String token : tokens) {
if (TextUtils.isEmpty(token)) {
continue;
}
if (Character.isDigit(token.charAt(0))) {
double num = Double.parseDouble(token);
numbers.push(num);
} else if (token.charAt(0) == '(') {
operators.push('(');
} else if (token.charAt(0) == ')') {
while (operators.peek() != '(') {
double num2 = numbers.pop();
double num1 = numbers.pop();
char opr = operators.pop();
double res = evaluate(num1, num2, opr);
numbers.push(res);
}
operators.pop();
} else {
while (!operators.isEmpty() && precedence(operators.peek(), token.charAt(0))) {
double num2 = numbers.pop();
double num1 = numbers.pop();
char opr = operators.pop();
double res = evaluate(num1, num2, opr);
numbers.push(res);
}
operators.push(token.charAt(0));
}
}
while (!operators.isEmpty()) {
double num2 = numbers.pop();
double num1 = numbers.pop();
char opr = operators.pop();
double res = evaluate(num1, num2, opr);
numbers.push(res);
}
double result = numbers.pop();
expression = new StringBuilder();
numbers.clear();
operators.clear();
expression.append(exp);
expression.append("=");
expression.append(result);
output.setText(expression.toString());
}
});
}
// 计算两个数字之间的运算结果
private double evaluate(double num1, double num2, char opr) {
switch (opr) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2 == 0) {
throw new ArithmeticException("除数不能为零");
}
return num1 / num2;
default:
return 0;
}
}
// 比较两个运算符的优先级
private boolean precedence(char opr1, char opr2) {
if (opr1 == '(' || opr1 == ')') {
return false;
}
if ((opr1 == '+' || opr1 == '-') && (opr2 == '*' || opr2 == '/')) {
return false;
}
return true;
}
}
```
这个计算器应用程序使用逆波兰表达式算法来计算表达式的结果,并将表达式和结果一起输出到 TextView 控件中。
阅读全文