- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class MainFrame extends JFrame {
- int flagk = 1;//标记左右括号
- int flage = 1;//表示”=“的状态,指示是否运算结束
- double res = 0;//存放运算结果
- JButton[] bt = new JButton[24];
- JTextField tfShow = new JTextField();
- public static void main(String[] args) {
- new MainFrame();
- }
- public MainFrame() {
- JFrame frame = new JFrame("计算器");
- Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
- frame.setSize(500,250);
- frame.setLocation((int) scrSize.getWidth() / 3, (int) scrSize
- .getHeight() / 3);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.setResizable(false);
- tfShow.setHorizontalAlignment(JTextField.RIGHT);
- tfShow.setFont(new Font("TimesRoman", Font.PLAIN, 20));
- tfShow.setBackground(Color.WHITE);
- tfShow.setEditable(false);
- tfShow.setText("");
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(4, 6, 10, 10));
- bt[0] = new JButton("1");
- bt[0].setForeground(Color.BLUE);
- bt[1] = new JButton("2");
- bt[1].setForeground(Color.BLUE);
- bt[2] = new JButton("3");
- bt[2].setForeground(Color.BLUE);
- bt[6] = new JButton("4");
- bt[6].setForeground(Color.BLUE);
- bt[7] = new JButton("5");
- bt[7].setForeground(Color.BLUE);
- bt[8] = new JButton("6");
- bt[8].setForeground(Color.BLUE);
- bt[12] = new JButton("7");
- bt[12].setForeground(Color.BLUE);
- bt[13] = new JButton("8");
- bt[13].setForeground(Color.BLUE);
- bt[14] = new JButton("9");
- bt[14].setForeground(Color.BLUE);
- bt[19] = new JButton("0");
- bt[19].setForeground(Color.BLUE);
- bt[18] = new JButton("+/-");
- bt[18].setForeground(Color.RED);
- bt[20] = new JButton(".");
- bt[20].setForeground(Color.RED);
- bt[3] = new JButton(" + ");
- bt[3].setForeground(Color.RED);
- bt[9] = new JButton(" - ");
- bt[9].setForeground(Color.RED);
- bt[15] = new JButton(" * ");
- bt[15].setForeground(Color.RED);
- bt[21] = new JButton(" / ");
- bt[21].setForeground(Color.RED);
- bt[10] = new JButton("sin");
- bt[10].setForeground(Color.RED);
- bt[16] = new JButton("cos");
- bt[16].setForeground(Color.RED);
- bt[5] = new JButton("C");
- bt[5].setForeground(Color.RED);
- bt[4] = new JButton("back");
- bt[4].setForeground(Color.RED);
- bt[22] = new JButton("1/x");
- bt[22].setForeground(Color.RED);
- bt[23] = new JButton(" = ");
- bt[23].setForeground(Color.RED);
- bt[11] = new JButton("( )");
- bt[11].setForeground(Color.RED);
- bt[17] = new JButton("√");
- bt[17].setForeground(Color.RED);
- for (int i = 0; i < 24; i++) {
- panel.add(bt[i]);
- bt[i].addActionListener(new MyListener());
- }
- panel.setBackground(Color.LIGHT_GRAY);
- frame.add(tfShow, BorderLayout.NORTH);
- frame.add(panel, BorderLayout.CENTER);
- frame.setVisible(true);
- }
- class MyListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if(flage == 0){
- tfShow.setText("");
- flage = 1 - flage;
- }
- if (e.getActionCommand().equals("C")) {
- tfShow.setText("");
- flagk = 1;
- flage = 1;
- } else if (e.getActionCommand().equals("back")) {
- String temp = tfShow.getText().trim();
- if (!temp.equals("")) {
- tfShow.setText(temp.substring(0, temp.length() - 1));
- }
- } else if (e.getActionCommand().equals("cos")) {
- tfShow.setText("" + Math.cos(Double.valueOf(tfShow.getText())/180*Math.PI));
- }
- else if (e.getActionCommand().equals("sin")) {
- tfShow.setText("" + Math.sin(Double.valueOf(tfShow.getText())/180*Math.PI));
- }
- else if (e.getActionCommand().equals("1/x")) {
- tfShow.setText("" + 1 / Double.valueOf(tfShow.getText()));
- }
- else if (e.getActionCommand().equals("+/-")) {
- tfShow.setText("" + 1 / Double.valueOf(tfShow.getText()));
- }
- else if (e.getActionCommand().equals("√")) {
- tfShow.setText("" + Math.sqrt(Double.valueOf(tfShow.getText())));
- }
- else if (e.getActionCommand().equals("( )")) {
- if (flagk == 1) {
- tfShow.setText(tfShow.getText() + "( ");
- flagk = 1 - flagk;
- } else {
- tfShow.setText(tfShow.getText() + " )");
- flagk = 1 - flagk;
- }
- } else if (e.getActionCommand().equals(" = ")) {
- String str = tfShow.getText();
- if (!str.equals("") && flagk == 1) {// 文本框不为空
- res = Arithmetic.arithmetic(str);
- tfShow.setText(tfShow.getText() + e.getActionCommand() + res);
- }
- flage = 1 - flage;
- } else {
- tfShow.setText(tfShow.getText() + e.getActionCommand());
- }
- }
- }
- }
- import java.math.BigDecimal;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Arithmetic {
- public static double arithmetic(String exp){
- String result = parseExp(exp).replaceAll("[\\[\\]]", "");
- return Double.parseDouble(result);
- }
- public static String parseExp(String expression){
- expression=expression.replaceAll("\\s+", "").replaceAll("^\\((.+)\\)$", "$1");
- String minExp="^((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))$";
- //最小表达式计算
- if(expression.matches(minExp)){
- String result=calculate(expression);
- return Double.parseDouble(result)>=0?result:"["+result+"]";
- }
- //计算不带括号的四则运算
- String noParentheses="^[^\\(\\)]+$";
- String priorOperatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";
- String operatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";
- if(expression.matches(noParentheses)){
- Pattern patt=Pattern.compile(priorOperatorExp);
- Matcher mat=patt.matcher(expression);
- if(mat.find()){
- String tempMinExp=mat.group();
- expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp));
- }else{
- patt=Pattern.compile(operatorExp);
- mat=patt.matcher(expression);
- if(mat.find()){
- String tempMinExp=mat.group();
- expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp));
- }
- }
- return parseExp(expression);
- }
- //计算带括号的四则运算
- String minParentheses="\\([^\\(\\)]+\\)";
- Pattern patt=Pattern.compile(minParentheses);
- Matcher mat=patt.matcher(expression);
- if(mat.find()){
- String tempMinExp=mat.group();
- expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp));
- }
- return parseExp(expression);
- }
- /**
- * 计算最小单位四则运算表达式(两个数字)
- * @param exp
- * @return
- */
- public static String calculate(String exp){
- exp=exp.replaceAll("[\\[\\]]", "");
- String number[]=exp.replaceFirst("(\\d)[\\+\\-\\*\\/]", "$1,").split(",");
- BigDecimal number1=new BigDecimal(number[0]);
- BigDecimal number2=new BigDecimal(number[1]);
- BigDecimal result=null;
- String operator=exp.replaceFirst("^.*\\d([\\+\\-\\*\\/]).+$", "$1");
- if("+".equals(operator)){
- result=number1.add(number2);
- }else if("-".equals(operator)){
- result=number1.subtract(number2);
- }else if("*".equals(operator)){
- result=number1.multiply(number2);
- }else if("/".equals(operator)){
- result=number1.divide(number2);
- }
- return result!=null?result.toString():null;
- }
- }