pull/1/head
gxy 2 months ago
commit 9757ac0c96

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,200 @@
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<Double> values = new Stack<>();
Stack<String> 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;
}
}
}

@ -0,0 +1,15 @@
//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
public static void main(String[] args) {
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
// 查看 IntelliJ IDEA 建议如何修正。
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
// 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
System.out.println("i = " + i);
}
}
}
Loading…
Cancel
Save