搜索工作 |
搜索工作 公司点评 查工资 看趋势
微信

使用微信服务,更方便

职友集>Java面试题 > java计算器的实现

java计算器的实现

2015-03-05 06:30:01 阅读( 154 )

1770人 收藏本页

标签:Java面试题

package com.mzxy.jsq;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
public class ComputerPad extends JFrame implements ActionListener {
private NumberButton[] numberButton;
private OperationButton[] operationButton;
private JButton dotButton, signButton, backspaceButton, reciprocalButton,
equalButton, clearZeroButton;
private JPanel panel;
private JTextField resultShow;
private String operators[] = { “+”, “-”, “*”, “/” };
private LinkedList dataList;
private boolean equalButtonIsPressed = false;
public ComputerPad() {
super(“计算器”);
dataList = new LinkedList();
numberButton = new NumberButton[10];
for (int i = 0; i <= 9; i++) {
numberButton = new NumberButton(i);
numberButton.addActionListener(this);
}
operationButton = new OperationButton[4];
for (int i = 0; i < 4; i++) {
operationButton = new OperationButton(operators);
operationButton.addActionListener(this);
}
dotButton = new JButton(“.”);
signButton = new JButton(“+/-”);
equalButton = new JButton(“=”);
reciprocalButton = new JButton(“1/x”);
backspaceButton = new JButton(“退格”);
clearZeroButton = new JButton(“C”);
clearZeroButton.setForeground(Color.red);
backspaceButton.setForeground(Color.red);
reciprocalButton.setForeground(Color.red);
equalButton.setForeground(Color.blue);
signButton.setForeground(Color.blue);
dotButton.setForeground(Color.blue);
clearZeroButton.addActionListener(this);
backspaceButton.addActionListener(this);
reciprocalButton.addActionListener(this);
equalButton.addActionListener(this);
signButton.addActionListener(this);
dotButton.addActionListener(this);
resultShow = new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font(“TimesRoman”, Font.PLAIN, 14));
resultShow.setBorder(new BevelBorder(BevelBorder.LOWERED));
resultShow.setBackground(Color.white);
resultShow.setEditable(false);
panel = new JPanel();
panel.setLayout(new GridLayout(4, 5));
// 面板中的第一行
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(operationButton[0]);
panel.add(clearZeroButton);
// 面板中的第2行
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operationButton[1]);
panel.add(backspaceButton);
// 面板中的第3行
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operationButton[2]);
panel.add(reciprocalButton);
// 面板中的第4行
panel.add(numberButton[0]);
panel.add(signButton);
panel.add(dotButton);
panel.add(operationButton[3]);
panel.add(equalButton);
add(panel, BorderLayout.CENTER);
add(resultShow, BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100, 50, 340, 180);
setResizable(false);
validate();
}
public static void main(String[] args) {
new ComputerPad();
}
@Override
public void actionPerformed(ActionEvent e) {
// 1.如果用户按下的是数字按钮
if (e.getSource() instanceof NumberButton) {
NumberButton b = (NumberButton) e.getSource();
String s = “” + b.getNumber();
if (dataList.size() == 0) {
dataList.add(s);
resultShow.setText(s);
} else if (dataList.size() == 1 && equalButtonIsPressed == false) {
String s1 = dataList.get(0);
String s2 = s1 + s;
dataList.set(0, s2);
resultShow.setText(s2);
} else if (dataList.size() == 1 && equalButtonIsPressed == true) {
dataList.set(0, s);
resultShow.setText(s);
equalButtonIsPressed = false;
} else if (dataList.size() == 2) {
dataList.add(s);
resultShow.setText(s);
} else if (dataList.size() == 3) {
String s1 = dataList.get(2);
String s2 = s1 + s;
dataList.set(2, s2);
resultShow.setText(s2);
}
}
// 2.如果用户按下的是操作符按钮
else if (e.getSource() instanceof OperationButton) {
OperationButton op = (OperationButton) e.getSource();
String ops = op.getOperator();
if (dataList.size() == 1) {
System.out.println(“2 size=1———”);
dataList.add(ops);
System.out.println(dataList);
} else if (dataList.size() == 2) {
dataList.set(1, ops);
} else if (dataList.size() == 3) {
String n1 = dataList.get(0);
String oper = dataList.get(1);
String n2 = dataList.get(2);
double d1 = Double.parseDouble(n1);
double d2 = Double.parseDouble(n2);
double d = 0.0;
if (oper.equals(“+”))
d = d1 + d2;
else if (oper.equals(“-”))
d = d1 – d2;
else if (oper.equals(“*”))
d = d1 * d2;
else if (oper.equals(“/”))
d = d1 / d2;
dataList.clear();
dataList.add(“” + d);
resultShow.setText(“” + d);
}
}
// 3.如果用户按下的是正负号按钮
else if (e.getSource() == signButton) {
if (dataList.size() == 1) {
String s = dataList.get(0);
char c = s.charAt(0);
if (c == ‘-’) {
s = s.substring(1);
} else {
s = “-” + s;
}
dataList.set(0, s);
resultShow.setText(s);
} else if (dataList.size() == 3) {
String s = dataList.get(2);
char c = s.charAt(0);
if (c == ‘-’) {
s = s.substring(1);
} else {
s = “-” + s;
}
dataList.set(2, s);
resultShow.setText(s);
}
}
// 4.如果用户按下的是小数点按钮
else if (e.getSource() == dotButton) {
if (dataList.size() == 0) {
dataList.add(“0.”);
resultShow.setText(“0.”);
} else if (dataList.size() == 1) {
String s = dataList.get(0);
if (!s.contains(“.”)) {
dataList.set(0, s + “.”);
resultShow.setText(s + “.”);
}
} else if (dataList.size() == 3) {
String s = dataList.get(2);
if (!s.contains(“.”)) {
dataList.set(2, s + “.”);
resultShow.setText(s + “.”);
}
}
}
// 5.如果用户按下的是清空按钮
else if (e.getSource() == clearZeroButton) {
dataList.clear();
resultShow.setText(“0″);
}
// 6.如果用户按下的是退格按钮
else if (e.getSource() == backspaceButton) {
if (dataList.size() == 1) {
String s = dataList.get(0);
if (s.length() >= 1) {
s = s.substring(0, s.length() – 1);
}
if (s.length() == 0) {
dataList.clear();
resultShow.setText(“0″);
} else {
dataList.set(0, s);
resultShow.setText(s);
}
} else if (dataList.size() == 3) {
String s = dataList.get(2);
if (s.length() >= 1) {
s = s.substring(0, s.length() – 1);
}
if (s.length() == 0) {
dataList.removeLast();
resultShow.setText(“0″);
} else {
dataList.set(2, s);
resultShow.setText(s);
}
}
}
// 7.如果用户按下的是倒数按钮
else if (e.getSource() == reciprocalButton) {
if(dataList.size() == 1 || dataList.size() == 2){
String number1 = (String)dataList.getFirst();
try{
double d = Double.parseDouble(number1);
d = 1.0 / d;
String str = String.valueOf(d);
dataList.set(0, str);
resultShow.setText(str);
}
catch(Exception ee){

}
}
}
// 8.如果用户按下的是等号按钮
else {
equalButtonIsPressed = true;
if (dataList.size() == 2) {
System.out.println(“============”);
String n1 = dataList.get(0);
String oper = dataList.get(1);
double d1 = Double.parseDouble(n1);
double d = 0.0;
if (oper.equals(“+”))
d = d1 + d1;
else if (oper.equals(“-”))
d = d1 – d1;
else if (oper.equals(“*”))
d = d1 * d1;
else if (oper.equals(“/”))
d = d1 / d1;
// equalButtonIsPressed = false;
dataList.clear();
dataList.add(“” + d);
resultShow.setText(“” + d);
} else if (dataList.size() == 3) {
String n1 = dataList.get(0);
String oper = dataList.get(1);
String n2 = dataList.get(2);
double d1 = Double.parseDouble(n1);
double d2 = Double.parseDouble(n2);
double d = 0.0;
if (oper.equals(“+”))
d = d1 + d2;
else if (oper.equals(“-”))
d = d1 – d2;
else if (oper.equals(“*”))
d = d1 * d2;
else if (oper.equals(“/”))
d = d1 / d2;
dataList.clear();
dataList.add(“” + d);
resultShow.setText(“” + d);
// equalButtonIsPressed = false;
}
}
}
}

import java.awt.Color;
import javax.swing.JButton;
public class NumberButton extends JButton {
private int number;
public NumberButton(int number) {
super(“” + number);
this.number = number;
setForeground(Color.blue);
// 还可以添加代码设置其他属性,以满足自己需要的外观
}
public int getNumber() {
return number;
}
}

import java.awt.Color;
import javax.swing.JButton;
public class OperationButton extends JButton {
private String operator;
public OperationButton(String s) {
super(s);
operator = s;
setForeground(Color.red);
// 还可以添加代码设置其他属性,以满足自己需要的外观
}
public String getOperator() {
return operator;
}
}

来自IT公司面试手册

下一篇:在java中怎样获得日期差,求天数

上一篇:解释一下class.forname的用法

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息: 面试题