• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            coreBugZJ

            此 blog 已棄。

            計算器 Java實現(xiàn)

            偶然從郵箱里發(fā)現(xiàn)的古董。。。




            我的代碼:

              1 // windows
              2 
              3 
              4 /*
              5 版本 1.0
              6 
              7 顯示一行數(shù)字
              8 簡單二元運算
              9 數(shù)字為 double 型
             10 無輸入數(shù)字位數(shù)限制
             11 可輸入過多的小數(shù)點
             12 
             13 界面:
             14 7 8 9 + div
             15 4 5 6 - mod
             16 1 2 3 * pow
             17 0 . = / ac
             18 */
             19 
             20 import java.awt.*;
             21 import java.awt.event.*;
             22 
             23 public class Calculator extends Frame implements ActionListener {
             24 
             25         public static void main(String[] args) {
             26                 new Calculator();
             27         }
             28 
             29         public Calculator() {
             30                 super"計算器 -- 趙杰" );
             31 
             32                 // keyBoard and key
             33                 key = new Button[ 300 ];
             34                 Panel kb = new Panel();
             35                 kb.setLayout( new GridLayout( 4533 ) );
             36                 int i;
             37 
             38                 key[ i = (int)'7' ] = new Button( "7" );
             39                 key[ i ].addActionListener( this );
             40                 kb.add( key[ i ] );
             41 
             42                 key[ i = (int)'8' ] = new Button( "8" );
             43                 key[ i ].addActionListener( this );
             44                 kb.add( key[ i ] );
             45 
             46                 key[ i = (int)'9' ] = new Button( "9" );
             47                 key[ i ].addActionListener( this );
             48                 kb.add( key[ i ] );
             49 
             50                 key[ i = (int)'+' ] = new Button( "+" );
             51                 key[ i ].addActionListener( this );
             52                 kb.add( key[ i ] );
             53 
             54                 key[ i = (int)'d' ] = new Button( "div" );
             55                 key[ i ].addActionListener( this );
             56                 kb.add( key[ i ] );
             57 
             58                 key[ i = (int)'4' ] = new Button( "4" );
             59                 key[ i ].addActionListener( this );
             60                 kb.add( key[ i ] );
             61 
             62                 key[ i = (int)'5' ] = new Button( "5" );
             63                 key[ i ].addActionListener( this );
             64                 kb.add( key[ i ] );
             65 
             66                 key[ i = (int)'6' ] = new Button( "6" );
             67                 key[ i ].addActionListener( this );
             68                 kb.add( key[ i ] );
             69 
             70                 key[ i = (int)'-' ] = new Button( "-" );
             71                 key[ i ].addActionListener( this );
             72                 kb.add( key[ i ] );
             73 
             74                 key[ i = (int)'m' ] = new Button( "mod" );
             75                 key[ i ].addActionListener( this );
             76                 kb.add( key[ i ] );
             77 
             78                 key[ i = (int)'1' ] = new Button( "1" );
             79                 key[ i ].addActionListener( this );
             80                 kb.add( key[ i ] );
             81 
             82                 key[ i = (int)'2' ] = new Button( "2" );
             83                 key[ i ].addActionListener( this );
             84                 kb.add( key[ i ] );
             85 
             86                 key[ i = (int)'3' ] = new Button( "3" );
             87                 key[ i ].addActionListener( this );
             88                 kb.add( key[ i ] );
             89 
             90                 key[ i = (int)'*' ] = new Button( "*" );
             91                 key[ i ].addActionListener( this );
             92                 kb.add( key[ i ] );
             93 
             94                 key[ i = (int)'p' ] = new Button( "pow" );
             95                 key[ i ].addActionListener( this );
             96                 kb.add( key[ i ] );
             97 
             98                 key[ i = (int)'0' ] = new Button( "0" );
             99                 key[ i ].addActionListener( this );
            100                 kb.add( key[ i ] );
            101 
            102                 key[ i = (int)'.' ] = new Button( "." );
            103                 key[ i ].addActionListener( this );
            104                 kb.add( key[ i ] );
            105 
            106                 key[ i = (int)'=' ] = new Button( "=" );
            107                 key[ i ].addActionListener( this );
            108                 kb.add( key[ i ] );
            109 
            110                 key[ i = (int)'/' ] = new Button( "/" );
            111                 key[ i ].addActionListener( this );
            112                 kb.add( key[ i ] );
            113 
            114                 key[ i = (int)'a' ] = new Button( "ac" );
            115                 key[ i ].addActionListener( this );
            116                 kb.add( key[ i ] );
            117 
            118                 this.add( kb );
            119 
            120                 // window
            121                 this.addWindowListener( new WindowAdapter() {
            122                         public void windowClosing( WindowEvent e ) {
            123                                 System.exit( 0 );
            124                         }
            125                 } );
            126                 this.setBounds( 200200300210 );
            127                 this.setLayout( new BorderLayout() );
            128                 dis = new TextArea( ""120, TextArea.SCROLLBARS_NONE );
            129                 dis.setEditable( false );
            130                 this.add( dis, BorderLayout.NORTH );
            131                 this.add( kb, BorderLayout.CENTER );
            132                 this.setVisible( true );
            133                 this.validate();
            134 
            135                 // data
            136                 bError   = false;
            137                 totInput = 0;
            138                 lastAns  = false;
            139         }
            140 
            141         public void actionPerformed( ActionEvent e ) {
            142                 Object ob = e.getSource();
            143                 if ( bError && ( ob != key[ (int)'a' ] ) ) {
            144                         return;
            145                 }
            146                 for ( char ch = '0'; ch <= '9'++ch ) {
            147                         if ( ob == key[ (int)ch ] ) {
            148                                 if ( ( totInput == 0 ) || ( totInput == 2 ) ) {
            149                                         dis.setText( "" );
            150                                         ++totInput;
            151                                 }
            152                                 if ( lastAns ) {
            153                                         dis.setText( "" );
            154                                         lastAns = false;
            155                                         totInput = 1;
            156                                 }
            157                                 dis.append( new Character( ch ).toString() );
            158                                 this.validate();
            159                                 return;
            160                         }
            161                 }
            162                 if ( ob == key[ (int)'.' ] ) {
            163                         if ( ( totInput == 0 ) || ( totInput == 2 ) ) {
            164                                 return;
            165                         }
            166                         dis.append( "." );
            167                         this.validate();
            168                         return;
            169                 }
            170                 if ( ob == key[ (int)'a' ] ) {
            171                         dis.setText( "" );
            172                         totInput = 0;
            173                         bError   = false;
            174                         lastAns  = false;
            175                         this.validate();
            176                         return;
            177                 }
            178                 lastAns = false;
            179                 if ( totInput == 0 ) {
            180                         return;
            181                 }
            182                 if ( ( totInput != 3 ) && ( ob == key[ (int)'=' ] ) ) {
            183                         return;
            184                 }
            185                 if ( ( totInput == 1 ) || ( totInput == 3 ) ) {
            186                         if ( cantStrToNum( totInput == 1 ) ) {
            187                                 dis.setText( "Error" );
            188                                 bError = true;
            189                                 this.validate();
            190                                 return;
            191                         }
            192                         if ( totInput == 3 ) {
            193                                 if ( cantCalcAns() ) {
            194                                         dis.setText( "Error" );
            195                                         bError = true;
            196                                         this.validate();
            197                                         return;
            198                                 }
            199                                 dis.setText( new Double( ans ).toString() );
            200                                 totInput = 1;
            201                                 this.validate();
            202                                 lastAns = true;
            203                                 return;
            204                         }
            205                         ++totInput;
            206                 }
            207                 if ( ob == key[ (int)'+' ] ) {
            208                         op = '+';
            209                         dis.setText( "+" );
            210                         this.validate();
            211                         return;
            212                 }
            213                 if ( ob == key[ (int)'d' ] ) {
            214                         op = 'd';
            215                         dis.setText( "div" );
            216                         this.validate();
            217                         return;
            218                 }
            219                 if ( ob == key[ (int)'-' ] ) {
            220                         op = '-';
            221                         dis.setText( "-" );
            222                         this.validate();
            223                         return;
            224                 }
            225                 if ( ob == key[ (int)'m' ] ) {
            226                         op = 'm';
            227                         dis.setText( "mod" );
            228                         this.validate();
            229                         return;
            230                 }
            231                 if ( ob == key[ (int)'*' ] ) {
            232                         op = '*';
            233                         dis.setText( "*" );
            234                         this.validate();
            235                         return;
            236                 }
            237                 if ( ob == key[ (int)'p' ] ) {
            238                         op = 'p';
            239                         dis.setText( "pow" );
            240                         this.validate();
            241                         return;
            242                 }
            243                 if ( ob == key[ (int)'/' ] ) {
            244                         op = '/';
            245                         dis.setText( "/" );
            246                         this.validate();
            247                         return;
            248                 }
            249         }
            250 
            251         private boolean cantCalcAns() {
            252                 switch ( op ) {
            253                         case '+' : ans = first + second; break;
            254                         case '-' : ans = first - second; break;
            255                         case '*' : ans = first * second; break;
            256                         case '/' : 
            257                                 if ( second == 0 ) {
            258                                         return true;
            259                                 }
            260                                 ans = first / second; break;
            261                         case 'd' : 
            262                                 if ( second == 0 ) {
            263                                         return true;
            264                                 }
            265                                 ans = ((int)first) / ((int)second); break;
            266                         case 'm' : 
            267                                 if ( second == 0 ) {
            268                                         return true;
            269                                 }
            270                                 ans = ((int)first) % ((int)second); break;
            271                         case 'p' : ans = Math.pow( first, second ); break;
            272                 }
            273                 return false;
            274         }
            275 
            276         private boolean cantStrToNum( boolean toFirst ) {
            277                 try {
            278                         if ( toFirst ) {
            279                                 first = Double.parseDouble( dis.getText() );
            280                         }
            281                         else {
            282                                 second = Double.parseDouble( dis.getText() );
            283                         }
            284                 }
            285                 catch ( Exception ex ) {
            286                         return true;
            287                 }
            288                 return false;
            289         }
            290 
            291         private TextArea  dis;
            292         private Button[]  key;
            293         private double    first, second, ans;
            294         private char      op;
            295         private int       totInput;
            296         private boolean   bError, lastAns;
            297 //        private final int MAX_NUM_LEN = 13;
            298 
            299 }
            300 

             

            posted on 2011-06-09 20:00 coreBugZJ 閱讀(1592) 評論(1)  編輯 收藏 引用 所屬分類: ProgrammingLanguage

            Feedback

            # re: 計算器 Java實現(xiàn) 2011-06-10 16:30 Skill

            佩服樓主的敬業(yè)精神  回復(fù)  更多評論   


            久久久这里有精品中文字幕| 久久精品国产精品亚洲艾草网美妙 | 久久91亚洲人成电影网站| 久久人人爽人人爽人人爽| 亚洲欧美国产日韩综合久久| 狠狠色伊人久久精品综合网| 久久综合狠狠色综合伊人| 久久久精品午夜免费不卡| 久久精品www人人爽人人| 久久婷婷五月综合97色一本一本 | 91精品国产9l久久久久| 久久超碰97人人做人人爱| 久久Av无码精品人妻系列| 精品久久8x国产免费观看| 国产精品久久亚洲不卡动漫| 国产高潮国产高潮久久久91 | 狠色狠色狠狠色综合久久| 久久精品中文无码资源站| 精品久久久久久久无码| 久久久久综合网久久| 亚洲综合精品香蕉久久网97| 久久久久国色AV免费看图片| 香蕉久久AⅤ一区二区三区| 中文成人久久久久影院免费观看| 偷窥少妇久久久久久久久| 亚洲AV无码久久| 久久精品视频网| 性做久久久久久久久久久| 亚洲午夜久久久久久久久久| 国产精品久久99| 蜜臀久久99精品久久久久久| 久久综合给久久狠狠97色| 久久精品国产99国产电影网| 久久久久久av无码免费看大片| 亚洲国产精品无码久久青草| 久久九九精品99国产精品| 精品久久综合1区2区3区激情| 2021国内精品久久久久久影院| 777米奇久久最新地址| 久久久久女教师免费一区| 久久综合综合久久综合|