📅  最后修改于: 2023-12-03 14:58:27.064000             🧑  作者: Mango
这是一道 GATE-CS-2006 的考题,要求程序员完成一个计算器程序。
请生成一个简单的计算器界面,至少要实现以下功能:
完成的程序需要满足以下要求:
考虑到实现一个完整功能的计算器程序需要大量的UI设计和算法实现,我们分步骤进行解析,以方便完成该任务。
我们可以将界面分为以下几部分:
当用户在计算器上的数字键、运算符键等按键时,对应的事件需要被监听到。在Java中,监听器通常继承 ActionListener 接口或 KeyListener 接口,并把重写的方法注册给需要监听的组件。
核心计算功能考虑使用栈(Stack)的数据结构来实现。当数字或操作符被输入时,将其 push 进栈;当等号被按下时,从栈中取出数据进行运算,并将计算结果 push 进栈中。在处理乘除法时,需要考虑优先级和结合律。
把界面、监听器、核心算法模块组合起来,完成计算器程序的实现。
我们以Java Swing框架为例,实现一种可供参考的计算器程序代码。完整的代码片段如下:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Calculator implements ActionListener {
private JFrame frame;
private JTextField textField;
private JButton btnClear;
private JButton btnBack;
private JButton btnFlip;
private JButton btnAdd;
private JButton btnSubtract;
private JButton btnMultiply;
private JButton btnDivide;
private JButton btnDot;
private JButton btnEqual;
private JButton btnZero;
private JButton btnOne;
private JButton btnTwo;
private JButton btnThree;
private JButton btnFour;
private JButton btnFive;
private JButton btnSix;
private JButton btnSeven;
private JButton btnEight;
private JButton btnNine;
private double firstOperand;
private String operator;
private boolean resultDisplayed;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Calculator() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 240, 270);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setFont(new Font("Arial", Font.BOLD, 20));
frame.getContentPane().add(textField, BorderLayout.NORTH);
textField.setColumns(10);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(5, 4, 0, 0));
btnClear = new JButton("C");
panel.add(btnClear);
btnBack = new JButton("←");
panel.add(btnBack);
btnFlip = new JButton("+/-");
panel.add(btnFlip);
btnDivide = new JButton("/");
panel.add(btnDivide);
btnSeven = new JButton("7");
panel.add(btnSeven);
btnEight = new JButton("8");
panel.add(btnEight);
btnNine = new JButton("9");
panel.add(btnNine);
btnMultiply = new JButton("*");
panel.add(btnMultiply);
btnFour = new JButton("4");
panel.add(btnFour);
btnFive = new JButton("5");
panel.add(btnFive);
btnSix = new JButton("6");
panel.add(btnSix);
btnSubtract = new JButton("-");
panel.add(btnSubtract);
btnOne = new JButton("1");
panel.add(btnOne);
btnTwo = new JButton("2");
panel.add(btnTwo);
btnThree = new JButton("3");
panel.add(btnThree);
btnAdd = new JButton("+");
panel.add(btnAdd);
btnZero = new JButton("0");
panel.add(btnZero);
btnDot = new JButton(".");
panel.add(btnDot);
btnEqual = new JButton("=");
panel.add(btnEqual);
initializeListeners();
}
private void initializeListeners() {
btnClear.addActionListener(this);
btnBack.addActionListener(this);
btnFlip.addActionListener(this);
btnDivide.addActionListener(this);
btnSeven.addActionListener(this);
btnEight.addActionListener(this);
btnNine.addActionListener(this);
btnMultiply.addActionListener(this);
btnFour.addActionListener(this);
btnFive.addActionListener(this);
btnSix.addActionListener(this);
btnSubtract.addActionListener(this);
btnOne.addActionListener(this);
btnTwo.addActionListener(this);
btnThree.addActionListener(this);
btnAdd.addActionListener(this);
btnZero.addActionListener(this);
btnDot.addActionListener(this);
btnEqual.addActionListener(this);
}
private void calculate() {
double secondOperand = Double.parseDouble(textField.getText());
String result = "";
if (operator.equals("+")) {
result = Double.toString(firstOperand + secondOperand);
} else if (operator.equals("-")) {
result = Double.toString(firstOperand - secondOperand);
} else if (operator.equals("*")) {
result = Double.toString(firstOperand * secondOperand);
} else if (operator.equals("/")) {
result = Double.toString(firstOperand / secondOperand);
}
textField.setText(result);
resultDisplayed = true;
}
private void append(String text) {
if (resultDisplayed) {
textField.setText("");
resultDisplayed = false;
}
textField.setText(textField.getText() + text);
}
@Override
public void actionPerformed(ActionEvent event) {
String input = ((JButton) event.getSource()).getText();
switch (input) {
case "C":
textField.setText("");
resultDisplayed = false;
break;
case "←":
String currentText = textField.getText();
if (currentText.length() > 0) {
textField.setText(currentText.substring(0, currentText.length() - 1));
}
break;
case "+/-":
if (textField.getText().length() > 0 && !textField.getText().equals("0")) {
double value = Double.parseDouble(textField.getText());
value *= -1;
textField.setText(Double.toString(value));
}
break;
case "+":
case "-":
case "*":
case "/":
if (!resultDisplayed) {
operator = input;
firstOperand = Double.parseDouble(textField.getText());
textField.setText("");
}
break;
case ".":
if (!textField.getText().contains(".")) {
append(".");
}
break;
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
append(input);
break;
case "=":
if (!resultDisplayed) {
calculate();
}
break;
default:
break;
}
}
}
计算器程序是一个比较基础的小项目,但却涵盖了UI设计、监听器、算法等诸多重要知识点。通过完成该任务,可以加深对这些知识点的理解和运用,提高程序员的实际开发能力。