1. import java.awt.*;  
  2. import java.awt.event.ActionEvent;  
  3. import java.awt.event.ActionListener;  
  4.  
  5. import javax.swing.*;  
  6.  
  7. public class MainFrame extends JFrame {  
  8.  
  9.     int flagk = 1;//标记左右括号  
  10.     int flage = 1;//表示”=“的状态,指示是否运算结束  
  11.     double res = 0;//存放运算结果  
  12.     JButton[] bt = new JButton[24];  
  13.     JTextField tfShow = new JTextField();  
  14.  
  15.     public static void main(String[] args) {  
  16.         new MainFrame();  
  17.     }  
  18.  
  19.     public MainFrame() {  
  20.         JFrame frame = new JFrame("计算器");  
  21.  
  22.         Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小  
  23.  
  24.         frame.setSize(500,250);  
  25.         frame.setLocation((int) scrSize.getWidth() / 3, (int) scrSize  
  26.                 .getHeight() / 3);  
  27.  
  28.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  29.         frame.setLayout(new BorderLayout());  
  30.         frame.setResizable(false);  
  31.  
  32.         tfShow.setHorizontalAlignment(JTextField.RIGHT);  
  33.         tfShow.setFont(new Font("TimesRoman", Font.PLAIN, 20));  
  34.         tfShow.setBackground(Color.WHITE);  
  35.         tfShow.setEditable(false);  
  36.         tfShow.setText("");  
  37.  
  38.         JPanel panel = new JPanel();  
  39.         panel.setLayout(new GridLayout(461010));  
  40.  
  41.         bt[0] = new JButton("1");  
  42.         bt[0].setForeground(Color.BLUE);  
  43.         bt[1] = new JButton("2");  
  44.         bt[1].setForeground(Color.BLUE);  
  45.         bt[2] = new JButton("3");  
  46.         bt[2].setForeground(Color.BLUE);  
  47.         bt[6] = new JButton("4");  
  48.         bt[6].setForeground(Color.BLUE);  
  49.         bt[7] = new JButton("5");  
  50.         bt[7].setForeground(Color.BLUE);  
  51.         bt[8] = new JButton("6");  
  52.         bt[8].setForeground(Color.BLUE);  
  53.         bt[12] = new JButton("7");  
  54.         bt[12].setForeground(Color.BLUE);  
  55.         bt[13] = new JButton("8");  
  56.         bt[13].setForeground(Color.BLUE);  
  57.         bt[14] = new JButton("9");  
  58.         bt[14].setForeground(Color.BLUE);  
  59.         bt[19] = new JButton("0");  
  60.         bt[19].setForeground(Color.BLUE);  
  61.         bt[18] = new JButton("+/-");  
  62.         bt[18].setForeground(Color.RED);  
  63.         bt[20] = new JButton(".");  
  64.         bt[20].setForeground(Color.RED);  
  65.         bt[3] = new JButton(" + ");  
  66.         bt[3].setForeground(Color.RED);  
  67.         bt[9] = new JButton(" - ");  
  68.         bt[9].setForeground(Color.RED);  
  69.         bt[15] = new JButton(" * ");  
  70.         bt[15].setForeground(Color.RED);  
  71.         bt[21] = new JButton(" / ");  
  72.         bt[21].setForeground(Color.RED);  
  73.         bt[10] = new JButton("sin");  
  74.         bt[10].setForeground(Color.RED);  
  75.         bt[16] = new JButton("cos");  
  76.         bt[16].setForeground(Color.RED);  
  77.         bt[5] = new JButton("C");  
  78.         bt[5].setForeground(Color.RED);  
  79.         bt[4] = new JButton("back");  
  80.         bt[4].setForeground(Color.RED);  
  81.         bt[22] = new JButton("1/x");  
  82.         bt[22].setForeground(Color.RED);  
  83.         bt[23] = new JButton(" = ");  
  84.         bt[23].setForeground(Color.RED);  
  85.         bt[11] = new JButton("( )");  
  86.         bt[11].setForeground(Color.RED);  
  87.         bt[17] = new JButton("√");  
  88.         bt[17].setForeground(Color.RED);  
  89.  
  90.         for (int i = 0; i < 24; i++) {  
  91.             panel.add(bt[i]);  
  92.             bt[i].addActionListener(new MyListener());  
  93.         }  
  94.           
  95.         panel.setBackground(Color.LIGHT_GRAY);  
  96.         frame.add(tfShow, BorderLayout.NORTH);  
  97.         frame.add(panel, BorderLayout.CENTER);  
  98.         frame.setVisible(true);  
  99.     }  
  100.  
  101.     class MyListener implements ActionListener {  
  102.  
  103.         public void actionPerformed(ActionEvent e) {  
  104.               
  105.             if(flage == 0){  
  106.                 tfShow.setText("");  
  107.                 flage = 1 - flage;  
  108.             }  
  109.               
  110.             if (e.getActionCommand().equals("C")) {  
  111.                 tfShow.setText("");  
  112.                 flagk = 1;  
  113.                 flage = 1;  
  114.             } else if (e.getActionCommand().equals("back")) {  
  115.                 String temp = tfShow.getText().trim();  
  116.                 if (!temp.equals("")) {  
  117.                     tfShow.setText(temp.substring(0, temp.length() - 1));  
  118.                 }  
  119.             } else if (e.getActionCommand().equals("cos")) {  
  120.                 tfShow.setText("" + Math.cos(Double.valueOf(tfShow.getText())/180*Math.PI));  
  121.             }  
  122.              else if (e.getActionCommand().equals("sin")) {  
  123.                     tfShow.setText("" + Math.sin(Double.valueOf(tfShow.getText())/180*Math.PI));  
  124.                 }  
  125.             else if (e.getActionCommand().equals("1/x")) {  
  126.                 tfShow.setText("" + 1 / Double.valueOf(tfShow.getText()));  
  127.             }  
  128.             else if (e.getActionCommand().equals("+/-")) {  
  129.                 tfShow.setText("" + 1 / Double.valueOf(tfShow.getText()));  
  130.             }  
  131.             else if (e.getActionCommand().equals("√")) {  
  132.                 tfShow.setText("" + Math.sqrt(Double.valueOf(tfShow.getText())));  
  133.             }  
  134.             else if (e.getActionCommand().equals("( )")) {  
  135.  
  136.                 if (flagk == 1) {  
  137.                     tfShow.setText(tfShow.getText() + "( ");  
  138.                     flagk = 1 - flagk;  
  139.                 } else {  
  140.                     tfShow.setText(tfShow.getText() + " )");  
  141.                     flagk = 1 - flagk;  
  142.                 }  
  143.             } else if (e.getActionCommand().equals(" = ")) {  
  144.                 String str = tfShow.getText();  
  145.  
  146.                 if (!str.equals("") && flagk == 1) {// 文本框不为空  
  147.  
  148.                     res = Arithmetic.arithmetic(str);  
  149.                     tfShow.setText(tfShow.getText() + e.getActionCommand() + res);  
  150.                 }  
  151.                 flage = 1 - flage;  
  152.                   
  153.             } else {  
  154.                 tfShow.setText(tfShow.getText() + e.getActionCommand());  
  155.             }  
  156.         }  
  157.     }  
  158. }  
  1. import java.math.BigDecimal;  
  2. import java.util.regex.Matcher;  
  3. import java.util.regex.Pattern;  
  4.  
  5. public class Arithmetic {  
  6.  
  7.     public static double arithmetic(String exp){  
  8.         String result = parseExp(exp).replaceAll("[\\[\\]]""");  
  9.         return Double.parseDouble(result);  
  10.     }  
  11.  
  12.     public static String parseExp(String expression){  
  13.  
  14.         expression=expression.replaceAll("\\s+""").replaceAll("^\\((.+)\\)$""$1");  
  15.         String minExp="^((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))$";  
  16.         //最小表达式计算  
  17.         if(expression.matches(minExp)){  
  18.             String result=calculate(expression);  
  19.               
  20.             return Double.parseDouble(result)>=0?result:"["+result+"]";  
  21.         }  
  22.         //计算不带括号的四则运算  
  23.         String noParentheses="^[^\\(\\)]+$";  
  24.         String priorOperatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";  
  25.         String operatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";  
  26.         if(expression.matches(noParentheses)){  
  27.             Pattern patt=Pattern.compile(priorOperatorExp);  
  28.             Matcher mat=patt.matcher(expression);  
  29.             if(mat.find()){  
  30.                 String tempMinExp=mat.group();  
  31.                 expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp));  
  32.             }else{  
  33.                 patt=Pattern.compile(operatorExp);  
  34.                 mat=patt.matcher(expression);  
  35.                   
  36.                 if(mat.find()){  
  37.                     String tempMinExp=mat.group();  
  38.                     expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp));  
  39.                 }  
  40.             }  
  41.             return parseExp(expression);  
  42.         }  
  43.         //计算带括号的四则运算  
  44.         String minParentheses="\\([^\\(\\)]+\\)";  
  45.         Pattern patt=Pattern.compile(minParentheses);  
  46.         Matcher mat=patt.matcher(expression);  
  47.         if(mat.find()){  
  48.             String tempMinExp=mat.group();  
  49.             expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp));  
  50.         }  
  51.         return parseExp(expression);  
  52.     }  
  53.     /**  
  54.      * 计算最小单位四则运算表达式(两个数字)  
  55.      * @param exp  
  56.      * @return  
  57.      */ 
  58.     public static String calculate(String exp){  
  59.         exp=exp.replaceAll("[\\[\\]]""");  
  60.         String number[]=exp.replaceFirst("(\\d)[\\+\\-\\*\\/]""$1,").split(",");  
  61.         BigDecimal number1=new BigDecimal(number[0]);  
  62.         BigDecimal number2=new BigDecimal(number[1]);  
  63.         BigDecimal result=null;  
  64.           
  65.         String operator=exp.replaceFirst("^.*\\d([\\+\\-\\*\\/]).+$""$1");  
  66.         if("+".equals(operator)){  
  67.             result=number1.add(number2);  
  68.         }else if("-".equals(operator)){  
  69.             result=number1.subtract(number2);  
  70.         }else if("*".equals(operator)){  
  71.             result=number1.multiply(number2);  
  72.         }else if("/".equals(operator)){  
  73.             result=number1.divide(number2);  
  74.         }  
  75.           
  76.         return result!=null?result.toString():null;  
  77.     }  
  78. }