diff --git a/src/CalculatorGUI.java b/src/CalculatorGUI.java index 2f63db5..382f6b0 100644 --- a/src/CalculatorGUI.java +++ b/src/CalculatorGUI.java @@ -147,10 +147,33 @@ public class CalculatorGUI extends JFrame { String command = e.getActionCommand(); if (!command.equals("=")) { if (command.equals("x²") || command.equals("x³") || command.equals("√x") || command.equals("x⁴")) { - // 计算函数值 + // 获取表达式和操作符 String expression = displayField.getText(); + String operator = ""; + switch (command) { + case "x²": + operator = "²"; + break; + case "x³": + operator = "³"; + break; + case "√x": + operator = "√"; + break; + case "x⁴": + operator = "⁴"; + break; + } + // 计算函数值 double result = calculateFunction(expression, command); + // 构建历史记录字符串并添加到历史记录区 + String historyEntry = expression + operator + " = " + result; + appendToHistory(historyEntry); + // 显示结果在实时显示区 displayField.setText(Double.toString(result)); + } else if (command.equals("C")) { + // 清除实时显示区域的算术式 + displayField.setText(""); } else { displayField.setText(displayField.getText() + command); }