java计算器退格功能
时间: 2023-12-13 22:05:14 浏览: 222
以下是一个简单的Java计算器,具有退格功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
private JPanel buttonPanel;
public Calculator() {
display = new JTextField("0", 20);
display.setEditable(false);
buttons = new JButton[buttonLabels.length];
buttonPanel = new JPanel(new GridLayout(4, 4));
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
getContentPane().add(display, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
pack();
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
display.setText("0");
} else if (command.equals("=")) {
String expression = display.getText();
try {
double result = evaluate(expression);
display.setText(Double.toString(result));
} catch (Exception ex) {
display.setText("Error");
}
} else if (command.equals("Backspace")) {
String text = display.getText();
if (text.length() > 0) {
text = text.substring(0, text.length() - 1);
if (text.length() == 0) {
text = "0";
}
display.setText(text);
}
} else {
String text = display.getText();
if (text.equals("0")) {
text = "";
}
display.setText(text + command);
}
}
private double evaluate(String expression) throws Exception {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ')
nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() throws Exception {
nextChar();
double x = parseExpression();
if (pos < expression.length())
throw new Exception("Unexpected: " + (char) ch);
return x;
}
double parseExpression() throws Exception {
double x = parseTerm();
for (;;) {
if (eat('+'))
x += parseTerm();
else if (eat('-'))
x -= parseTerm();
else
return x;
}
}
double parseTerm() throws Exception {
double x = parseFactor();
for (;;) {
if (eat('*'))
x *= parseFactor();
else if (eat('/'))
x /= parseFactor();
else
return x;
}
}
double parseFactor() throws Exception {
if (eat('+'))
return parseFactor();
if (eat('-'))
return -parseFactor();
double x;
int startPos = this.pos;
if (eat('(')) {
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') {
while ((ch >= '0' && ch <= '9') || ch == '.')
nextChar();
x = Double.parseDouble(expression.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') {
while (ch >= 'a' && ch <= 'z')
nextChar();
String func = expression.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt"))
x = Math.sqrt(x);
else if (func.equals("sin"))
x = Math.sin(Math.toRadians(x));
else if (func.equals("cos"))
x = Math.cos(Math.toRadians(x));
else if (func.equals("tan"))
x = Math.tan(Math.toRadians(x));
else
throw new Exception("Unknown function: " + func);
} else {
throw new Exception("Unexpected: " + (char) ch);
}
if (eat('^'))
x = Math.pow(x, parseFactor());
return x;
}
}.parse();
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.setVisible(true);
}
}
```
阅读全文