MINAMI 6 months ago
parent 40ce227cb1
commit 8abd258c3b

@ -15,7 +15,6 @@ public class calculator extends JFrame implements ActionListener {
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout()); setLayout(new BorderLayout());
// 增加结果显示区域的高度
display = new JTextField("0"); display = new JTextField("0");
display.setEditable(false); display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT); display.setHorizontalAlignment(JTextField.RIGHT);
@ -24,14 +23,14 @@ public class calculator extends JFrame implements ActionListener {
add(display, BorderLayout.NORTH); add(display, BorderLayout.NORTH);
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4)); // 增加一行用于放置清除按钮 panel.setLayout(new GridLayout(5, 4));
String[] buttons = { String[] buttons = {
"7", "8", "9", "/", "7", "8", "9", "/",
"4", "5", "6", "*", "4", "5", "6", "*",
"1", "2", "3", "-", "1", "2", "3", "-",
"0", ".", "=", "+", "0", ".", "=", "+",
"C" // 清除按钮 "C", "%" // 添加取余按钮
}; };
for (String label : buttons) { for (String label : buttons) {
@ -42,7 +41,7 @@ public class calculator extends JFrame implements ActionListener {
} }
add(panel, BorderLayout.CENTER); add(panel, BorderLayout.CENTER);
setLocationRelativeTo(null); // 居中显示 setLocationRelativeTo(null);
} }
@Override @Override
@ -56,15 +55,14 @@ public class calculator extends JFrame implements ActionListener {
} else { } else {
display.setText(display.getText() + command); display.setText(display.getText() + command);
} }
} else if ("+-*/%".indexOf(command) >= 0) { } else if ("+-*/%".indexOf(command) >= 0) { // 添加 % 运算符
equation = display.getText() + command; equation = display.getText() + command;
display.setText("0"); display.setText("0");
newNumber = true; newNumber = true;
} else if (command.equals("=")) { } else if (command.equals("=")) {
try { try {
equation += display.getText(); equation += display.getText();
double result = eval(equation); double result = new ExpressionEvaluator(equation).evaluate();
// 检查结果是否为整数
if (result == (long) result) { if (result == (long) result) {
display.setText(String.valueOf((long) result)); display.setText(String.valueOf((long) result));
} else { } else {
@ -81,75 +79,79 @@ public class calculator extends JFrame implements ActionListener {
} }
} }
private double eval(final String str) { public static void main(String[] args) {
return new Object() { SwingUtilities.invokeLater(() -> {
int pos = -1, ch; calculator calc = new calculator();
calc.setVisible(true);
});
}
}
void nextChar() { class ExpressionEvaluator {
ch = (++pos < str.length()) ? str.charAt(pos) : -1; private int pos = -1;
} private int ch;
private final String str;
boolean eat(int charToEat) { public ExpressionEvaluator(String str) {
while (ch == ' ') nextChar(); this.str = str;
if (ch == charToEat) { }
nextChar();
return true;
}
return false;
}
double parse() { private void nextChar() {
nextChar(); ch = (++pos < str.length()) ? str.charAt(pos) : -1;
double x = parseExpression(); }
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
return x;
}
double parseExpression() { private boolean eat(int charToEat) {
double x = parseTerm(); while (ch == ' ') nextChar();
for (;;) { if (ch == charToEat) {
if (eat('+')) x += parseTerm(); // addition nextChar();
else if (eat('-')) x -= parseTerm(); // subtraction return true;
else return x; }
} return false;
} }
double parseTerm() { public double evaluate() {
double x = parseFactor(); nextChar();
for (;;) { double x = parseExpression();
if (eat('*')) x *= parseFactor(); // multiplication if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
else if (eat('/')) x /= parseFactor(); // division return x;
else return x; }
}
}
double parseFactor() { private double parseExpression() {
if (eat('+')) return parseFactor(); // unary plus double x = parseTerm();
if (eat('-')) return -parseFactor(); // unary minus for (;;) {
if (eat('+')) x += parseTerm(); // addition
double x; else if (eat('-')) x -= parseTerm(); // subtraction
int startPos = this.pos; else return x;
if (eat('(')) { // parentheses }
x = parseExpression(); }
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation private double parseTerm() {
return x; double x = parseFactor();
} for (;;) {
}.parse(); if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else if (eat('%')) x %= parseFactor(); // modulus
else return x;
}
} }
public static void main(String[] args) { private double parseFactor() {
SwingUtilities.invokeLater(() -> { if (eat('+')) return parseFactor(); // unary plus
calculator calc = new calculator(); if (eat('-')) return -parseFactor(); // unary minus
calc.setVisible(true);
}); double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
} }
} }

Loading…
Cancel
Save