




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、用JAVA編寫計算器程序(模擬Windows計算器)import java.awt.*;import java.awt.event.*;1 / 15public class Calculation extends WindowAdapter implements ActionListener double dResult=0; double dNowInput=0; double dMemory; int n=0; /記載小數位數 int nOperation=1; / 記錄運算符類型 int nB
2、itsNum=0; /記錄總共輸入的位數 boolean alreadyHaveDot=false; /已經有小數點? boolean keyAvailable=true; boolean alreadyClickedEqueal=false; /是否按下過"="? boolean isTempNowInput=false; /是否在計算出結果后直接按運算符將結果賦給了當前輸入值? Frame f; Panel p1,p2,p3,p4,p5,p6; TextField tf1,tf
3、2; Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; Button bDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative; Button bBackspace,bCE,bC,bMR,bMS,bMC,bM; public void display() f=new Frame("計算器"); f.setSize(280,213); f.setLocation
4、(200,200); f.setBackground(Color.LIGHT_GRAY); f.setResizable(false); f.setLayout(new BorderLayout(3,3); p1=new Panel(new GridLayout(1,3,5,5); /用于存放backspace,ce,c三鍵 p2=new Panel(new GridLayout(4,5,5,5); /用于存放數字區及附近共20鍵, 此處間隙設置可能不合理,以后調整 p3=new P
5、anel(new GridLayout(5,1,5,5); /用于存放MC,MR,MS,M+鍵及顯示M狀態文本框,此處間隙設置可能不合理,以后調整 p4=new Panel(new FlowLayout(); /用于存放p1,p2 p5=new Panel(new FlowLayout(); p6=new Panel(new FlowLayout(); p4.add(p1); p4.add(p2); tf1=new TextField(35); /存
6、放顯示區 tf1.setText("0."); tf1.setEditable(false); p5.add(tf1); f.add(p5,BorderLayout.NORTH); f.add(p4,BorderLayout.CENTER); f.add(p3,BorderLayout.WEST); b1=new Button("1"); b2=new Button("2"); b3=new Button(&quo
7、t;3"); b4=new Button("4"); b5=new Button("5"); b6=new Button("6"); b7=new Button("7"); b8=new Button("8"); b9=new Button("9"); b0=new Button("0"); b1.addActionListener(th
8、is); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b0.addActionListe
9、ner(this); bDiv=new Button("/"); bSqrt=new Button("sqrt"); bMulti=new Button("*"); bMinus=new Button("-"); bPercent=new Button("%"); bPlus=new Button("+"); bReciprocal=new Button("1/x&qu
10、ot;); bEqual=new Button("="); bDot=new Button("."); bNegative=new Button("+/-"); bDiv.addActionListener(this); bSqrt.addActionListener(this); bMulti.addActionListener(this); bMinus.addActionListener(this); bPercent.
11、addActionListener(this); bPlus.addActionListener(this); bReciprocal.addActionListener(this); bEqual.addActionListener(this); bDot.addActionListener(this); bNegative.addActionListener(this); p2.add(b7); p2.add(b8); p2.add(b9); p2.add(bDiv);
12、 p2.add(bSqrt); p2.add(b4); p2.add(b5); p2.add(b6); p2.add(bMulti); p2.add(bPercent); p2.add(b1); p2.add(b2); p2.add(b3); p2.add(bMinus); p2.add(bReciprocal); p2.add(b0); p2.add(bNegative); p2.add(bDot); p2.add
13、(bPlus); p2.add(bEqual); bBackspace=new Button("Backspace"); bCE=new Button("CE"); bC=new Button("C"); bBackspace.addActionListener(this); bCE.addActionListener(this); bC.addActionListener(this); p1.ad
14、d(bBackspace); p1.add(bCE); p1.add(bC); tf2=new TextField(2); tf2.setEnabled(false); tf2.setBackground(Color.LIGHT_GRAY); bMC=new Button("MC"); bMR=new Button("MR"); bMS=new Button("MS"); bM=new Button("
15、M+"); bMC.addActionListener(this); bMR.addActionListener(this); bMS.addActionListener(this); bM.addActionListener(this); p6.add(tf2); p3.add(p6); p3.add(bMC); p3.add(bMR); p3.add(bMS); p3.add(bM); f.setVisibl
16、e(true); f.addWindowListener(this); public void actionPerformed(ActionEvent e) /key 0 to 9 if(this.keyAvailable && e.getActionCommand().length()=1 && e.getActionCommand().compareTo("0")>=0 && e.getActionCommand().compare
17、To("9")<=0) if(this.isTempNowInput) this.dNowInput=0; this.isTempNowInput=false; this.nBitsNum+; if(this.alreadyHaveDot=false) this.dNowInput=this.dNowInput*1
18、0+Double.parseDouble(e.getActionCommand(); else double temp=Double.parseDouble(e.getActionCommand(); for(int i=this.n;i<0;i+) temp*=0.1; &
19、#160; this.dNowInput+=temp; this.n-; this.tf1.setText(Double.toString(this.dNowInput); / key dot if(this.keyAvailable && e.getActionCommand()=".") if(this.alreadyHaveDot=false)
20、 this.nBitsNum+; this.alreadyHaveDot=true; this.n=-1; /key "+","-","*","/" if(this.keyAvailable && e.getActionCommand()="+" | e.getActionCommand()="-" | e.ge
21、tActionCommand()="*" | e.getActionCommand()="/") if(this.alreadyClickedEqueal) this.dNowInput=this.dResult; this.isTempNowInput=true; else switch(this.nOper
22、ation) case 1: this.dResult+=this.dNowInput; break; case 2: this.dResult-=this.dNowInput; break; case 3: this.dResult*=this.dNowInput; break; case 4:
23、; if(this.dNowInput=0) tf1.setText("除數不能為零"); this.keyAvailable=false; else this.dResult=this.dResult/this.d
24、NowInput; if(this.keyAvailable)tf1.setText(Double.toString(this.dResult); this.dNowInput=0; if(e.getActionCommand()="+") this.nOperation=1;
25、 if(e.getActionCommand()="-") this.nOperation=2; if(e.getActionCommand()="*") this.nOperation=3; if(e.getActionCommand()="/")
26、160; this.nOperation=4; this.nBitsNum=0; this.alreadyClickedEqueal=false; / key "+/-" if(this.keyAvailable && e.getActionCommand()="+/-") this.dNowInput=0-this.dNowInput; &
27、#160; tf1.setText(Double.toString(this.dNowInput); / key "C" if(e.getActionCommand()="C") this.nBitsNum=0; this.dResult=0; this.dNowInput=0; this.alreadyHaveDot=false; this.n=0;
28、60; this.nOperation=1; this.keyAvailable=true; this.alreadyClickedEqueal=false; tf1.setText("0."); / key "CE" if(e.getActionCommand()="CE") this.nBitsNum=0; this.dNowInput=0;
29、0; this.alreadyHaveDot=false; this.n=0; this.nOperation=1; this.keyAvailable=true; tf1.setText("0."); / key "sqrt" if(this.keyAvailable && e.getActionCommand()="sqrt")
30、160; if(this.alreadyClickedEqueal) if(this.dResult>=0) this.dResult=Math.sqrt(this.dResult); tf1.setText(Double.toString(this.dResult); else
31、60; tf1.setText("函數輸入無效"); this.keyAvailable=false; else if(this.dNowInput>=0) this.dNowInput=Math.sqrt(this.dNowInput); &
32、#160; tf1.setText(Double.toString(this.dNowInput); else tf1.setText("函數輸入無效"); this.keyAvailable=false; / key "1/x"
33、0; if(this.keyAvailable && e.getActionCommand()="1/x") if(this.dNowInput=0) tf1.setText("除數不能為零"); this.keyAvailable=false; else this.dNowInput=1/th
34、is.dNowInput; tf1.setText(Double.toString(this.dNowInput); / key "=" if(this.keyAvailable && e.getActionCommand()="=") this.alreadyClickedEqueal=true; switch(this.nOperation)
35、0; case 1: this.dResult+=this.dNowInput; break; case 2: this.dResult-=this.dNowInput; break; case 3: this.dResult*=this.dNowInput; break; case 4: if(this.dNowInput=0)
36、 tf1.setText("除數不能為零"); this.keyAvailable=false; else this.dResult=this.dResult/this.dNowInput; if(this.keyAvailable)tf1.setText(Double.t
37、oString(this.dResult); / key "MS" if(this.keyAvailable && e.getActionCommand()="MS") this.dMemory=this.dNowInput; if(this.dMemory!=0) tf2.setText("M"); / key "MC"
38、160; if(this.keyAvailable && e.getActionCommand()="MC") this.dMemory=0; tf2.setText(""); / key "MR" if(this.keyAvailable && e.getActionCommand()="MR") this.dNowInput=this.dMem
39、ory; tf1.setText(Double.toString(this.dNowInput); / key "M+" if(this.keyAvailable && e.getActionCommand()="M+") this.dMemory+=this.dNowInput; if(this.dMemory!=0) tf2.setText("M");
40、60; else tf2.setText(""); / key "%" if(this.keyAvailable && e.getActionCommand()="%") this.dNowInput=(this.dResult*this.dNowInput)/100; tf1.setText(Double.toString(this.dNowInput); / key "
41、Backspace" if(this.keyAvailable && e.getActionCommand()="Backspace") if(!this.alreadyClickedEqueal) if(this.dNowInput!=0) if(this.alreadyHaveDot) if(this.n=-1)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T/LTXH 001-2023“天賦河套”區域公用品牌黃柿子
- 上海教師考試真題及答案
- 絕地求生活動比賽策劃與執行
- 2025西寧城市職業技術學院輔導員考試試題及答案
- 2025貴州機械工業職工大學輔導員考試試題及答案
- 2025焦作大學輔導員考試試題及答案
- 有趣的漢字主題班隊活動
- 愛護水資源教學課件大綱
- 拖班課程設計實施框架
- 國能包頭煤化工有限責任公司招聘筆試題庫2025
- 福建省醫學會專科分會管理辦法
- DB63∕T 1683-2018 青海省農牧區公共廁所工程建設標準
- 我們的互聯網時代課件PPT模板
- 維修電工高級技師論文(6篇推薦范文)
- 人民幣教具正反面完美打印版
- 新編簡明英語語言學教程第二版課后參考答案
- 最新房地產開發預算表
- 最新零售藥店GSP認證條款
- 斗輪機安裝調試及使用說明書
- 家庭住房情況查詢申請表
- 2019年甘肅省天水市中考生物試題(word版,含答案)
評論
0/150
提交評論