java计算器类方法_用java编写计算器,类,方法,属性的说明

展开全部

^简易的计算器

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();

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值