diff --git a/UML.iml b/UML.iml deleted file mode 100644 index 37cc804..0000000 --- a/UML.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/src/Calculator6218.java b/src/Calculator6218.java deleted file mode 100644 index 0191cb9..0000000 --- a/src/Calculator6218.java +++ /dev/null @@ -1,200 +0,0 @@ - -import javafx.application.Application; -import javafx.geometry.Insets; -import javafx.geometry.Pos; -import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.TextField; -import javafx.scene.layout.*; -import javafx.stage.Stage; - -import java.util.Stack; - -public class Calculator6218 extends Application { - private TextField display; - private StringBuilder currentInput = new StringBuilder(); - - public static void main(String[] args) { - launch(args); - } - - @Override - public void start(Stage primaryStage) { - VBox root = getRoot(); - Scene scene = new Scene(root, 300, 400); - primaryStage.setTitle("计算器"); - primaryStage.setScene(scene); - primaryStage.show(); - } - - private VBox getRoot() { - // 显示区域 - display = new TextField("0"); - display.setEditable(false); - display.setAlignment(Pos.CENTER_RIGHT); - display.setStyle("-fx-font-size: 24; -fx-background-color: #FFFFFF; -fx-border-color: #000000;"); - - // 按钮布局 - GridPane buttonGrid = new GridPane(); - buttonGrid.setPadding(new Insets(10)); - buttonGrid.setHgap(10); - buttonGrid.setVgap(10); - - // 创建按钮 - String[] buttonLabels = { - "%", "CE", "C", "×", - "1/x", "x²", "√", "÷", - "7", "8", "9", "-", - "4", "5", "6", "+", - "1", "2", "3", "=", - "+/-", "0", "." - }; - - int index = 0; - for (int row = 0; row < 5; row++) { - for (int col = 0; col < 4; col++) { - Button button = new Button(buttonLabels[index]); - button.setMinSize(50, 50); - button.setOnAction(event -> handleButtonClick(button.getText())); - buttonGrid.add(button, col, row); - index++; - } - } - - // 主布局 - VBox root = new VBox(10); - root.setPadding(new Insets(20)); - root.getChildren().addAll(display, buttonGrid); - return root; - } - - private void handleButtonClick(String buttonText) { - // 处理按钮点击 - switch (buttonText) { - case "C": - display.setText("0"); - currentInput.setLength(0); - break; - case "CE": - currentInput.setLength(0); - display.setText("0"); - break; - case "=": - try { - double result = evaluateExpression(currentInput.toString()); - display.setText(String.valueOf(result)); - currentInput.setLength(0); - currentInput.append(result); - } catch (Exception e) { - display.setText("错误"); - } - break; - case "+": - case "-": - case "×": - case "÷": - case "√": - case "x²": - case "1/x": - currentInput.append(convertOperator(buttonText)); - display.setText(currentInput.toString()); - break; - default: - currentInput.append(buttonText); - display.setText(currentInput.toString()); - break; - } - } - - private String convertOperator(String operator) { - switch (operator) { - case "×": - return "*"; - case "÷": - return "/"; - case "√": - return "Math.sqrt("; - case "x²": - return "^(2)"; - case "1/x": - return "(1/("; - default: - return operator; - } - } - - private double evaluateExpression(String expression) throws Exception { - // 使用简单的后缀表达式来评估表达式 - String[] tokens = expression.split("(?<=[-+*/()])|(?=[-+*/()])"); - Stack values = new Stack<>(); - Stack operations = new Stack<>(); - - for (String token : tokens) { - if (isNumeric(token)) { - values.push(Double.parseDouble(token)); - } else if (isOperator(token)) { - while (!operations.isEmpty() && precedence(token) <= precedence(operations.peek())) { - double b = values.pop(); - double a = values.pop(); - String op = operations.pop(); - values.push(applyOperation(a, b, op)); - } - operations.push(token); - } - } - - while (!operations.isEmpty()) { - double b = values.pop(); - double a = values.pop(); - String op = operations.pop(); - values.push(applyOperation(a, b, op)); - } - - return values.pop(); - } - - private boolean isNumeric(String str) { - return str.matches("-?\\d+(\\.\\d+)?"); - } - - private boolean isOperator(String str) { - return str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || - str.equals("√") || str.equals("x²") || str.equals("1/x"); - } - - private int precedence(String operator) { - switch (operator) { - case "+": case "-": - return 1; - case "×": case "÷": - return 2; - case "√": case "x²": - return 3; - default: - return 0; - } - } - - private double applyOperation(double a, double b, String operator) throws Exception { - switch (operator) { - case "+": - return a + b; - case "-": - return a - b; - case "*": - return a * b; - case "/": - if (b == 0) throw new Exception("除以零"); - return a / b; - case "√": - return Math.sqrt(b); - case "x²": - return Math.pow(a, 2); - case "1/x": - if (a == 0) throw new Exception("除以零"); - return 1 / a; - default: - return 0; - } - } -} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index fe7aa2b..0000000 --- a/src/Main.java +++ /dev/null @@ -1,15 +0,0 @@ -//TIP 要运行代码,请按 或 -// 点击装订区域中的 图标。 -public class Main { - public static void main(String[] args) { - //TIP 当文本光标位于高亮显示的文本处时按 - // 查看 IntelliJ IDEA 建议如何修正。 - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP 按 开始调试代码。我们已经设置了一个 断点 - // 但您始终可以通过按 添加更多断点。 - System.out.println("i = " + i); - } - } -} \ No newline at end of file