![Java實(shí)驗(yàn)報(bào)告-圖形化界面計(jì)算器_第1頁](http://file4.renrendoc.com/view/0a2b14f284931c160c2d6db2ac364200/0a2b14f284931c160c2d6db2ac3642001.gif)
![Java實(shí)驗(yàn)報(bào)告-圖形化界面計(jì)算器_第2頁](http://file4.renrendoc.com/view/0a2b14f284931c160c2d6db2ac364200/0a2b14f284931c160c2d6db2ac3642002.gif)
![Java實(shí)驗(yàn)報(bào)告-圖形化界面計(jì)算器_第3頁](http://file4.renrendoc.com/view/0a2b14f284931c160c2d6db2ac364200/0a2b14f284931c160c2d6db2ac3642003.gif)
![Java實(shí)驗(yàn)報(bào)告-圖形化界面計(jì)算器_第4頁](http://file4.renrendoc.com/view/0a2b14f284931c160c2d6db2ac364200/0a2b14f284931c160c2d6db2ac3642004.gif)
![Java實(shí)驗(yàn)報(bào)告-圖形化界面計(jì)算器_第5頁](http://file4.renrendoc.com/view/0a2b14f284931c160c2d6db2ac364200/0a2b14f284931c160c2d6db2ac3642005.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
-21-封面/lzj,2015/11/29Java實(shí)驗(yàn)報(bào)告 --圖形化界面計(jì)算器 計(jì)算機(jī)22班 清哥蕩漾 2120505047
目錄封面/lzj,2015/11/29 -1-目錄 -2-一、 實(shí)驗(yàn)要求 -3-二、 實(shí)驗(yàn)流程 -3-1. 需求分析 -3-2. 概要設(shè)計(jì) -3-3. 詳細(xì)設(shè)計(jì) -4-3-1.代碼結(jié)構(gòu) -4-3-2.結(jié)構(gòu)說明 -4-4. 編碼實(shí)現(xiàn) -5-5. 測試糾錯 -5-三、 實(shí)驗(yàn)結(jié)果 -6-1. 初始化界面 -6-2. 界面說明 -7-2-1顯示部分 -7-2-2控制部分 -8-3. 結(jié)果演示 -8-3-1單目運(yùn)算 -8-3-2雙目運(yùn)算 -9-3-3混合運(yùn)算 -10-3-4錯誤演示 -11-3-5保存本地操作結(jié)果 -11-3-6上傳服務(wù)器操作演示 -12-4. 總結(jié)結(jié)果 -13-四、 總結(jié)分析 -13-五、 實(shí)驗(yàn)代碼 -14-1. gui包 -14-GUI.java -14-DispPanel.java -15-CtrlPanel.java -17-2. component包 -18-MyButton.java -18-MyTestField.java -18-3. actionlistener包 -19-MyButtonActionListener.java -19-4. operation包 -23-ArithmeLogic.java -23-UploadOperate.java -35-SavaOparete.java -35-5. Main.java -37-實(shí)驗(yàn)要求實(shí)現(xiàn)圖形化界面計(jì)算器,完成基本功能實(shí)現(xiàn)保存計(jì)算歷史紀(jì)錄到本地的功能實(shí)現(xiàn)上傳計(jì)算歷史紀(jì)錄到服務(wù)器的功能實(shí)驗(yàn)流程需求分析對于基本功能,要求實(shí)現(xiàn)圖形化界面計(jì)算器,則可參照電腦或手機(jī)上的計(jì)算器進(jìn)行頁面布局,最后加上各種監(jiān)聽器即可。附加要求實(shí)現(xiàn)保存到本地,只需將內(nèi)存中的歷史紀(jì)錄通過文件讀寫到本地即可。附加要求實(shí)現(xiàn)上傳至服務(wù)器,可以用TCP或者UDP報(bào)文通信實(shí)現(xiàn)概要設(shè)計(jì) 2-1.整體采用手機(jī)計(jì)算器布局架構(gòu) 2-2.屏幕顯示用文本框?qū)崿F(xiàn) 2-3.按鈕監(jiān)聽自定義ActionListener類 2-4.輸入時(shí)應(yīng)限制錯誤輸入 2-5.運(yùn)算時(shí)采用逆波蘭式2-6.保存本地功能采用隨機(jī)讀寫方式添加至文件尾部2-7.上傳采用UDP數(shù)據(jù)報(bào)方式詳細(xì)設(shè)計(jì)3-1.代碼結(jié)構(gòu)3-2.結(jié)構(gòu)說明 gui包:實(shí)現(xiàn)圖形化界面 GUI.java:整體架構(gòu) DispPanel.java:顯示面板部分 CtrlPanel.java:控制面板部分 component包:用到的部件 MyButton.java:自定義JButton類,進(jìn)行自定義設(shè)定 MyTextField.java:自定義JTextField類,用于屏幕顯示 actionlistener包:監(jiān)聽器 MyButtonActionListener.java:對按鈕進(jìn)行監(jiān)聽操作,分別進(jìn)行不同操作 operation包:計(jì)算、保存上傳操作 ArithmeticLogic.java:獲取屏幕輸入字符串進(jìn)行運(yùn)算后返回結(jié)果 SaveOperate.java:保存計(jì)算歷史到本地 UploadOperate.java:上傳計(jì)算歷史到服務(wù)器 Main.java:main函數(shù)所在,調(diào)用整個(gè)工程編碼實(shí)現(xiàn) 工具:Eclipse 環(huán)境:jse1.8 人員:我 耗時(shí):數(shù)十小時(shí) 經(jīng)歷:小問題有些,debug很煎熬 結(jié)果:基本功能完成,保存功能完成,上傳功能完成但未驗(yàn)證測試糾錯 每一個(gè)模塊完成后都及時(shí)進(jìn)行糾錯處理,最后整體進(jìn)行大量測試,對發(fā)現(xiàn)的錯誤進(jìn)行糾正。發(fā)現(xiàn)錯誤包括: 輸入限制邏輯錯誤 運(yùn)算邏輯錯誤 異常監(jiān)聽錯誤 函數(shù)調(diào)用錯誤 經(jīng)過鏖戰(zhàn),干掉bug!實(shí)驗(yàn)結(jié)果初始化界面界面說明2-1顯示部分 屏幕區(qū)域由上到下分為三個(gè)區(qū)域,依次顯示: 提示信息:正常顯示“Let’sROCK”,發(fā)生錯誤提示錯誤信息,及其他操作信息 上次運(yùn)算歷史:如圖“sin(2.5*pi)=1” 本次運(yùn)算結(jié)果:如圖“1.0”2-2控制部分 控制部分整體采用Gridlayout布局進(jìn)行排列: 按鈕布局仿照紅米1s的計(jì)算器結(jié)果演示3-1單目運(yùn)算3-2雙目運(yùn)算3-3混合運(yùn)算3-4錯誤演示3-5保存本地操作結(jié)果 注意:采用特定輸入序列(88224466!!^^)啟動操作3-6上傳服務(wù)器操作演示 注意:采用特定輸入序列(82824646!^!^)啟動操作總結(jié)結(jié)果 基本功能堪稱完美,經(jīng)大量測試不再有未知錯誤發(fā)生,已發(fā)現(xiàn)bug已經(jīng)全部解決 上傳操作發(fā)送數(shù)據(jù)報(bào)報(bào)文,時(shí)間原因來不及測試了 保存本地操作沒有任何問題,測試結(jié)果良好總結(jié)分析 通過本次實(shí)驗(yàn),發(fā)現(xiàn)了平時(shí)許多沒有發(fā)現(xiàn)的問題。首先此前沒有學(xué)過java,上課時(shí)學(xué)到的知識在課后沒能及時(shí)復(fù)習(xí)回顧,遺漏了一些很小但是卻致命的問題,幸而在本次實(shí)驗(yàn)中及時(shí)發(fā)現(xiàn),亡羊補(bǔ)牢,結(jié)果還大體滿意。 理論學(xué)習(xí)固然重要,實(shí)際應(yīng)用也必不可少,在實(shí)踐中把學(xué)過的知識應(yīng)用出來才能最大程度的加深自己的學(xué)習(xí)理解程度,也可以將自己平時(shí)沒有注意到的錯誤即使解決掉。 最后,感謝老師的教導(dǎo)!實(shí)驗(yàn)代碼gui包GUI.javapackagecom.lzj.caculator_javaexp.gui;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Panel;importjava.awt.Toolkit;importjavax.swing.ImageIcon;importjavax.swing.JFrame;publicclassGUIextendsJFrame{//圖形化界面 privateintmidScreenX;//屏幕中心點(diǎn)x privateintmidScreenY;//屏幕中心點(diǎn)y privatefinalintwindowWidth=320;//窗口寬度 privatefinalintwindowHeight=480;//窗口高度 privatefinalintdispPanelX=10; privatefinalintdispPanelY=10; privatefinalintdispPanelWidth=300; privatefinalintdispPanelHeight=165; privatefinalintctrlPanelX=20; privatefinalintctrlPanelY=185; privatefinalintctrlPanelWidth=280; privatefinalintctrlPanelHeight=275; publicGUI(){ //TODOAuto-generatedconstructorstub ImageIconicon=newImageIcon("res/icon.png");//設(shè)定圖標(biāo) setIconImage(icon.getImage()); setTitle("計(jì)算器Beta1.0"); setBounds();//調(diào)用自定義函數(shù)設(shè)定邊界 setContent();//調(diào)用自定義函數(shù)設(shè)定內(nèi)容 setResizable(false); // setUndecorated(true); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //自定義setBounds,使窗口位于屏幕中央 privatevoidsetBounds(){ DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize(); midScreenX=(int)(screenSize.getWidth()/2); midScreenY=(int)(screenSize.getHeight()/2); //有邊框,保持內(nèi)部大小故在寬度高度上補(bǔ)全 setBounds(midScreenX-windowWidth/2,midScreenY-windowHeight/2,windowWidth+6,windowHeight+29); } privatevoidsetContent(){ setLayout(null); //面板 Panelpanel=newPanel(null); panel.setBounds(0,0,windowWidth,windowHeight); panel.setBackground(newColor(51,51,51));//整體顏色,仿計(jì)算器 add(panel); //顯示面板 DispPaneldispPanel=newDispPanel(dispPanelWidth,dispPanelHeight); dispPanel.setBounds(dispPanelX,dispPanelY,dispPanelWidth,dispPanelHeight); panel.add(dispPanel); //控制面板 CtrlPanelctrlPanel=newCtrlPanel(ctrlPanelWidth,ctrlPanelWidth); ctrlPanel.setBounds(ctrlPanelX,ctrlPanelY,ctrlPanelWidth,ctrlPanelHeight); panel.add(ctrlPanel); }}DispPanel.javapackagecom.lzj.caculator_javaexp.gui;importjava.awt.Color;importjava.awt.Font;importjava.awt.Panel;importcom.lzj.caculator_ponent.MyTextField;publicclassDispPanelextendsPanel{//顯示面板類 publicstaticMyTextFieldshowHintInfo_TextField; publicstaticMyTextFieldlastCaculation_TextField; publicstaticMyTextFieldcurrentCaculation_TextField; publicDispPanel(intwidth,intheight){ //TODOAuto-generatedconstructorstub setBackground(newColor(204,255,204));//仿屏幕顏色 setLayout(null); //操作提示信息 showHintInfo_TextField=newMyTextField(); showHintInfo_TextField.setBounds(0,0,width,height/4); showHintInfo_TextField.setFont(newFont(null,Font.PLAIN,16)); showHintInfo_TextField.setText("Let'sROCK"); add(showHintInfo_TextField); //上次運(yùn)算 lastCaculation_TextField=newMyTextField(); lastCaculation_TextField.setBounds(5,height/4,width-10,height/4); lastCaculation_TextField.setFont(newFont(null,Font.PLAIN,16)); lastCaculation_TextField.setText(""); add(lastCaculation_TextField); //本次運(yùn)算 currentCaculation_TextField=newMyTextField(); currentCaculation_TextField.setBounds(5,height/2,width-10,height/2); currentCaculation_TextField.setFont(newFont(null,Font.PLAIN,32)); currentCaculation_TextField.setText("0"); add(currentCaculation_TextField); }}CtrlPanel.javapackagecom.lzj.caculator_javaexp.gui;importjava.awt.GridLayout;importjava.awt.Panel;importcom.lzj.caculator_ponent.MyButton;publicclassCtrlPanelextendsPanel{ publicCtrlPanel(intwidth,intheight){ //TODOAuto-generatedconstructorstub setLayout(newGridLayout(6,5)); add(newMyButton("!")); add(newMyButton("^")); add(newMyButton("√")); add(newMyButton("π")); add(newMyButton("C")); add(newMyButton("sin")); add(newMyButton("(")); add(newMyButton(")")); add(newMyButton("e")); add(newMyButton("←")); add(newMyButton("cos")); add(newMyButton("7")); add(newMyButton("8")); add(newMyButton("9")); add(newMyButton("÷")); add(newMyButton("tan")); add(newMyButton("4")); add(newMyButton("5")); add(newMyButton("6")); add(newMyButton("×")); add(newMyButton("ln")); add(newMyButton("1")); add(newMyButton("2")); add(newMyButton("3")); add(newMyButton("-")); add(newMyButton("log")); add(newMyButton("0")); add(newMyButton(".")); add(newMyButton("=")); add(newMyButton("+")); }}component包MyButton.javapackagecom.lzj.caculator_ponent;importjava.awt.Color;importjava.awt.Font;importjavax.swing.JButton;importcom.lzj.caculator_javaexp.actionlistener.MyButtonActionListener;publicclassMyButtonextendsJButton{ publicMyButton(Stringstr){ //TODOAuto-generatedconstructorstub newJButton(); setText(str); if(str.equals("=")){ setBackground(newColor(255,102,00)); } setFont(newFont(null,Font.PLAIN,16)); setFocusPainted(false); addActionListener(newMyButtonActionListener()); } }MyTestField.javapackagecom.lzj.caculator_ponent;importjavax.swing.JTextField;publicclassMyTextFieldextendsJTextField{ publicMyTextField(){//自定義JTextField類 newJTextField(); setEditable(false); setBackground(null); setBorder(null); setHorizontalAlignment(JTextField.RIGHT); } publicvoidappend(Stringstr){//追加字符串 Stringstring=getText(); setText(string+str); if(string.length()==0){ setText(str); }else{ if(string.charAt(string.length()-1)=='0'&&!str.equals(".")){//末尾為"0"且不是追加"." if(string.length()==1){//只有一個(gè)"0" string=""; }elseif(string.charAt(string.length()-2)<'1'&&string.charAt(string.length()-2)>'9'){//"0"之前不是數(shù)字 string=string.substring(0,string.length()-1); } } setText(string+str); } } publicvoidremoveEnd(){//刪除最后一個(gè)字符 Stringstring=getText(); if(string.length()<=1){ setText("0"); }else{ setText(string.substring(0,string.length()-1)); } }}actionlistener包MyButtonActionListener.javapackagecom.lzj.caculator_javaexp.actionlistener;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importcom.lzj.caculator_ponent.MyButton;importcom.lzj.caculator_javaexp.gui.DispPanel;importcom.lzj.caculator_javaexp.operation.ArithmeticLogic;importcom.lzj.caculator_javaexp.operation.SaveOperate;importcom.lzj.caculator_javaexp.operation.UploadOperate;publicclassMyButtonActionListenerimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ //TODOAuto-generatedmethodstub Stringstring=((MyButton)e.getSource()).getText();//按鈕上的文本 switch(string){//各個(gè)按鈕的功能 //直接在最后面追加本次輸入 case"0": case"1": case"2": case"3": case"4": case"5": case"6": case"7": case"8": case"9": case"π": case"e": case"!": case"^": case"(": case")": DispPanel.currentCaculation_TextField.append(string); break; //后跟一個(gè)"(" case"√": case"sin": case"cos": case"tan": case"ln": case"log": DispPanel.currentCaculation_TextField.append(string+"("); break; //一個(gè)數(shù)字只能輸入一個(gè)"." case".": StringtempStr=DispPanel.currentCaculation_TextField.getText(); charc=tempStr.charAt(tempStr.length()-1); if(c<'0'||c>'9'){//最后不是數(shù)字,直接返回 return; } for(inti=0;i<tempStr.length();i++){ c=tempStr.charAt(tempStr.length()-1-i); if(c>='0'&&c<='9'){ continue; }elseif(c=='.'){//數(shù)字已經(jīng)含有小數(shù)點(diǎn),返回 return; }else{//最后一個(gè)數(shù)字內(nèi)不含小數(shù)點(diǎn) break; } } DispPanel.currentCaculation_TextField.append("."); break; //運(yùn)算符問題 case"+": case"÷": case"×": StringtempStr1=DispPanel.currentCaculation_TextField.getText(); charlastChar=tempStr1.charAt(tempStr1.length()-1); if(lastChar=='+'||lastChar=='-'||lastChar=='×'||lastChar=='÷'){ DispPanel.currentCaculation_TextField.removeEnd(); } DispPanel.currentCaculation_TextField.append(string); break; //運(yùn)算符問題(可能負(fù)號) case"-": StringtempStr2=DispPanel.currentCaculation_TextField.getText(); charlastChar1=tempStr2.charAt(tempStr2.length()-1); // System.out.println("lastChar:"+lastChar1); if(lastChar1=='-'){ break; }else{ if(lastChar1=='+'){ DispPanel.currentCaculation_TextField.removeEnd(); } DispPanel.currentCaculation_TextField.append("-"); break; } //回退 case"←": DispPanel.currentCaculation_TextField.removeEnd(); break; //清零,清楚提示信息 case"C": DispPanel.currentCaculation_TextField.setText("0"); DispPanel.showHintInfo_TextField.setText("Let'sROCK"); break; //計(jì)算本次結(jié)果,并更新歷史計(jì)算框 case"=": getResult(); break; } } privatevoidgetResult(){//按"="后的任務(wù) Stringstring=DispPanel.currentCaculation_TextField.getText(); //輸入密令,保存計(jì)算歷史到本地 if(string.equals("88224466!!^^")){ // if(SaveOperate.saveToLocal()){//保存操作 if(newSaveOperate().saveToLocal()){ DispPanel.showHintInfo_TextField.setText("保存成功,,,繼續(xù)Rock"); }else{ DispPanel.showHintInfo_TextField.setText("保存失敗,可憐的娃>>>接著Rock"); } DispPanel.currentCaculation_TextField.setText("0"); } //輸入密令,上傳到服務(wù)器 elseif(string.equals("82824646!^!^")){ if(UploadOperate.uploadToServer()){//保存操作 DispPanel.showHintInfo_TextField.setText("上傳成功,,,繼續(xù)Rock"); }else{ DispPanel.showHintInfo_TextField.setText("上傳失敗,可憐的娃>>>接著Rock"); } DispPanel.currentCaculation_TextField.setText("0"); } //進(jìn)行運(yùn)算,更新上次運(yùn)算顯示框;若算式錯誤提示錯誤 else{ Stringresult=ArithmeticLogic.ALFunc(string); if(result.contains("Error")){//算式錯誤,識別錯誤碼并進(jìn)行提示 DispPanel.showHintInfo_TextField.setText(result.substring(6)); }else{//運(yùn)算成功 DispPanel.currentCaculation_TextField.setText(result);//運(yùn)算結(jié)果 string=string+"="+result; DispPanel.lastCaculation_TextField.setText(string); } } }}operation包ArithmeLogic.javapackagecom.lzj.caculator_javaexp.operation;importjava.util.Stack;/**publicstaticStringALFunc(Stringinput){}方法實(shí)現(xiàn)運(yùn)算**>>>運(yùn)算流程1:將輸入的中綴表達(dá)式轉(zhuǎn)為后綴表達(dá)式,即逆波蘭式* * >>>注意:使用一個(gè)棧和一個(gè)隊(duì)列,輸出的放到隊(duì)列里;* >>>我暈,怎么搞不定隊(duì)列??那就倆棧吧,,,(-__-)b**>>>運(yùn)算流程2:對流程1后的隊(duì)列進(jìn)行出隊(duì)入棧,棧頂運(yùn)算***>>>注意錯誤:* >>>掃描input輸入串時(shí)可能括號不匹配(這叫輸入錯誤)* >>>數(shù)字錯誤(限定小數(shù)點(diǎn)輸入,故此暫未發(fā)現(xiàn))* >>>運(yùn)算符錯誤(暫未發(fā)現(xiàn))* >>>計(jì)算逆波蘭式時(shí)可能除數(shù)為0(實(shí)測結(jié)果為無窮)* >>>數(shù)字過大計(jì)算結(jié)果溢出,應(yīng)該可以用BigInteger搞定(暫未溢出,java好強(qiáng)大)*>>>bug:* 500行上下所在* 我勒個(gè)去了,為毛stack.firstElement不是棧頂,,,好吧,懶了沒有自己定義* 偷懶poppush了一次拿到棧頂元素**/publicclassArithmeticLogic{ privatestaticStringinput;//存放輸入的算式字符串 privatestaticStringoutput;//最終輸出的結(jié)果字符串 //運(yùn)算時(shí)使用 privatestaticStack<String>s1; privatestaticStack<String>s2; privatestaticStack<Float>s3; privatestaticStringelem;//當(dāng)前元素 privatestaticintfence;//字符串當(dāng)前位置 publicstaticStringALFunc(Stringstring){ input=string; output=""; if(transToRPN()==false){//轉(zhuǎn)換失敗 returnoutput; } while(!s2.isEmpty()){//從s2壓至s1,轉(zhuǎn)為逆波蘭式 s1.push(s2.pop()); } if(caculate()==false){//計(jì)算失敗 returnoutput; } returnoutput; } //從中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式 privatestaticbooleantransToRPN(){ s1=newStack<String>(); s2=newStack<String>(); fence=0; while(true){//否則掃描完畢 switch(getElement()){ case-3: if(!dealCurrentOpr()){ returnfalse; } pushRemain(); returntrue; case-2: s2.push(elem); pushRemain(); returntrue; case-1: output="Error:表達(dá)式錯誤"; returnfalse; case1: returntrue; case2: s2.push(elem); break; case3: if(!dealCurrentOpr()){ returnfalse; } break; default: returnfalse; } } } //計(jì)算逆波蘭式的結(jié)果 privatestaticbooleancaculate(){ s3=newStack<Float>(); while(!s1.isEmpty()){ Stringtemp=s1.pop(); if(temp.charAt(0)>='0'&&temp.charAt(0)<='9'){//運(yùn)算數(shù) try{ Floatf=Float.parseFloat(temp); s3.push(f); }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } }elseif(temp.equals("e")){//e&π s3.push((float)Math.E); }elseif(temp.equals("π")){ s3.push((float)Math.PI); }else{//運(yùn)算符 switch(temp){ case"+": if(s3.size()<2){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push(s3.pop()+s3.pop()); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"-": if(s3.size()==0){ output="Error:表達(dá)式錯誤"; returnfalse; }elseif(s3.size()==1){ s3.push(-s3.pop()); break; } try{ s3.push(-(s3.pop()-s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"×": if(s3.size()<2){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push(s3.pop()*s3.pop()); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"÷": if(s3.size()<2){ output="Error:表達(dá)式錯誤"; returnfalse; } Floattemp1=s3.pop(); if(temp1==0){ output="Error:除數(shù)為零"; returnfalse; } try{ s3.push(s3.pop()/temp1); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"^": if(s3.size()<2){ output="Error:表達(dá)式錯誤"; returnfalse; } floattemp2=s3.pop(); try{ s3.push((float)Math.pow(s3.pop(),temp2)); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"!": if(s3.isEmpty()){ output="Error:表達(dá)式錯誤"; returnfalse; } Floattemp3=s3.pop(); if(temp3%1!=0||temp3<1){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ floattemp4=1; while(temp3>1){ temp4*=temp3--; } s3.push(temp4); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"√": if(s3.isEmpty()){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push((float)Math.pow(s3.pop(),0.5)); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"sin": if(s3.isEmpty()){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push((float)Math.sin(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"cos": if(s3.isEmpty()){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push((float)Math.cos(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"tan": if(s3.isEmpty()){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push((float)Math.tan(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"ln": if(s3.isEmpty()){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push((float)Math.log(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"log": if(s3.isEmpty()){ output="Error:表達(dá)式錯誤"; returnfalse; } try{ s3.push((float)Math.log10(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } } } } if(s3.size()>1){ returnfalse; }else{ output=s3.pop().toString(); returntrue; } } /* *從輸入字符串內(nèi)讀取一個(gè)運(yùn)算數(shù)或運(yùn)算符 * *>>>返回-1:出現(xiàn)錯誤>>>返回-2:結(jié)尾為操作數(shù)>>>返回-3:結(jié)尾為操作符>>>返回1:結(jié)束>>>返回2:操作數(shù) *>>>返回3:操作符 * */ privatestaticintgetElement(){ charc=input.charAt(fence); if(c>='0'&&c<='9'){//數(shù)字 elem=""+c; while(fence<input.length()-1){ fence++; c=input.charAt(fence); if((c<'0'||c>'9')&&c!='.'){ if(c=='π'||c=='e'){//錯誤:*5e* return-1; } return2;//操作數(shù) } elem+=c; } return-2;//input輸入串掃描完畢 } elseif(c=='π'||c=='e'){ elem=""+c; if(fence==input.length()-1){ return-2;//input輸入串掃描完畢 } fence++; c=input.charAt(fence); if((c>='0'&&c<='9')||c=='e'||c=='π'){ return-1;//錯誤:*e1* } return2; } else{//運(yùn)算符 switch(c){ case'!': case'^': case'√': case'(': case')': case'+': case'-': case'×': case'÷': elem=""+c; if(fence==input.length()-1){ return-3;//結(jié)尾是運(yùn)算符 } fence++; c=input.charAt(fence); return3;//運(yùn)算符 case's': if(input.length()-fence<3){ return-1;//錯誤 } if(input.charAt(fence+1)!='i'||input.charAt(fence+2)!='n'){ return-1;//錯誤 } elem="sin"; fence+=2; if(fence==input.length()-1){ return-3;//運(yùn)算符結(jié)尾 } fence++; c=input.charAt(fence); return3;//運(yùn)算符 case'c': if(input.length()-fence<3){ return-1;//錯誤 } if(input.charAt(fence+1)!='o'||input.charAt(fence+2)!='s'){ return-1;//錯誤 } elem="cos"; fence+=2; if(fence==input.length()-1){ return-3;//運(yùn)算符結(jié)尾 } fence++; c=input.charAt(fence); return3;//運(yùn)算符 case't': if(input.length()-fence<3){ return-1;//錯誤 } if(input.charAt(fence+1)!='a'||input.charAt(fence+2)!='n'){ return-1;//錯誤 } elem="tan"; fence+=2; if(fence==input.length()-1){ return-3;//運(yùn)算符結(jié)尾 } fence++; c=input.charAt(fence); return3;//運(yùn)算符 case'l': elem=""+c; if(fence==input.length()-1){ return-1;//錯誤:*l+null } fence++; c=input.charAt(fence); if(c=='n'){ elem+=c; if(fence==input.length()-1){ return-3;//結(jié)尾是運(yùn)算符 } fence++; c=input.charAt(fence); return3;//運(yùn)算符 }elseif(c=='o'){ if(input.length()-fence<2){ return-1;//錯誤 } if(input.charAt(fence+1)!='g'){ return-1;//錯誤 } elem="log"; fence++; if(fence==input.length()){ return-3;//運(yùn)算符結(jié)尾 } fence++; c=input.charAt(fence); return3; } default: return-1; } } } //轉(zhuǎn)換逆波蘭式時(shí)對當(dāng)前操作符進(jìn)行處理 privatestaticbooleandealCurrentOpr(){ if(s1.isEmpty()){//空,直接入棧 s1.push(elem); returntrue; }elseif(elem.equals("(")){//左括號直接入棧 s1.push(elem); returntrue; }elseif(elem.equals(")")){//右括號,出棧至左括號 while(true){ if(s1.isEmpty()){ output="Error:括號不匹配"; returnfalse; } Stringtemp=s1.pop(); if(temp.equals("(")){ returntrue; } s2.push(temp); } }else{//其他符號,根據(jù)優(yōu)先級不同開始操作 while(true){ /* if(!s1.isEmpty()){//****firstElement()不是棧頂????! System.out.println(s1.firstElement()); Stringtemp=s1.pop(); System.out.println(temp); s1.push(temp); }*/ Stringtemp_wuyu=""; if(!s1.isEmpty()){temp_wuyu=s1.pop();s1.push(temp_wuyu);}//好無奈的問題 if(s1.isEmpty()||getPriority(/*s1.firstElement()*/temp_wuyu)<getPriority(elem)){ s1.push(elem); returntrue; } Stringtemp=s1.pop(); s2.push(temp); } } } //input字符串掃描完畢后,將符號棧內(nèi)容壓入逆波蘭式棧 privatestaticvoidpushRemain(){ while(!s1.isEmpty()){ s2.push(s1.pop()); } } //返回當(dāng)前運(yùn)算符的優(yōu)先級 privatestaticintgetPriority(Stringopr){ s
溫馨提示
- 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 服裝店裝修承包協(xié)議
- 服裝店裝修合同范本公裝
- 農(nóng)村養(yǎng)殖場裝修協(xié)議模板
- 市政項(xiàng)目土石方運(yùn)輸合同
- KTV裝修預(yù)算及施工協(xié)議
- 婚紗店裝修合同模板
- 綠化養(yǎng)護(hù)工程施工方案
- 發(fā)票附帶合同范例范例
- 煙囪外掛電梯安裝施工方案
- 襄城鍋爐噴淋塔施工方案
- 2021年全國高考物理真題試卷及解析(全國已卷)
- 期末試卷(試題)-2024-2025學(xué)年四年級上冊數(shù)學(xué)滬教版
- 綜合實(shí)踐項(xiàng)目 制作水族箱飼養(yǎng)淡水魚 教學(xué)設(shè)計(jì)-2024-2025學(xué)年魯科版生物六年級上冊
- 安徽省2024年高考語文模擬試卷及答案5
- 關(guān)于餐飲合同范本
- CHT 4019-2016 城市政務(wù)電子地圖技術(shù)規(guī)范(正式版)
- 廣西壯族自治區(qū)南寧市2024年七年級下學(xué)期語文期末試卷附答案
- 微量注射泵安全使用和維護(hù)保養(yǎng)
- 建設(shè)用地土壤污染風(fēng)險(xiǎn)評估技術(shù)導(dǎo)則(HJ 25.3-2019代替HJ 25.3-2014)
- 高二物理人教版(2019)選擇性必修第一冊教學(xué)設(shè)計(jì)
- JJG 692-2010無創(chuàng)自動測量血壓計(jì)
評論
0/150
提交評論