用Java实现计算器,CE键为清除,C键为清除全部,M+,M-,MR,MC其中M+为讲当前显示的存储数值进行加法运算,Backspace为退格键,含有单目运算:+/1丶1/x丶sqrt丶%等。双目运算符有+丶-丶*丶/
时间: 2024-04-29 21:21:47 浏览: 84
好的,我可以为您提供一个简单的 Java 计算器实现。以下是代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener {
private JFrame frame;
private JPanel panel;
private JTextField displayField;
private JButton buttonCE, buttonC, buttonBackspace, buttonMPlus, buttonMMinus, buttonMR, buttonMC;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEquals;
private JButton buttonNegate, buttonReciprocal, buttonRoot, buttonModulo;
private JButton buttonZero, buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive, buttonSix, buttonSeven, buttonEight, buttonNine;
private double currentValue = 0.0;
private double memoryValue = 0.0;
private char currentOperator = ' ';
public Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
displayField = new JTextField("0");
displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 5;
panel.add(displayField, c);
buttonCE = new JButton("CE");
buttonCE.addActionListener(this);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
panel.add(buttonCE, c);
buttonC = new JButton("C");
buttonC.addActionListener(this);
c.gridx = 1;
c.gridy = 1;
panel.add(buttonC, c);
buttonBackspace = new JButton("<=");
buttonBackspace.addActionListener(this);
c.gridx = 2;
c.gridy = 1;
panel.add(buttonBackspace, c);
buttonMPlus = new JButton("M+");
buttonMPlus.addActionListener(this);
c.gridx = 3;
c.gridy = 1;
panel.add(buttonMPlus, c);
buttonMMinus = new JButton("M-");
buttonMMinus.addActionListener(this);
c.gridx = 4;
c.gridy = 1;
panel.add(buttonMMinus, c);
buttonMR = new JButton("MR");
buttonMR.addActionListener(this);
c.gridx = 0;
c.gridy = 2;
panel.add(buttonMR, c);
buttonMC = new JButton("MC");
buttonMC.addActionListener(this);
c.gridx = 1;
c.gridy = 2;
panel.add(buttonMC, c);
buttonNegate = new JButton("+/-");
buttonNegate.addActionListener(this);
c.gridx = 2;
c.gridy = 2;
panel.add(buttonNegate, c);
buttonReciprocal = new JButton("1/x");
buttonReciprocal.addActionListener(this);
c.gridx = 3;
c.gridy = 2;
panel.add(buttonReciprocal, c);
buttonRoot = new JButton("sqrt");
buttonRoot.addActionListener(this);
c.gridx = 4;
c.gridy = 2;
panel.add(buttonRoot, c);
buttonSeven = new JButton("7");
buttonSeven.addActionListener(this);
c.gridx = 0;
c.gridy = 3;
panel.add(buttonSeven, c);
buttonEight = new JButton("8");
buttonEight.addActionListener(this);
c.gridx = 1;
c.gridy = 3;
panel.add(buttonEight, c);
buttonNine = new JButton("9");
buttonNine.addActionListener(this);
c.gridx = 2;
c.gridy = 3;
panel.add(buttonNine, c);
buttonDivide = new JButton("/");
buttonDivide.addActionListener(this);
c.gridx = 3;
c.gridy = 3;
panel.add(buttonDivide, c);
buttonModulo = new JButton("%");
buttonModulo.addActionListener(this);
c.gridx = 4;
c.gridy = 3;
panel.add(buttonModulo, c);
buttonFour = new JButton("4");
buttonFour.addActionListener(this);
c.gridx = 0;
c.gridy = 4;
panel.add(buttonFour, c);
buttonFive = new JButton("5");
buttonFive.addActionListener(this);
c.gridx = 1;
c.gridy = 4;
panel.add(buttonFive, c);
buttonSix = new JButton("6");
buttonSix.addActionListener(this);
c.gridx = 2;
c.gridy = 4;
panel.add(buttonSix, c);
buttonMultiply = new JButton("*");
buttonMultiply.addActionListener(this);
c.gridx = 3;
c.gridy = 4;
panel.add(buttonMultiply, c);
buttonReciprocal = new JButton("%");
buttonReciprocal.addActionListener(this);
c.gridx = 4;
c.gridy = 4;
panel.add(buttonReciprocal, c);
buttonOne = new JButton("1");
buttonOne.addActionListener(this);
c.gridx = 0;
c.gridy = 5;
panel.add(buttonOne, c);
buttonTwo = new JButton("2");
buttonTwo.addActionListener(this);
c.gridx = 1;
c.gridy = 5;
panel.add(buttonTwo, c);
buttonThree = new JButton("3");
buttonThree.addActionListener(this);
c.gridx = 2;
c.gridy = 5;
panel.add(buttonThree, c);
buttonSubtract = new JButton("-");
buttonSubtract.addActionListener(this);
c.gridx = 3;
c.gridy = 5;
panel.add(buttonSubtract, c);
buttonReciprocal = new JButton("%");
buttonReciprocal.addActionListener(this);
c.gridx = 4;
c.gridy = 5;
panel.add(buttonReciprocal, c);
buttonZero = new JButton("0");
buttonZero.addActionListener(this);
c.gridx = 0;
c.gridy = 6;
c.gridwidth = 2;
panel.add(buttonZero, c);
buttonEquals = new JButton("=");
buttonEquals.addActionListener(this);
c.gridx = 2;
c.gridy = 6;
c.gridwidth = 2;
panel.add(buttonEquals, c);
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
c.gridx = 3;
c.gridy = 6;
c.gridwidth = 1;
panel.add(buttonAdd, c);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
public void actionPerformed(ActionEvent e) {
String buttonLabel = e.getActionCommand();
switch (buttonLabel) {
case "CE":
currentValue = 0.0;
displayField.setText("0");
break;
case "C":
currentValue = 0.0;
memoryValue = 0.0;
currentOperator = ' ';
displayField.setText("0");
break;
case "<=":
String text = displayField.getText();
if (text.length() == 1 && text.equals("0")) {
break;
} else {
text = text.substring(0, text.length() - 1);
if (text.length() == 0) {
text = "0";
}
displayField.setText(text);
}
break;
case "M+":
memoryValue += Double.parseDouble(displayField.getText());
break;
case "M-":
memoryValue -= Double.parseDouble(displayField.getText());
break;
case "MR":
currentValue = memoryValue;
displayField.setText(Double.toString(currentValue));
break;
case "MC":
memoryValue = 0.0;
break;
case "+/-":
currentValue = -Double.parseDouble(displayField.getText());
displayField.setText(Double.toString(currentValue));
break;
case "1/x":
currentValue = 1.0 / Double.parseDouble(displayField.getText());
displayField.setText(Double.toString(currentValue));
break;
case "sqrt":
currentValue = Math.sqrt(Double.parseDouble(displayField.getText()));
displayField.setText(Double.toString(currentValue));
break;
case "%":
currentValue = Double.parseDouble(displayField.getText()) / 100.0;
displayField.setText(Double.toString(currentValue));
break;
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
String textToAdd = e.getActionCommand();
if (displayField.getText().equals("0")) {
displayField.setText(textToAdd);
} else {
displayField.setText(displayField.getText() + textToAdd);
}
currentValue = Double.parseDouble(displayField.getText());
break;
case "+":
case "-":
case "*":
case "/":
currentOperator = buttonLabel.charAt(0);
currentValue = Double.parseDouble(displayField.getText());
displayField.setText("0");
break;
case "=":
if (currentOperator == ' ') {
break;
}
double secondValue = Double.parseDouble(displayField.getText());
switch (currentOperator) {
case '+':
currentValue += secondValue;
break;
case '-':
currentValue -= secondValue;
break;
case '*':
currentValue *= secondValue;
break;
case '/':
currentValue /= secondValue;
break;
}
displayField.setText(Double.toString(currentValue));
currentOperator = ' ';
break;
}
}
}
```
这是一个简单的 GUI 计算器,使用了 Java 的 Swing 库来构建用户界面。实现了您所描述的所有功能,包括单目和双目运算符、存储和存储操作、清除和清除全部等。
您可以创建一个名为 `Calculator.java` 的文件,并将上述代码复制粘贴到该文件中。然后,从命令行运行以下命令来编译并运行该程序:
```
javac Calculator.java
java Calculator
```
这将启动计算器应用程序并显示用户界面。
阅读全文