|
|
|
@ -1,48 +1,155 @@
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
|
|
public class calculator {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入第一个数:");
|
|
|
|
|
double num1 = scanner.nextDouble();
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入第二个数:");
|
|
|
|
|
double num2 = scanner.nextDouble();
|
|
|
|
|
|
|
|
|
|
System.out.print("请选择操作符 (+, -, *, /, %):");
|
|
|
|
|
String operation = scanner.next();
|
|
|
|
|
|
|
|
|
|
double result;
|
|
|
|
|
switch (operation) {
|
|
|
|
|
case "+":
|
|
|
|
|
result = num1 + num2;
|
|
|
|
|
break;
|
|
|
|
|
case "-":
|
|
|
|
|
result = num1 - num2;
|
|
|
|
|
break;
|
|
|
|
|
case "*":
|
|
|
|
|
result = num1 * num2;
|
|
|
|
|
break;
|
|
|
|
|
case "/":
|
|
|
|
|
if (num2 == 0) {
|
|
|
|
|
System.out.println("错误:除数不能为0");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
result = num1 / num2;
|
|
|
|
|
break;
|
|
|
|
|
case "%":
|
|
|
|
|
if (num2 == 0) {
|
|
|
|
|
System.out.println("错误:除数不能为0");
|
|
|
|
|
return;
|
|
|
|
|
public class calculator extends JFrame implements ActionListener {
|
|
|
|
|
|
|
|
|
|
private JTextField display;
|
|
|
|
|
private String equation = "";
|
|
|
|
|
private boolean newNumber = true;
|
|
|
|
|
|
|
|
|
|
public calculator() {
|
|
|
|
|
setTitle("简易计算器");
|
|
|
|
|
setSize(300, 400);
|
|
|
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
|
|
|
setLayout(new BorderLayout());
|
|
|
|
|
|
|
|
|
|
// 增加结果显示区域的高度
|
|
|
|
|
display = new JTextField("0");
|
|
|
|
|
display.setEditable(false);
|
|
|
|
|
display.setHorizontalAlignment(JTextField.RIGHT);
|
|
|
|
|
display.setFont(new Font("Arial", Font.BOLD, 24));
|
|
|
|
|
display.setPreferredSize(new Dimension(300, 60));
|
|
|
|
|
add(display, BorderLayout.NORTH);
|
|
|
|
|
|
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
|
panel.setLayout(new GridLayout(5, 4)); // 增加一行用于放置清除按钮
|
|
|
|
|
|
|
|
|
|
String[] buttons = {
|
|
|
|
|
"7", "8", "9", "/",
|
|
|
|
|
"4", "5", "6", "*",
|
|
|
|
|
"1", "2", "3", "-",
|
|
|
|
|
"0", ".", "=", "+",
|
|
|
|
|
"C" // 清除按钮
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (String label : buttons) {
|
|
|
|
|
JButton button = new JButton(label);
|
|
|
|
|
button.setFont(new Font("Arial", Font.BOLD, 18));
|
|
|
|
|
button.addActionListener(this);
|
|
|
|
|
panel.add(button);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(panel, BorderLayout.CENTER);
|
|
|
|
|
setLocationRelativeTo(null); // 居中显示
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
String command = e.getActionCommand();
|
|
|
|
|
|
|
|
|
|
if ("0123456789.".indexOf(command) >= 0) {
|
|
|
|
|
if (newNumber) {
|
|
|
|
|
display.setText(command);
|
|
|
|
|
newNumber = false;
|
|
|
|
|
} else {
|
|
|
|
|
display.setText(display.getText() + command);
|
|
|
|
|
}
|
|
|
|
|
} else if ("+-*/%".indexOf(command) >= 0) {
|
|
|
|
|
equation = display.getText() + command;
|
|
|
|
|
display.setText("0");
|
|
|
|
|
newNumber = true;
|
|
|
|
|
} else if (command.equals("=")) {
|
|
|
|
|
try {
|
|
|
|
|
equation += display.getText();
|
|
|
|
|
double result = eval(equation);
|
|
|
|
|
// 检查结果是否为整数
|
|
|
|
|
if (result == (long) result) {
|
|
|
|
|
display.setText(String.valueOf((long) result));
|
|
|
|
|
} else {
|
|
|
|
|
display.setText(String.valueOf(result));
|
|
|
|
|
}
|
|
|
|
|
result = num1 % num2;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("无效的操作符");
|
|
|
|
|
return;
|
|
|
|
|
newNumber = true;
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
display.setText("Error");
|
|
|
|
|
}
|
|
|
|
|
} else if (command.equals("C")) {
|
|
|
|
|
display.setText("0");
|
|
|
|
|
equation = "";
|
|
|
|
|
newNumber = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double eval(final String str) {
|
|
|
|
|
return new Object() {
|
|
|
|
|
int pos = -1, ch;
|
|
|
|
|
|
|
|
|
|
System.out.printf("结果是: %.2f\n", result);
|
|
|
|
|
void nextChar() {
|
|
|
|
|
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean eat(int charToEat) {
|
|
|
|
|
while (ch == ' ') nextChar();
|
|
|
|
|
if (ch == charToEat) {
|
|
|
|
|
nextChar();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double parse() {
|
|
|
|
|
nextChar();
|
|
|
|
|
double x = parseExpression();
|
|
|
|
|
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double parseExpression() {
|
|
|
|
|
double x = parseTerm();
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (eat('+')) x += parseTerm(); // addition
|
|
|
|
|
else if (eat('-')) x -= parseTerm(); // subtraction
|
|
|
|
|
else return x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double parseTerm() {
|
|
|
|
|
double x = parseFactor();
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (eat('*')) x *= parseFactor(); // multiplication
|
|
|
|
|
else if (eat('/')) x /= parseFactor(); // division
|
|
|
|
|
else return x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double parseFactor() {
|
|
|
|
|
if (eat('+')) return parseFactor(); // unary plus
|
|
|
|
|
if (eat('-')) return -parseFactor(); // unary minus
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}.parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
|
calculator calc = new calculator();
|
|
|
|
|
calc.setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|