• <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實現

            偶然從郵箱里發現的古董。。。




            我的代碼:

              1 // windows
              2 
              3 
              4 /*
              5 版本 1.0
              6 
              7 顯示一行數字
              8 簡單二元運算
              9 數字為 double 型
             10 無輸入數字位數限制
             11 可輸入過多的小數點
             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實現 2011-06-10 16:30 Skill

            佩服樓主的敬業精神  回復  更多評論   


            欧美午夜A∨大片久久| 伊人久久综在合线亚洲2019| 久久天堂AV综合合色蜜桃网| 久久热这里只有精品在线观看| 久久精品亚洲福利| 久久免费视频观看| 久久精品无码一区二区日韩AV| 99久久99久久久精品齐齐| 久久久国产精品福利免费| 久久男人Av资源网站无码软件 | 日本精品一区二区久久久| 国内精品伊人久久久久| 国产成人综合久久久久久| 伊人久久大香线蕉成人| 久久久久久精品免费免费自慰| 久久青青色综合| 18禁黄久久久AAA片| 国产精品久久99| 久久91亚洲人成电影网站| 国产亚州精品女人久久久久久| 狠狠综合久久综合88亚洲| 久久综合久久综合久久| 精品国产福利久久久| 99热热久久这里只有精品68| 久久有码中文字幕| 99久久无色码中文字幕 | 久久人人爽爽爽人久久久| 国产亚洲欧美精品久久久| 亚洲精品久久久www| 国产精品久久影院| 久久久久亚洲AV成人网人人软件 | 蜜臀久久99精品久久久久久| 精品久久久久久无码不卡| 亚洲狠狠婷婷综合久久久久| 久久久久久国产精品无码下载| 伊人久久大香线蕉成人| 久久er99热精品一区二区| 日韩影院久久| 久久精品国产69国产精品亚洲| 亚洲人成电影网站久久| 精品久久久久久成人AV|