展开全部
^简易的计算器
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calcular implements ActionListener {
JTextField jtf = new JTextField(10);
boolean flag = false;
boolean emblem = false;
@SuppressWarnings("static-access")
public Calcular() {
JFrame jf = new JFrame("testcalcular");
jf.setLayout(new BorderLayout());
jtf.setEditable(false);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jtf.setText("0");
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(5, 4));
String[] lab = { "BACK", "CE", "C", "+", "7", "8", "9", "-", "4", "5",
"6", "*", "3", "2", "1", "/", "0", "+/-", ".", "=" };
JButton[] jb = new JButton[lab.length];
for (int i = 0; i < jb.length; i++) {
jb[i] = new JButton(lab[i]);
if (lab[i].matches("^e68a8462616964757a686964616f31333335303439\\d$")) {
jb[i].setBackground(Color.PINK);
} else if (lab[i].matches("^[a-zA-Z]+$")) {
jb[i].setBackground(Color.GREEN);
} else {
jb[i].setBackground(Color.LIGHT_GRAY);
}
jb[i].addActionListener(this);
jp.add(jb[i]);
}
jf.add(jtf, BorderLayout.NORTH);
jf.add(jp);
jf.setSize(300, 200);
jf.setLocation(300, 200);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Calcular();
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if ("CE".equals(comm)) {
jtf.setText("0");
flag = false;
emblem = false;
} else if ("C".equals(comm)) {
jtf.setText("0");
flag = false;
emblem = false;
} else if ("BACK".equals(comm)) {
String com = jtf.getText() + comm;
com = com.substring(0, com.length() - 5);
if (com.length() == 0) {
jtf.setText("0");
flag = false;
} else {
jtf.setText(com);
}
} else if ("+-*/".contains(comm)) {
if (emblem) {
String num = number(jtf.getText());
jtf.setText(num + comm);
emblem = false;
} else {
String number = jtf.getText() + comm;
jtf.setText(number);
emblem = true;
}
} else if ("=".equals(comm)) {
emblem = false;
jtf.setText(number(jtf.getText()));
} else if ("+/-".equals(comm)) {
String com = jtf.getText();
com = com.substring(0, 1);
if ("-".equals(com)) {
com = jtf.getText();
com = com.substring(1, com.length());
jtf.setText(com);
} else {
com = "-" + jtf.getText();
jtf.setText(com);
}
} else {
if (flag) {
String com = jtf.getText() + comm;
jtf.setText(com);
} else {
jtf.setText(comm);
if ("0".equals(comm)) {
flag = false;
} else {
flag = true;
}
}
}
}
public String number(String s) {
BigDecimal a = null;
String ss = "^[-]{1}\\d+[\\+\\-\\*\\/]{1}\\d+$";
String sss = "^\\d+[\\+\\-\\*\\/]{1}\\d+$";
if (s.matches(sss)) {
if (s.contains("*")) {
String[] str = s.split("\\*");
BigDecimal b1 = new BigDecimal(str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.multiply(b2);
}
if (s.contains("/")) {
String[] str = s.split("\\/");
BigDecimal b1 = new BigDecimal(str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.divide(b2, 10, BigDecimal.ROUND_HALF_UP);
}
if (s.contains("+")) {
String[] str = s.split("\\+");
BigDecimal b1 = new BigDecimal(str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.add(b2);
}
if (s.contains("-")) {
String[] str = s.split("\\-");
BigDecimal b1 = new BigDecimal(str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.subtract(b2);
}
} else if (s.matches(ss)) {
s = s.substring(1);
if (s.contains("*")) {
String[] str = s.split("\\*");
BigDecimal b1 = new BigDecimal("-" + str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.multiply(b2);
}
if (s.contains("/")) {
String[] str = s.split("\\/");
BigDecimal b1 = new BigDecimal("-" + str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.divide(b2, 10, BigDecimal.ROUND_HALF_UP);
}
if (s.contains("+")) {
String[] str = s.split("\\+");
BigDecimal b1 = new BigDecimal("-" + str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.add(b2);
}
if (s.contains("-")) {
String[] str = s.split("\\-");
BigDecimal b1 = new BigDecimal("-" + str[0]);
BigDecimal b2 = new BigDecimal(str[1]);
a = b1.subtract(b2);
}
} else {
return "ERROR";
}
return a.toString();
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起