用Java实现实现计算器,backspace为退格键,单目运算符:+/-,1/x,sqrt,%双目运算符:+,-,*,/。CE为清除,C为清除全部,M+,M-,MR,MC,M+表示将当前显示的存储数值进行加法运算
时间: 2024-04-30 13:25:06 浏览: 137
以下是一个简单的Java计算器实现,包括上述要求的功能:
```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 JButton display;
private JButton backspace;
private JButton plusMinus;
private JButton reciprocal;
private JButton squareRoot;
private JButton percent;
private JButton clearEntry;
private JButton clearAll;
private JButton memoryPlus;
private JButton memoryMinus;
private JButton memoryRecall;
private JButton memoryClear;
private JButton divide;
private JButton multiply;
private JButton subtract;
private JButton add;
private JButton equals;
private double memory = 0;
private double currentNumber = 0;
private double result = 0;
private char lastOperator;
private boolean startNewNumber = true;
public Calculator() {
super("Calculator");
display = new JButton("0");
display.setEnabled(false);
backspace = new JButton("Backspace");
backspace.addActionListener(this);
plusMinus = new JButton("+/-");
plusMinus.addActionListener(this);
reciprocal = new JButton("1/x");
reciprocal.addActionListener(this);
squareRoot = new JButton("sqrt");
squareRoot.addActionListener(this);
percent = new JButton("%");
percent.addActionListener(this);
clearEntry = new JButton("CE");
clearEntry.addActionListener(this);
clearAll = new JButton("C");
clearAll.addActionListener(this);
memoryPlus = new JButton("M+");
memoryPlus.addActionListener(this);
memoryMinus = new JButton("M-");
memoryMinus.addActionListener(this);
memoryRecall = new JButton("MR");
memoryRecall.addActionListener(this);
memoryClear = new JButton("MC");
memoryClear.addActionListener(this);
divide = new JButton("/");
divide.addActionListener(this);
multiply = new JButton("*");
multiply.addActionListener(this);
subtract = new JButton("-");
subtract.addActionListener(this);
add = new JButton("+");
add.addActionListener(this);
equals = new JButton("=");
equals.addActionListener(this);
JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 5, 5));
buttonPanel.add(memoryClear);
buttonPanel.add(memoryRecall);
buttonPanel.add(memoryMinus);
buttonPanel.add(memoryPlus);
buttonPanel.add(percent);
buttonPanel.add(squareRoot);
buttonPanel.add(reciprocal);
buttonPanel.add(divide);
buttonPanel.add(7);
buttonPanel.add(8);
buttonPanel.add(9);
buttonPanel.add(multiply);
buttonPanel.add(4);
buttonPanel.add(5);
buttonPanel.add(6);
buttonPanel.add(subtract);
buttonPanel.add(1);
buttonPanel.add(2);
buttonPanel.add(3);
buttonPanel.add(add);
buttonPanel.add(plusMinus);
buttonPanel.add(0);
buttonPanel.add(".");
buttonPanel.add(equals);
buttonPanel.add(clearEntry);
buttonPanel.add(clearAll);
buttonPanel.add(backspace);
JPanel displayPanel = new JPanel(new BorderLayout());
displayPanel.add(display, BorderLayout.CENTER);
add(displayPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
setSize(300, 300);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void handleNumberInput(int number) {
if (startNewNumber) {
display.setText(String.valueOf(number));
currentNumber = number;
startNewNumber = false;
} else {
String current = display.getText();
if (current.equals("0")) {
display.setText(String.valueOf(number));
} else {
display.setText(current + number);
}
currentNumber = Double.parseDouble(display.getText());
}
}
private void handleOperatorInput(char operator) {
if (startNewNumber) {
lastOperator = operator;
} else {
switch (lastOperator) {
case '+':
result += currentNumber;
break;
case '-':
result -= currentNumber;
break;
case '*':
result *= currentNumber;
break;
case '/':
result /= currentNumber;
break;
case '%':
result %= currentNumber;
break;
default:
result = currentNumber;
break;
}
display.setText(String.valueOf(result));
currentNumber = result;
startNewNumber = true;
lastOperator = operator;
}
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == backspace) {
String current = display.getText();
if (current.length() > 1) {
display.setText(current.substring(0, current.length() - 1));
currentNumber = Double.parseDouble(display.getText());
} else {
display.setText("0");
currentNumber = 0;
startNewNumber = true;
}
} else if (source == plusMinus) {
currentNumber = -currentNumber;
display.setText(String.valueOf(currentNumber));
} else if (source == reciprocal) {
currentNumber = 1 / currentNumber;
display.setText(String.valueOf(currentNumber));
} else if (source == squareRoot) {
currentNumber = Math.sqrt(currentNumber);
display.setText(String.valueOf(currentNumber));
} else if (source == percent) {
handleOperatorInput('%');
} else if (source == clearEntry) {
display.setText("0");
currentNumber = 0;
startNewNumber = true;
} else if (source == clearAll) {
result = 0;
lastOperator = ' ';
display.setText("0");
currentNumber = 0;
startNewNumber = true;
} else if (source == memoryPlus) {
memory += currentNumber;
} else if (source == memoryMinus) {
memory -= currentNumber;
} else if (source == memoryRecall) {
currentNumber = memory;
display.setText(String.valueOf(currentNumber));
} else if (source == memoryClear) {
memory = 0;
} else if (source == divide) {
handleOperatorInput('/');
} else if (source == multiply) {
handleOperatorInput('*');
} else if (source == subtract) {
handleOperatorInput('-');
} else if (source == add) {
handleOperatorInput('+');
} else if (source == equals) {
switch (lastOperator) {
case '+':
result += currentNumber;
break;
case '-':
result -= currentNumber;
break;
case '*':
result *= currentNumber;
break;
case '/':
result /= currentNumber;
break;
case '%':
result %= currentNumber;
break;
default:
result = currentNumber;
break;
}
display.setText(String.valueOf(result));
currentNumber = result;
startNewNumber = true;
lastOperator = ' ';
} else {
handleNumberInput((Integer) source);
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个实现使用了Java的Swing GUI库来创建一个带有按钮和文本框的窗口,并在按钮上添加了相应的事件处理程序来实现计算器的功能。
阅读全文