基于swing的计算器sin
时间: 2023-07-24 13:49:31 浏览: 101
以下是一个基于 Swing 的计算器,包含 sin 函数的实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "/", "sin",
"4", "5", "6", "*", "cos",
"1", "2", "3", "-", "tan",
"0", ".", "C", "+", "="
};
private JPanel buttonPanel;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
display = new JTextField("0", 20);
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().add(display, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 5));
buttons = new JButton[25];
for (int i = 0; i < 25; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
getContentPane().add(buttonPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
display.setText("0");
} else if (command.equals("=")) {
try {
double result = eval(display.getText());
display.setText(Double.toString(result));
} catch (Exception ex) {
display.setText("Error");
}
} else if (command.equals("sin")) {
try {
double result = Math.sin(Math.toRadians(Double.parseDouble(display.getText())));
display.setText(Double.toString(result));
} catch (Exception ex) {
display.setText("Error");
}
} else if (command.equals("cos")) {
try {
double result = Math.cos(Math.toRadians(Double.parseDouble(display.getText())));
display.setText(Double.toString(result));
} catch (Exception ex) {
display.setText("Error");
}
} else if (command.equals("tan")) {
try {
double result = Math.tan(Math.toRadians(Double.parseDouble(display.getText())));
display.setText(Double.toString(result));
} catch (Exception ex) {
display.setText("Error");
}
} else {
if (display.getText().equals("0")) {
display.setText(command);
} else {
display.setText(display.getText() + command);
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
public static double eval(String str) {
return new Object() {
int pos = -1, ch;
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();
}
}
```
这个计算器在每次点击 sin 按钮时,将文本框中的值解析为角度,然后计算 sine 函数的值,最后更新文本框的内容。cos 和 tan 函数的实现类似。
阅读全文