From 7af39fd145b2ffe828a605b46b0771a78bfe8933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BE=8A=E9=A9=BC=E6=83=B3=E7=9D=A1=E8=A7=89?= <204532647@qq.com> Date: Sat, 13 Apr 2024 19:19:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=8F=E6=99=93=E9=9D=994/13=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CalculatorGUI.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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); }