Java 计算器实现

简介:

1、加减乘除操作

public  class  MyMath {
     public  static  final  int  DEFAULT_SCALE = 20 ;
     
     public  static  double  add( double  num1, double  num2) {
         
         BigDecimal first = getBigDecimal(num1);
         BigDecimal second = getBigDecimal(num2);
         return  first.add(second).doubleValue();
     }
     
     public  static  double  substract( double  num1, double  num2) {
         
         BigDecimal first = getBigDecimal(num1);
         BigDecimal second = getBigDecimal(num2);
         return  first.subtract(second).doubleValue();
     }
     
     public  static  double  multiply( double  num1, double  num2) {
         
         BigDecimal first = getBigDecimal(num1);
         BigDecimal second = getBigDecimal(num2);
         return  first.multiply(second).doubleValue();
     }
     
     public  static  double  divide( double  num1, double  num2) {
         
         BigDecimal first = getBigDecimal(num1);
         BigDecimal second = getBigDecimal(num2);
         return  first.divide(second,DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
     }
     
     private  static  BigDecimal getBigDecimal( double  num)
     {
         return  new  BigDecimal(num);
     }
 
}

 

2、UI

public  class  CalFrame extends  JFrame {
 
     /**
      *
      */
     private  static  final  long  serialVersionUID = 1L;
     private  TextField textField;
     private  final  int  Frame_Width = 360 ;
     private  final  int  Frame_Height = 250 ;
     private  CalService service = new  CalService();
     
     private  String[] mOpStrings = { "MC" , "MR" , "MS" , "M+" };
     private  String[] nOp = { "7" , "8" , "9" , "/" , "sqrt" , "4" , "5" , "6" , "*" ,
             "%" , "1" , "2" , "3" , "-" , "1/x" , "0" , "+/-" , "." , "+" , "="  };
     private  String[] rOp = { "Back" , "CE" , "C"  };
     
     public  CalFrame(){
         super ();
         initialize();
     }
 
     private   void  initialize() {
         this .setTitle( "我的计算器" );
         this .setResizable( false );
         JPanel panel = new  JPanel();
         
         textField = new  TextField( "0" );
         textField.setBackground(Color.white);
         textField.setEditable( false );
         
         panel.setLayout( new  BorderLayout( 10 , 1 ));
         panel.add(textField, BorderLayout.NORTH);
         
         panel.setPreferredSize( new  DimensionUIResource(Frame_Width, Frame_Height));
         
         JPanel panel1 = new  JPanel();
         panel1.setLayout( new  GridLayout( 5 , 1 , 0 , 5 ));
         JButton[] mButton = new  JButton[mOpStrings.length];
         for  ( int  i = 0 ; i < this .mOpStrings.length; i++) {
             JButton b = new  JButton( this .mOpStrings[i]);
             b.addActionListener( new  ActionListener() {
                 
                 @Override
                 public  void  actionPerformed(ActionEvent arg0) {
                     // TODO Auto-generated method stub
                     String cmdString = arg0.getActionCommand();
                     String result = null ;
                     
                     try  {
                           result = service.callMethod(cmdString, textField.getText());
                     } catch  (Exception e) {
                         // TODO: handle exception
                         System.out.println(e.getMessage());
                     }
                     
                     if  (cmdString.indexOf( "MC" ) == 0 ) {
                         
                     }
                     
                     if  (result != null ) {
                         textField.setText(result);
                     }
                     
                 }
             });
             b.setBackground(Color.red);
             mButton[i] = b;
         }  
     
 
         for  (JButton b : mButton) {
             panel1.add(b);
         }
         
         /////////////////////////////////////////////////////////
         Panel panel2 = new  Panel();
         panel2.setLayout( new  BorderLayout( 1 , 5 ));
         
         Panel panel21 = new  Panel();   
         JButton[] result = new  JButton[rOp.length];
         for  ( int  i = 0 ; i < this .rOp.length; i++) {
             JButton b = new  JButton( this .rOp[i]);
             b.addActionListener( new  ActionListener() {
                 
                 @Override
                 public  void  actionPerformed(ActionEvent arg0) {
                     // TODO Auto-generated method stub
                     
                 }
             });
             b.setBackground(Color.red);
             result[i] = b;
         }
         
         panel21.setLayout( new  GridLayout( 1 , 3 , 3 , 3 ));
         for  (JButton button : result) {
             panel21.add(button);
         }
         
         Panel panel22 = new  Panel();
         panel22.setLayout( new  GridLayout( 4 , 5 , 3 , 5 ));
         JButton[] result2 = new  JButton[nOp.length];
         String[] redButton = { "/" , "*" , "-" , "+" , "="  };
         for  ( int  i = 0 ; i < this .nOp.length; i++) {
             JButton b2 = new  JButton( this .nOp[i]);
             b2.addActionListener( new  ActionListener() {
                 
                 @Override
                 public  void  actionPerformed(ActionEvent arg0) {
                     // TODO Auto-generated method stub
                     
                 }
             });
             
             Arrays.sort(redButton);
             if  (Arrays.binarySearch(redButton, nOp[i]) >= 0 ) {
                 b2.setForeground(Color.red);
             }
             else  {
                 b2.setForeground(Color.blue);
             }
             result2[i] = b2;
         }
         for  (JButton button : result2) {
             panel22.add(button);
         }
         
         
         
         panel2.add(panel21, BorderLayout.NORTH);
         panel2.add(panel22,BorderLayout.CENTER);
         
         panel.add(panel1, BorderLayout.WEST);
         panel.add(panel2, BorderLayout.CENTER);
 
         this .add(panel);
     }
}

  

3、业务类

public  class  CalService {
     private  String firstNum = null ;
     private  String secondNum = null ;
     private  String numString = "0123456789." ;
     private  String opString = "+-*/" ;
     
     public  String callMethod(String cmd, String text) throws  Exception  {
         if  (cmd.equals( "C" )) {
             return  clearAll();
         } else  if  (cmd.equals( "CE" )) {
             return  clear(text);
         } else  if  (cmd.equals( "Back" )) {
             return  backSpace(text);
         } else  if  (numString.indexOf(cmd) != - 1 ) {
             return  catNum(cmd, text);
         } else  if  (opString.indexOf(cmd) != - 1 ) {
             return  setOp(cmd, text);
         } else  if  (cmd.equals( "=" )) {
             return  cal(text, false );
         } else  if  (cmd.equals( "+/-" )) {
             return  setNegative(text);
         } else  if  (cmd.equals( "1/x" )) {
             return  setReciprocal(text);
         } else  if  (cmd.equals( "sqrt" )) {
             return  sqrt(text);
         } else  if  (cmd.equals( "%" )) {
             return  cal(text, true );
         } else  {
             return  mCmd(cmd, text);
         }
     }
 
     private  String backSpace(String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String catNum(String cmd, String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String setOp(String cmd, String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String setNegative(String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String setReciprocal(String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String sqrt(String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String cal(String text, boolean  b) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String mCmd(String cmd, String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String clear(String text) {
         // TODO Auto-generated method stub
         return  null ;
     }
 
     private  String clearAll() {
         // TODO Auto-generated method stub
         this .firstNum = "0" ;
         this .secondNum = null ;
         return  this .firstNum;
     }
}

  

4、Main类

public  static  void  main(String[] args) {
     // TODO Auto-generated method stub
     
     CalFrame frame = new  CalFrame();
     frame.pack();
     frame.setVisible( true );
     frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/12/14/2818257.html,如需转载请自行联系原作者

目录
相关文章
|
6月前
|
前端开发 JavaScript Java
Java中规模软件开发实训——简单计算器制作
​ ✨博主:命运之光 🌸专栏:Python星辰秘典 🐳专栏:web开发(html css js) ❤️专栏:Java经典程序设计 ☀️博主的其他文章:点击进入博主的主页
77 0
|
5月前
|
Java
Java实现计算器demo
Java实现计算器demo
|
5月前
|
存储 Java
Java设计一个简单的计算器程序
Java设计一个简单的计算器程序
|
3月前
|
Java
用Java编写一个代码实现计算器功能
【1月更文挑战第17天】用Java编写一个代码实现计算器功能
24 0
|
3月前
|
Python Java Go
Java每日一练(20230416) 三数之和、基本计算器、通配符匹配
Java每日一练(20230416) 三数之和、基本计算器、通配符匹配
29 0
Java每日一练(20230416) 三数之和、基本计算器、通配符匹配
|
3月前
|
Python Java 算法
Java每日一练(20230415) 扰乱字符串、单词拆分、模拟计算器
Java每日一练(20230415) 扰乱字符串、单词拆分、模拟计算器
27 0
Java每日一练(20230415) 扰乱字符串、单词拆分、模拟计算器
|
3月前
|
Java 程序员
java实现简单计算器(二)
java实现简单计算器(二)
|
3月前
|
Java
java实现简单计算器
java实现简单计算器
|
3月前
|
Java
计算器Java版(小玩具)
计算器Java版(小玩具)
16 0
|
5月前
|
Java UED
Java设计一个简单的能保留计算过程的计算器
Java设计一个简单的能保留计算过程的计算器