diff --git a/doc/文档模板-开源软件维护报告文档 (1).docx b/doc/文档模板-开源软件维护报告文档 (1).docx new file mode 100644 index 0000000..2c93bfe Binary files /dev/null and b/doc/文档模板-开源软件维护报告文档 (1).docx differ diff --git a/src/新M(3).txt b/src/新M(3).txt new file mode 100644 index 0000000..f891f01 --- /dev/null +++ b/src/新M(3).txt @@ -0,0 +1,314 @@ +package net.micode.myapplication; +import androidx.appcompat.app.AppCompatActivity; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; +public class MainActivity extends AppCompatActivity { + private TextView display; + private TextView historyDisplay; + private String currentInput = ""; + private String previousInput = ""; + private String operation = ""; + private boolean isNewCalculation = false; + private String lastResult = ""; + private boolean hasOperation = false; + // 新增错误状态标记 + private boolean hasError = false; + private DecimalFormat decimalFormat = new DecimalFormat("#.##########"); + private List historyList = new ArrayList<>(); + private SharedPreferences preferences; + private static final String PREF_THEME = "theme_preference"; + private static final int THEME_LIGHT = 0; + private static final int THEME_DARK = 1; + private int currentTheme; + @Override + protected void onCreate(Bundle savedInstanceState) { + preferences = getSharedPreferences("CalculatorPrefs", MODE_PRIVATE); + currentTheme = preferences.getInt(PREF_THEME, THEME_LIGHT); + setTheme(currentTheme == THEME_DARK ? R.style.DarkTheme : R.style.LightTheme); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + display = findViewById(R.id.display); + historyDisplay = findViewById(R.id.history_display); + if (savedInstanceState != null) { + historyList = savedInstanceState.getStringArrayList("history"); + lastResult = savedInstanceState.getString("lastResult", ""); + hasOperation = savedInstanceState.getBoolean("hasOperation", false); + hasError = savedInstanceState.getBoolean("hasError", false); + updateDisplay(); // 错误状态需要恢复显示 + } + setupNumberButton(R.id.btn_0, "0"); + setupNumberButton(R.id.btn_1, "1"); + setupNumberButton(R.id.btn_2, "2"); + setupNumberButton(R.id.btn_3, "3"); + setupNumberButton(R.id.btn_4, "4"); + setupNumberButton(R.id.btn_5, "5"); + setupNumberButton(R.id.btn_6, "6"); + setupNumberButton(R.id.btn_7, "7"); + setupNumberButton(R.id.btn_8, "8"); + setupNumberButton(R.id.btn_9, "9"); + setupNumberButton(R.id.btn_dot, "."); + setupOperationButton(R.id.btn_add, "+"); + setupOperationButton(R.id.btn_subtract, "-"); + setupOperationButton(R.id.btn_multiply, "*"); + setupOperationButton(R.id.btn_divide, "/"); + setupScientificButton(R.id.btn_sin, "sin"); + setupScientificButton(R.id.btn_cos, "cos"); + setupScientificButton(R.id.btn_tan, "tan"); + setupScientificButton(R.id.btn_square, "x²"); + setupScientificButton(R.id.btn_sqrt, "√"); + findViewById(R.id.btn_equals).setOnClickListener(v -> calculateResult()); + findViewById(R.id.btn_clear).setOnClickListener(v -> clearAll()); + findViewById(R.id.btn_percent).setOnClickListener(v -> calculatePercentage()); + findViewById(R.id.btn_plus_minus).setOnClickListener(v -> changeSign()); + findViewById(R.id.btn_history).setOnClickListener(v -> showHistory()); + findViewById(R.id.btn_theme).setOnClickListener(v -> switchTheme()); + } + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putStringArrayList("history", new ArrayList<>(historyList)); + outState.putString("lastResult", lastResult); + outState.putBoolean("hasOperation", hasOperation); + outState.putBoolean("hasError", hasError); + } + private void setupNumberButton(int buttonId, String number) { + Button button = findViewById(buttonId); + button.setOnClickListener(v -> { + // 错误状态下输入数字,清除错误并开始新计算 + if (hasError) { + clearAll(); + hasError = false; + } + if (hasOperation && isNewCalculation && !lastResult.isEmpty()) { + currentInput = lastResult; + isNewCalculation = false; + hasOperation = false; + } else if (isNewCalculation && !lastResult.isEmpty()) { + currentInput = lastResult; + isNewCalculation = false; + } + if (number.equals(".") && currentInput.contains(".")) return; + if (currentInput.equals("0") && !number.equals(".")) { + currentInput = number; + } else { + currentInput += number; + } + updateDisplay(); + }); + } + private void setupOperationButton(int buttonId, String op) { + Button button = findViewById(buttonId); + button.setOnClickListener(v -> { + // 错误状态下输入操作符,清除错误并开始新计算 + if (hasError) { + clearAll(); + hasError = false; + } + if (!currentInput.isEmpty() || !lastResult.isEmpty()) { + String input = currentInput; + if (currentInput.isEmpty() && !lastResult.isEmpty()) { + input = lastResult; + } + if (!previousInput.isEmpty() && !operation.isEmpty() && !isNewCalculation) { + calculateResult(); + // 若上次计算有错误,不更新操作符 + if (hasError) return; + previousInput = lastResult; + currentInput = ""; + operation = op; + hasOperation = true; + updateDisplay(); + return; + } + previousInput = input; + currentInput = ""; + operation = op; + hasOperation = true; + isNewCalculation = false; + updateDisplay(); + } + }); + } + private void setupScientificButton(int buttonId, String func) { + Button button = findViewById(buttonId); + button.setOnClickListener(v -> { + // 错误状态下输入科学计算按钮,清除错误并开始新计算 + if (hasError) { + clearAll(); + hasError = false; + } + String input = currentInput; + if (isNewCalculation && !lastResult.isEmpty()) { + input = lastResult; + isNewCalculation = false; + } + if (!input.isEmpty()) { + double value = Double.parseDouble(input); + double result = 0; + boolean errorOccurred = false; + switch (func) { + case "sin": + result = Math.sin(Math.toRadians(value)); + break; + case "cos": + result = Math.cos(Math.toRadians(value)); + break; + case "tan": + result = Math.tan(Math.toRadians(value)); + break; + case "x²": + result = value * value; + break; + case "√": + if (value < 0) { + display.setText("Error"); + hasError = true; + errorOccurred = true; + } else { + result = Math.sqrt(value); + } + break; + } + if (!errorOccurred) { + currentInput = decimalFormat.format(result); + lastResult = currentInput; + hasOperation = false; + updateDisplay(); + } + } + }); + } + private void calculateResult() { + if (hasError) return; // 错误状态下不执行计算 + if (previousInput.isEmpty() || (currentInput.isEmpty() && lastResult.isEmpty()) || operation.isEmpty()) return; + double prev = Double.parseDouble(previousInput); + double current = currentInput.isEmpty() ? Double.parseDouble(lastResult) : Double.parseDouble(currentInput); + double result = 0; + boolean errorOccurred = false; + + // 除零错误处理核心逻辑 + if (operation.equals("/") && current == 0) { + display.setText("不能除以0"); + hasError = true; + errorOccurred = true; + } else { + switch (operation) { + case "+": + result = prev + current; + break; + case "-": + result = prev - current; + break; + case "*": + result = prev * current; + break; + case "/": + result = prev / current; + break; + } + } + + if (!errorOccurred) { + String historyItem = previousInput + " " + operation + " " + + (currentInput.isEmpty() ? lastResult : currentInput) + " = " + result; + historyList.add(historyItem); + updateHistoryDisplay(); + currentInput = decimalFormat.format(result); + lastResult = currentInput; + previousInput = ""; + operation = ""; + isNewCalculation = true; + hasOperation = false; + updateDisplay(); + } + } + private void clearAll() { + currentInput = ""; + previousInput = ""; + operation = ""; + lastResult = ""; + isNewCalculation = false; + hasOperation = false; + hasError = false; // 清除错误状态 + updateDisplay(); + } + private void calculatePercentage() { + if (hasError) { + clearAll(); + hasError = false; + } + String input = currentInput; + if (isNewCalculation && !lastResult.isEmpty()) { + input = lastResult; + isNewCalculation = false; + } + if (!input.isEmpty()) { + double value = Double.parseDouble(input) / 100; + currentInput = decimalFormat.format(value); + lastResult = currentInput; + hasOperation = false; + updateDisplay(); + } else if (!previousInput.isEmpty()) { + double value = Double.parseDouble(previousInput) / 100; + previousInput = decimalFormat.format(value); + updateDisplay(); + } + } + private void changeSign() { + if (hasError) { + clearAll(); + hasError = false; + } + String input = currentInput; + if (isNewCalculation && !lastResult.isEmpty()) { + input = lastResult; + isNewCalculation = false; + } + if (!input.isEmpty()) { + double value = Double.parseDouble(input); + value = -value; + currentInput = decimalFormat.format(value); + lastResult = currentInput; + hasOperation = false; + updateDisplay(); + } + } + private void updateDisplay() { + if (hasError) { + display.setText("Error"); + return; + } + StringBuilder displayText = new StringBuilder(); + if (!previousInput.isEmpty()) { + displayText.append(previousInput).append(" "); + if (!operation.isEmpty()) { + displayText.append(operation).append(" "); + } + } + if (!currentInput.isEmpty()) { + displayText.append(currentInput); + } + display.setText(displayText.toString()); + } + private void updateHistoryDisplay() { + StringBuilder historyText = new StringBuilder(); + for (int i = historyList.size() - 1; i >= 0; i--) { + historyText.append(historyList.get(i)).append("\n"); + } + historyDisplay.setText(historyText.toString()); + } + private void showHistory() { + updateHistoryDisplay(); + } + private void switchTheme() { + currentTheme = (currentTheme == THEME_LIGHT) ? THEME_DARK : THEME_LIGHT; + preferences.edit().putInt(PREF_THEME, currentTheme).apply(); + recreate(); + } +} \ No newline at end of file diff --git a/src/新a(3).txt b/src/新a(3).txt new file mode 100644 index 0000000..5363a13 --- /dev/null +++ b/src/新a(3).txt @@ -0,0 +1,204 @@ + + + + + + + + + + +