xml代码
<?xml version="1.0" encoding="utf-8"?><EditText
android:id="@+id/inputField1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"/>
<EditText
android:id="@+id/inputField2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number (for operations that require two inputs)"/>
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result will be shown here"/>
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+"/>
<Button
android:id="@+id/subtractButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"/>
<Button
android:id="@+id/multiplyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="×"/>
<Button
android:id="@+id/divideButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="÷"/>
<Button
android:id="@+id/sqrtButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="平方根"/>
<Button
android:id="@+id/powerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="幂运算"/>
<Button
android:id="@+id/factorialButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="阶乘"/>
java代码
package com.example.a62jzjsq;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.math.BigDecimal;
import java.math.BigInteger;
public class MainActivity extends AppCompatActivity {
private EditText inputField1;
private EditText inputField2;
private TextView resultTextView;
private Button addButton;
private Button subtractButton;
private Button multiplyButton;
private Button divideButton;
private Button sqrtButton;
private Button powerButton;
private Button factorialButton;
private static final String BASE62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputField1 = findViewById(R.id.inputField1);
inputField2 = findViewById(R.id.inputField2);
resultTextView = findViewById(R.id.resultTextView);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);
sqrtButton = findViewById(R.id.sqrtButton);
powerButton = findViewById(R.id.powerButton);
factorialButton = findViewById(R.id.factorialButton);
addButton.setOnClickListener(v -> performOperation("add"));
subtractButton.setOnClickListener(v -> performOperation("subtract"));
multiplyButton.setOnClickListener(v -> performOperation("multiply"));
divideButton.setOnClickListener(v -> performOperation("divide"));
sqrtButton.setOnClickListener(v -> performOperation("sqrt"));
powerButton.setOnClickListener(v -> performOperation("power"));
factorialButton.setOnClickListener(v -> performOperation("factorial"));
}
private void performOperation(String operation) {
String input1 = inputField1.getText().toString();
String input2 = inputField2.getText().toString();
try {
BigDecimal result;
switch (operation) {
case "add":
result = convertToDecimal(input1).add(convertToDecimal(input2));
break;
case "subtract":
result = convertToDecimal(input1).subtract(convertToDecimal(input2));
break;
case "multiply":
result = convertToDecimal(input1).multiply(convertToDecimal(input2));
break;
case "divide":
result = convertToDecimal(input1).divide(convertToDecimal(input2), 10, BigDecimal.ROUND_HALF_UP);
break;
case "sqrt":
result = BigDecimal.valueOf(Math.sqrt(convertToDecimal(input1).doubleValue()));
break;
case "power":
BigDecimal base = convertToDecimal(input1);
BigDecimal exponent = convertToDecimal(input2);
result = BigDecimal.valueOf(Math.pow(base.doubleValue(), exponent.doubleValue()));
break;
case "factorial":
BigInteger number = convertToDecimal(input1).toBigInteger();
result = factorial(number);
break;
default:
resultTextView.setText("Invalid operation");
return;
}
resultTextView.setText(convertToBase62(result));
} catch (NumberFormatException e) {
resultTextView.setText("Invalid input");
}
}
private BigDecimal convertToDecimal(String base62Number) {
BigInteger decimalValue = BigInteger.ZERO;
int power = 0;
for (int i = base62Number.length() - 1; i >= 0; i--) {
int charValue = BASE62_CHARS.indexOf(base62Number.charAt(i));
decimalValue = decimalValue.add(BigInteger.valueOf(charValue).multiply(BigInteger.valueOf(62).pow(power)));
power++;
}
return new BigDecimal(decimalValue);
}
private String convertToBase62(BigDecimal decimalNumber) {
if (decimalNumber.equals(BigDecimal.ZERO)) {
return "0";
}
StringBuilder base62Number = new StringBuilder();
BigInteger number = decimalNumber.toBigInteger();
while (number.compareTo(BigInteger.ZERO) > 0) {
BigInteger remainder = number.mod(BigInteger.valueOf(62));
base62Number.insert(0, BASE62_CHARS.charAt(remainder.intValue()));
number = number.divide(BigInteger.valueOf(62));
}
return base62Number.toString();
}
private BigDecimal factorial(BigInteger number) {
BigDecimal result = BigDecimal.ONE;
for (BigInteger i = BigInteger.ONE; i.compareTo(number) <= 0; i = i.add(BigInteger.ONE)) {
result = result.multiply(new BigDecimal(i));
}
return result;
}
}