You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.9 KiB

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
public class CalculatorControl {
@FXML
private TextArea display;
private double firstNumber;
private double secondNumber;
private String operation;
private boolean awaitingSecondNumber;
@FXML
void onClearButtonClicked(ActionEvent event) {
display.clear();
firstNumber = 0;
secondNumber = 0;
operation = null;
awaitingSecondNumber = false;
}
@FXML
void onClearEntryButtonClicked(ActionEvent event) {
display.clear();
}
@FXML
void onDeleteButtonClicked(ActionEvent event) {
String currentText = display.getText();
if (currentText != null && !currentText.isEmpty()) {
display.setText(currentText.substring(0, currentText.length() - 1));
}
}
@FXML
void onEqualsButtonClicked(ActionEvent event) {
if (awaitingSecondNumber) {
String displayText = display.getText();
String[] parts = displayText.split(" ");
try {
secondNumber = Double.parseDouble(parts[parts.length - 1]);
// 检查两个数字是否都是整数
if ((firstNumber % 1 == 0) && (secondNumber % 1 == 0)) {
int intFirstNumber = (int) firstNumber;
int intSecondNumber = (int) secondNumber;
switch (operation) {
case "+":
int result = intFirstNumber + intSecondNumber;
display.setText(String.format("%d + %d = %d", intFirstNumber, intSecondNumber, result));
break;
case "-":
result = intFirstNumber - intSecondNumber;
display.setText(String.format("%d - %d = %d", intFirstNumber, intSecondNumber, result));
break;
case "*":
result = intFirstNumber * intSecondNumber;
display.setText(String.format("%d * %d = %d", intFirstNumber, intSecondNumber, result));
break;
case "/":
if (intSecondNumber == 0) {
display.setText("Error");
} else {
result = intFirstNumber / intSecondNumber;
display.setText(String.format("%d / %d = %d", intFirstNumber, intSecondNumber, result));
}
break;
case "%":
result = intFirstNumber % intSecondNumber;
display.setText(String.format("%d %% %d = %d", intFirstNumber, intSecondNumber, result));
break;
}
} else if ((firstNumber % 1 != 0) || (secondNumber % 1 != 0)) {
double doubleFirstNumber = firstNumber;
double doubleSecondNumber = secondNumber;
switch (operation) {
case "+":
double result = doubleFirstNumber + doubleSecondNumber;
display.setText(String.format("%.2f + %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result));
break;
case "-":
result = doubleFirstNumber - doubleSecondNumber;
display.setText(String.format("%.2f - %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result));
break;
case "*":
result = doubleFirstNumber * doubleSecondNumber;
display.setText(String.format("%.2f * %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result));
break;
case "/":
if (doubleSecondNumber == 0) {
display.setText("Error");
} else {
result = doubleFirstNumber / doubleSecondNumber;
display.setText(String.format("%.2f / %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result));
}
break;
case "%":
double intPart = Math.floor(doubleFirstNumber);
double remainder = doubleFirstNumber - intPart;
display.setText(String.format("%.2f %% %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, remainder));
break;
}
} else {
display.setText("Non-integer input");
}
awaitingSecondNumber = false;
} catch (NumberFormatException e) {
display.setText("Error");
}
}
}
@FXML
void onNumberButtonClicked(ActionEvent event) {
Button button = (Button) event.getSource();
String buttonText = button.getText();
display.appendText(buttonText);
}
@FXML
void onOperationButtonClicked(ActionEvent event) {
Button button = (Button) event.getSource();
String buttonText = button.getText();
if (!awaitingSecondNumber) {
String displayText = display.getText();
String[] parts = displayText.split(" ");
try {
firstNumber = Double.parseDouble(parts[parts.length - 1]);
operation = buttonText;
awaitingSecondNumber = true;
display.appendText(" " + buttonText + " ");
} catch (NumberFormatException e) {
display.setText("Error");
}
}
}
}