簡易計算器課程設(shè)計_第1頁
簡易計算器課程設(shè)計_第2頁
簡易計算器課程設(shè)計_第3頁
簡易計算器課程設(shè)計_第4頁
簡易計算器課程設(shè)計_第5頁
已閱讀5頁,還剩28頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、目錄一、運行環(huán)境············································5二、問題描述及要求···

2、···································5三、需求分析··············

3、;······························5四、設(shè)計思路··················

4、3;·························54.工作原理圖·······················&#

5、183;···············64.功能規(guī)劃·································&

6、#183;·······6五、程序的界面設(shè)計及代碼實現(xiàn)····························75.聲明的類··········

7、83;·····························75.引用的包···················&#

8、183;····················75.界面設(shè)計····························&

9、#183;···········75.4計算功能實現(xiàn)····································15六

10、、參考文獻(xiàn)···········································21七、總結(jié)·····&

11、#183;·········································21八、源代碼·······

12、······································22一、運行環(huán)境(1)操作系統(tǒng):Solaris、Windows xp、Windows 7等(2). 應(yīng)用軟件:jdk1.5、Eclipse二、問

13、題描述及要求制作一個計算器,要求仿Windows里的計算器,設(shè)計一個圖形界面,其中基本組件包括09、+、-、*、/、.、=、+/-、Back、CE、C、sqrt、%、1/x、。其基本功能完成加減乘除、開方、求模、求倒,十進(jìn)制與八進(jìn)制、二進(jìn)制、十六進(jìn)制的轉(zhuǎn)換等,退格、清零等按鈕的實現(xiàn)。 在我的計算器上實現(xiàn)了以上功能。在菜單欄有查看、編輯、幫助菜單,在“查看”菜單中有“標(biāo)準(zhǔn)型”菜單項,實現(xiàn)加減乘除等基本功能,“科學(xué)型”菜單項,實現(xiàn)進(jìn)制間的轉(zhuǎn)換。在“編輯”菜單中有“復(fù)制”、“粘貼”菜單項。“幫助”菜單中有“關(guān)于計算器”、“幫助主題”菜單項,并實現(xiàn)相關(guān)功能。三、需求分析日常生活中經(jīng)常需要用到計算器,比

14、如科學(xué)計算、數(shù)值計算、會計業(yè)務(wù)等,但簡單的計算器已不能滿足日常需要,因此有必要開發(fā)一些進(jìn)制轉(zhuǎn)換、開方等多種運算的計算器。創(chuàng)建一個簡單計算器,具有簡單的人機(jī)交互界面,便于數(shù)據(jù)計算。我的計算器具有的功能如下:1、實現(xiàn)基本的加、減、乘、除四則運算及開方、求倒、求模。2、Back退格、CE返回上層運算符、C清零功能。3、十進(jìn)制與二進(jìn)制、八進(jìn)制、十六進(jìn)制之間的轉(zhuǎn)換。4、菜單中包含的一些功能,如幫助文檔。四、設(shè)計思路4.1 工作原理圖開始按鍵判斷進(jìn)制轉(zhuǎn)換數(shù)字按鍵運用相應(yīng)處理按鍵判斷運算符按鍵進(jìn)制轉(zhuǎn)換顯示結(jié)果是否繼續(xù)操作結(jié)束4.2功能規(guī)劃本程序繼承父類Frame,運用了布局管理器GridLayout和Bor

15、derlayout,將界面分為三塊,頂上為單行文本框,中間為進(jìn)制單選鈕和退格、清空按鈕,下面是數(shù)字、符號按鈕。各種按鈕采用行列的網(wǎng)格布局,并注冊按鈕事件監(jiān)聽器。事件監(jiān)聽器中的事件處理方法void actionPerformed(ActionEvent event)完成主要的按鈕事件的處理。事件分為以下幾種情況:數(shù)字按鈕事件()、運算符按鈕事件(、×、)、正負(fù)號按鈕事件()、小數(shù)點按鈕事件()、等號按鈕事件()、求倒按鈕事件()、三角函數(shù)按鈕事件(cos,sin,tan)、開方按鈕事件(sqrt)、對數(shù)按鈕事件(lgX,lnX)、進(jìn)制轉(zhuǎn)換按鈕事件。在此聲明的是每次輸入的數(shù)據(jù)都要進(jìn)行類型

16、轉(zhuǎn)換。五、程序的界面設(shè)計及代碼實現(xiàn)5.1聲明的類* 類名: calculator * 作用: 主類。* 繼承的父類: JFrame類 * 實現(xiàn)的接口:ActionListener類 * 類名: WindowDestroyer * 作用: 退出窗口動作。 * 繼承的父類: WindowAdapter類 * 實現(xiàn)的接口:無 * 類名: objConversion * 作用: 各個進(jìn)制之間的轉(zhuǎn)換。* 繼承的父類: 無 * 實現(xiàn)的接口:無 *5.2引用的包import java.awt.*;import javax.swing.*;import java.lang.Math;import java.a

17、wt.event.*;5.3界面設(shè)計、調(diào)試后的界面:標(biāo)準(zhǔn)型JScrollPane scrollHelp;private objConversion convert = new objConversion();JMenuItem fileMenu,exitItemOfFile,s,t, about, me;JRadioButton sixteen,ten,eight,two;/單選按扭JbuttonBack,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;Jbuttona,b,cc,dd,ee,ff,jia,jian,cheng,ch

18、u,quyu,deng,fu,dian,kai,dao,cos,sin,tan,lgX,lnX;Container cp;/容器,便于集體操作 JTextField text; String copycontent=""booleanclickable=true,clear=true;intall=0;doubleqian; String fuhao,copy;intjin=10,first=1;public Calculator()super("計算器"); setSize(400,400); setLocation(400,400);text=new

19、 JTextField(25);text.setHorizontalAlignment(JTextField.LEFT);/從左到右 JPanel cp1=new JPanel(); JPanel cp2=new JPanel(); JPanel cp3=new JPanel();cp=getContentPane();cp.add(cp1,"North");cp.add(cp2,"Center");cp.add(cp3,"South"); cp1.setLayout(new GridLayout(1,1); cp2.setLayou

20、t(new GridLayout(2,4); cp3.setLayout(new GridLayout(7,4);sixteen=new JRadioButton("十六進(jìn)制");sixteen.setVisible(false);ten=new JRadioButton("進(jìn)制",true);ten.setVisible(false);eight=new JRadioButton("八進(jìn)制");eight.setVisible(false);two=new JRadioButton("二進(jìn)制");two.setV

21、isible(false);a = new JButton("A");a.setVisible(false);b = new JButton("B");b.setVisible(false);cc = new JButton("C");cc.setVisible(false);dd = new JButton("D");dd.setVisible(false);ee = new JButton("E");ee.setVisible(false);ff = new JButton("F&

22、quot;);ff.setVisible(false);jia = new JButton("+");jian = new JButton("-");cheng = new JButton("×");chu = new JButton("÷");quyu = new JButton("%");deng = new JButton("=");fu = new JButton("+/-");dian = new JButton("

23、.");kai = new JButton("sqrt");dao = new JButton("1/x");num0=new JButton("0");num1=new JButton("1");num2=new JButton("2");num3=new JButton("3");num4=new JButton("4");num5=new JButton("5");num6=new JButton("6&quo

24、t;);num7=new JButton("7");num8=new JButton("8");cos=new JButton("cos");sin=new JButton("sin");tan=new JButton("tan");lgX=new JButton("lgX");lnX=new JButton("lnX");num9=new JButton("9");sixteen.addActionListener(this);ten

25、.addActionListener(this);eight.addActionListener(this);two.addActionListener(this); ButtonGroup btg=new ButtonGroup();/創(chuàng)建一個多斥作用域 btg.add(sixteen); btg.add(ten); btg.add(eight); btg.add(two); cp1.add(text);text.setEditable(false);text.setBackground(Color.white);Back=new JButton("Back");/Bac

26、kBack.setForeground(Color.red);Back.addActionListener(this);ce=new JButton("CE");/CEce.setForeground(Color.red);ce.addActionListener(this);c=new JButton("C");/Cc.setForeground(Color.red);c.addActionListener(this); cp2.add(sixteen); cp2.add(ten); cp2.add(eight); cp2.add(two); cp2.

27、add(Back); cp2.add(ce); cp2.add(c); cp3.add(num7);num7.addActionListener(this); cp3.add(num8);num8.addActionListener(this); cp3.add(num9);num9.addActionListener(this); cp3.add(chu);chu.addActionListener(this); cp3.add(kai);kai.addActionListener(this); cp3.add(num4);num4.addActionListener(this); cp3.

28、add(num5);num5.addActionListener(this); cp3.add(num6);num6.addActionListener(this); cp3.add(cheng);cheng.addActionListener(this); cp3.add(quyu);quyu.addActionListener(this); cp3.add(num1);num1.addActionListener(this); cp3.add(num2);num2.addActionListener(this); cp3.add(num3);num3.addActionListener(t

29、his); cp3.add(jian);jian.addActionListener(this); cp3.add(dao);dao.addActionListener(this); cp3.add(num0);num0.addActionListener(this); cp3.add(fu);fu.addActionListener(this); cp3.add(dian);dian.addActionListener(this); cp3.add(jia);jia.addActionListener(this); cp3.add(deng);deng.addActionListener(t

30、his); cp3.add(cos);cos.addActionListener(this); cp3.add(sin);sin.addActionListener(this); cp3.add(tan);tan.addActionListener(this); cp3.add(lnX);lnX.addActionListener(this); cp3.add(lgX);lgX.addActionListener(this); cp3.add(a);a.setForeground(Color.magenta);a.setBackground(Color.pink);a.addActionLis

31、tener(this); cp3.add(b);b.setForeground(Color.magenta);b.setBackground(Color.pink);b.addActionListener(this); cp3.add(cc);cc.setForeground(Color.magenta);cc.setBackground(Color.pink);cc.addActionListener(this); cp3.add(dd);dd.setForeground(Color.magenta);dd.setBackground(Color.pink);dd.addActionList

32、ener(this); cp3.add(ee);ee.setForeground(Color.magenta);ee.setBackground(Color.pink);ee.addActionListener(this); cp3.add(ff);ff.setForeground(Color.magenta);ff.setBackground(Color.pink);ff.addActionListener(this); JMenuBar mainMenu = new JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu(

33、"編輯"); JMenu viewMenu = new JMenu("查看"); JMenu helpMenu = new JMenu("幫助"); mainMenu.add(viewMenu); mainMenu.add(editMenu); mainMenu.add(helpMenu);fileMenu = new JMenu("復(fù)制C Ctrl+C");exitItemOfFile = new JMenuItem("粘貼V Ctrl+V");fileMenu.addActionListen

34、er(this);exitItemOfFile.addActionListener(this); editMenu.add(fileMenu); editMenu.add(exitItemOfFile);t = new JMenuItem("標(biāo)準(zhǔn)型");s = new JMenuItem(" 科學(xué)型"); viewMenu.add(t); viewMenu.add(s);t.addActionListener(this);s.addActionListener(this);about = new JMenuItem(" 關(guān)于計算器")

35、;me = new JMenuItem(" 幫助主題");helpMenu.add(about); helpMenu.add(me);about.addActionListener(this);me.addActionListener(this); addWindowListener(new WindowDestroyer();、科學(xué)型:、這是“幫助”菜單里的“幫助主題”菜單項:用了JtextArea將內(nèi)容顯示出來me = new JMenuItem(" 幫助主題");JTextArea help = new JTextArea(10, 30); scr

36、ollHelp = new JScrollPane(help); help.setEditable(false); help.append("執(zhí)行簡單計算 "+"n"); help.append("1. 鍵入計算的第一個數(shù)字。"+"n"); help.append("2. 單擊“+”執(zhí)行加、“-”執(zhí)行減、“*”執(zhí)行乘或“/”執(zhí)行除"+"n"); help.append("3. 鍵入計算的下一個數(shù)字。"+"n"); help.append

37、("4. 輸入所有剩余的運算符和數(shù)字。"+"n"); help.append("5. 單擊“=“ ");、這是“幫助”菜單里的“關(guān)于計算器”菜單項:用了JOptionPane.showMessageDialog(null, "計算機(jī)開發(fā)者:魏曉力");5.4計算功能實現(xiàn)、四則運算if (temp = jia)/加法qian = Double.parseDouble(text.getText();fuhao = "+"clear = false; if (temp = jian)qian = Do

38、uble.parseDouble(text.getText();fuhao = "-"clear = false; if (temp = cheng)qian = Double.parseDouble(text.getText();fuhao = "×"clear = false; if(temp=chu)qian = Double.parseDouble(text.getText();fuhao = "÷"clear = false; if (temp = quyu)qian = Double.parseDoub

39、le(text.getText();fuhao="%"clear = false; if (temp = deng)double ss = Double.parseDouble(text.getText();text.setText("");if (fuhao = "+")text.setText(qian + ss + "");if (fuhao = "-")text.setText(qian - ss + "");if (fuhao = "×"

40、;)text.setText(qian * ss + "");if (fuhao = "÷")text.setText(qian / ss + "");if(fuhao="%")text.setText(qian%ss + "");clear = false;/要清空前一次的數(shù)據(jù) 、三角函數(shù)運算if(temp=cos) text.setText(Double.toString(Math.cos(Double.parseDouble(text.getText()*Math.PI/180)

41、; if(temp=sin)text.setText(Double.toString(Math.sin(Double.parseDouble(text.getText()*Math.PI/180); if(temp=tan) text.setText(Double.toString(Math.tan(Double.parseDouble(text.getText()*Math.PI/180); 、對數(shù)運算if(temp=lgX) text.setText(Double.toString(Math.log10(Double.parseDouble(text.getText();if(temp=l

42、nX) text.setText(Double.toString(Math.log(Double.parseDouble(text.getText(); 、開方、求倒運算if (temp = kai)String s = text.getText();if (s.charAt(0) = '-')text.setText("負(fù)數(shù)不能開根號");elsetext.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText();clear = false; if (temp

43、 = dao)if (text.getText().charAt(0) = '0' && text.getText().length() = 1)text.setText("除數(shù)不能為零");elseboolean isDec = true;int i, j, k;String s = Double.toString(1 / Double.parseDouble(text.getText();for (i = 0; i < s.length(); i+)if (s.charAt(i) = '.')break;for (j

44、 = i + 1; j < s.length(); j+)if (s.charAt(j) != '0')isDec = false; break; if (isDec = true) String stemp = ""for (k = 0; k < i; k+) stemp += s.charAt(k);text.setText(stemp); elsetext.setText(s); clear = false; 、正負(fù)號運算if (temp = fu)boolean isNumber = true; String s = text.getTe

45、xt();for (int i = 0; i < s.length(); i+) if(!(s.charAt(i)>='0'&&s.charAt(i)<='9'|s.charAt(i)='.'|s.charAt(i)='-')isNumber = false; break; if (isNumber = true) if (s.charAt(0) = '-')text.setText("");for (int i = 1; i < s.length();

46、i+)char a = s.charAt(i);text.setText(text.getText() + a); elsetext.setText('-' + s); 、退出窗口動作class WindowDestroyer extends WindowAdapter/publicvoid windowClosing(WindowEvent e) System.exit(0); 、進(jìn)制轉(zhuǎn)換class objConversionpublic String decDec(int decNum) String strDecNum = Integer.toString(decNum)

47、;for (int i = strDecNum.length(); i < 3; i+) strDecNum = "0" + strDecNum;return invert (strDecNum, 5); public String decHex (int decNum)/10 to 16 String strHexNum = ""int currentNum = 0;while(decNum != 0)if (decNum > 15) currentNum=decNum%16; decNum /= 16; else currentNum =

48、 decNum; decNum = 0; switch (currentNum)case 15: strHexNum += "F" break;case 14: strHexNum += "E" break;case 13: strHexNum += "D" break;case 12: strHexNum += "C" break;case 11: strHexNum += "B" break;case 10: strHexNum += "A" break;default:

49、 strHexNum +=Integer.toString(currentNum);break; return invert(strHexNum,2); public String decOct (int decNum)/10 to 8 String strOctNum = ""while (decNum != 0)if (decNum > 7) strOctNum += Integer.toString(decNum % 8); decNum /= 8; else strOctNum += Integer.toString(decNum); decNum = 0;

50、return invert (strOctNum, 3); public String decBin (int decNum) /10 to 2 String strBinNum = ""while (decNum != 0) if (decNum > 1) strBinNum += Integer.toString(decNum % 2); decNum /= 2; else strBinNum += Integer.toString(decNum); decNum = 0; return invert (strBinNum, 8); private String

51、invert(String strNum,int minLength) String answer = ""int length = strNum.length();if (length < minLength)for (int padding =(minLength -length);padding>0;padding-) answer += "0" for(int i=length;i>0;i-) answer+=strNum.charAt(i-1);return answer; 六、參考文獻(xiàn)01施霞萍,張歡.Java課程設(shè)計(第二

52、版)M機(jī)械工業(yè)出版社.2006年8月02 耿祥義,張躍平. Java2實用教程(第三版).清華大學(xué)出版社。七、總結(jié)設(shè)計給人以創(chuàng)作的沖動,但是也要為這次沖動承擔(dān)一定的痛苦,卻事后會發(fā)現(xiàn),這一切都是值得的。本次的Java課程設(shè)計讓我對Java的理論知識又有了更深一步的了解,溫故而知新,開始設(shè)計時完全沒頭緒,感覺很混亂,對書本知識不夠扎實的我深感“書到用時方恨少”,只好又一遍瀏覽全書,讓我對其有了大概的復(fù)習(xí),對知識系統(tǒng)全面進(jìn)行了了解,讓我里出了我設(shè)計的框架,遇到困難時先是苦思冥想在向同學(xué)請教。這次課程設(shè)計使我感到收獲不小,讓我對Java的設(shè)計過程有了更深的了解,促進(jìn)了對理論知識的消化與吸收,也鞏固和

53、完善了本門課程的知識體系結(jié)構(gòu)。設(shè)計過程中遇到了不少麻煩:如進(jìn)制間的轉(zhuǎn)換,也遇到了平時學(xué)習(xí)中老師強(qiáng)調(diào)與教過的疑難點,不懂的通過翻閱資料和與同學(xué)間的討論都一一解決了。通過實踐讓我發(fā)現(xiàn)了我的不足,并加深了自身學(xué)習(xí)能力,提高了綜合能力。因此在以后的學(xué)習(xí)中我會通過實踐來檢驗自己的不足和加深、鞏固自身。以此來完善自己的知識系統(tǒng)。這次課程設(shè)計的主要目的是學(xué)會Java程序開發(fā)的環(huán)境搭建與配置,并在實際運用中學(xué)習(xí)和掌握J(rèn)ava程序開發(fā)的全過程,以及進(jìn)一步熟悉掌握J(rèn)ava程序設(shè)計語言的基礎(chǔ)內(nèi)容,提高Java編程技術(shù)以及分析解決問題的綜合能力。通過這次課程設(shè)計,我基本掌握了以上要求。由于專業(yè)知識有限,以及動手能力的

54、欠缺,所以開發(fā)的系統(tǒng)不是很完善,有一些功能未實現(xiàn),但是簡易計算器的基本功能均已實現(xiàn)。以前對Java語言的很多知識認(rèn)識都不深刻,做過這次課程設(shè)計之后,我對Java語言的開發(fā)有了一個比較系統(tǒng)的了解;比如:用戶圖形界面設(shè)計等的運用已經(jīng)比較熟練。八、源代碼import java.awt.*;import javax.swing.*;import java.lang.Math;import java.awt.event.*;publicclassCalculatorextends JFrame implements ActionListenerJScrollPane scrollHelp;private

55、 objConversion convert = new objConversion();/各個進(jìn)制之間的轉(zhuǎn)化JMenuItem fileMenu,exitItemOfFile,s,t, about, me;JRadioButton sixteen,ten,eight,two;/單選按扭JButton Back,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;JButton a,b,cc,dd,ee,ff,jia,jian,cheng,chu,quyu,deng,fu,dian,kai,dao,cos,sin,tan,lgX,lnX

56、;Container cp;/容器,便于集體操作 JTextField text; String copycontent=""booleanclickable=true,clear=true;intall=0;doubleqian; String fuhao,copy;intjin=10,first=1;public Calculator()super("計算器"); setSize(400,400); setLocation(400,400);text=new JTextField(25);text.setHorizontalAlignment(JTe

57、xtField.LEFT);/從左到右 JPanel cp1=new JPanel(); JPanel cp2=new JPanel(); JPanel cp3=new JPanel();cp=getContentPane();cp.add(cp1,"North");cp.add(cp2,"Center");cp.add(cp3,"South"); cp1.setLayout(new GridLayout(1,1); cp2.setLayout(new GridLayout(2,4); cp3.setLayout(new GridLa

58、yout(7,4);sixteen=new JRadioButton("十六進(jìn)制");sixteen.setVisible(false);ten=new JRadioButton("進(jìn)制",true);ten.setVisible(false);eight=new JRadioButton("八進(jìn)制");eight.setVisible(false);two=new JRadioButton("二進(jìn)制");two.setVisible(false);a = new JButton("A");a.

59、setVisible(false);b = new JButton("B");b.setVisible(false);cc = new JButton("C");cc.setVisible(false);dd = new JButton("D");dd.setVisible(false);ee = new JButton("E");ee.setVisible(false);ff = new JButton("F");ff.setVisible(false);jia = new JButton(&

60、quot;+");jian = new JButton("-");cheng = new JButton("×");chu = new JButton("÷");quyu = new JButton("%");deng = new JButton("=");fu = new JButton("+/-");dian = new JButton(".");kai = new JButton("sqrt");da

61、o = new JButton("1/x");num0=new JButton("0");num1=new JButton("1");num2=new JButton("2");num3=new JButton("3");num4=new JButton("4");num5=new JButton("5");num6=new JButton("6");num7=new JButton("7");num8=new JB

62、utton("8");cos=new JButton("cos");sin=new JButton("sin");tan=new JButton("tan");lgX=new JButton("lgX");lnX=new JButton("lnX");num9=new JButton("9");sixteen.addActionListener(this);ten.addActionListener(this);eight.addActionListene

63、r(this);two.addActionListener(this); ButtonGroup btg=new ButtonGroup();/創(chuàng)建一個多斥作用域 btg.add(sixteen); btg.add(ten); btg.add(eight); btg.add(two); cp1.add(text);text.setEditable(false);text.setBackground(Color.white);Back=new JButton("Back");/BackBack.setForeground(Color.red);Back.addActionListener(this);ce=new JButton("CE");/CEce.setForeground(Color.red);ce.addActionListener(this);c=new JButton("C");/Cc.setForeground(Color.red);c.addActionListener(this); cp2.add(six

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論