Java实验报告-图形化界面计算器_第1页
Java实验报告-图形化界面计算器_第2页
Java实验报告-图形化界面计算器_第3页
Java实验报告-图形化界面计算器_第4页
Java实验报告-图形化界面计算器_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

-21-封面/lzj,2015/11/29Java实验报告 --图形化界面计算器 计算机22班 清哥荡漾 2120505047

目录封面/lzj,2015/11/29 -1-目录 -2-一、 实验要求 -3-二、 实验流程 -3-1. 需求分析 -3-2. 概要设计 -3-3. 详细设计 -4-3-1.代码结构 -4-3-2.结构说明 -4-4. 编码实现 -5-5. 测试纠错 -5-三、 实验结果 -6-1. 初始化界面 -6-2. 界面说明 -7-2-1显示部分 -7-2-2控制部分 -8-3. 结果演示 -8-3-1单目运算 -8-3-2双目运算 -9-3-3混合运算 -10-3-4错误演示 -11-3-5保存本地操作结果 -11-3-6上传服务器操作演示 -12-4. 总结结果 -13-四、 总结分析 -13-五、 实验代码 -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-实验要求实现图形化界面计算器,完成基本功能实现保存计算历史纪录到本地的功能实现上传计算历史纪录到服务器的功能实验流程需求分析对于基本功能,要求实现图形化界面计算器,则可参照电脑或手机上的计算器进行页面布局,最后加上各种监听器即可。附加要求实现保存到本地,只需将内存中的历史纪录通过文件读写到本地即可。附加要求实现上传至服务器,可以用TCP或者UDP报文通信实现概要设计 2-1.整体采用手机计算器布局架构 2-2.屏幕显示用文本框实现 2-3.按钮监听自定义ActionListener类 2-4.输入时应限制错误输入 2-5.运算时采用逆波兰式2-6.保存本地功能采用随机读写方式添加至文件尾部2-7.上传采用UDP数据报方式详细设计3-1.代码结构3-2.结构说明 gui包:实现图形化界面 GUI.java:整体架构 DispPanel.java:显示面板部分 CtrlPanel.java:控制面板部分 component包:用到的部件 MyButton.java:自定义JButton类,进行自定义设定 MyTextField.java:自定义JTextField类,用于屏幕显示 actionlistener包:监听器 MyButtonActionListener.java:对按钮进行监听操作,分别进行不同操作 operation包:计算、保存上传操作 ArithmeticLogic.java:获取屏幕输入字符串进行运算后返回结果 SaveOperate.java:保存计算历史到本地 UploadOperate.java:上传计算历史到服务器 Main.java:main函数所在,调用整个工程编码实现 工具:Eclipse 环境:jse1.8 人员:我 耗时:数十小时 经历:小问题有些,debug很煎熬 结果:基本功能完成,保存功能完成,上传功能完成但未验证测试纠错 每一个模块完成后都及时进行纠错处理,最后整体进行大量测试,对发现的错误进行纠正。发现错误包括: 输入限制逻辑错误 运算逻辑错误 异常监听错误 函数调用错误 经过鏖战,干掉bug!实验结果初始化界面界面说明2-1显示部分 屏幕区域由上到下分为三个区域,依次显示: 提示信息:正常显示“Let’sROCK”,发生错误提示错误信息,及其他操作信息 上次运算历史:如图“sin(2.5*pi)=1” 本次运算结果:如图“1.0”2-2控制部分 控制部分整体采用Gridlayout布局进行排列: 按钮布局仿照红米1s的计算器结果演示3-1单目运算3-2双目运算3-3混合运算3-4错误演示3-5保存本地操作结果 注意:采用特定输入序列(88224466!!^^)启动操作3-6上传服务器操作演示 注意:采用特定输入序列(82824646!^!^)启动操作总结结果 基本功能堪称完美,经大量测试不再有未知错误发生,已发现bug已经全部解决 上传操作发送数据报报文,时间原因来不及测试了 保存本地操作没有任何问题,测试结果良好总结分析 通过本次实验,发现了平时许多没有发现的问题。首先此前没有学过java,上课时学到的知识在课后没能及时复习回顾,遗漏了一些很小但是却致命的问题,幸而在本次实验中及时发现,亡羊补牢,结果还大体满意。 理论学习固然重要,实际应用也必不可少,在实践中把学过的知识应用出来才能最大程度的加深自己的学习理解程度,也可以将自己平时没有注意到的错误即使解决掉。 最后,感谢老师的教导!实验代码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;//屏幕中心点x privateintmidScreenY;//屏幕中心点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");//设定图标 setIconImage(icon.getImage()); setTitle("计算器Beta1.0"); setBounds();//调用自定义函数设定边界 setContent();//调用自定义函数设定内容 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); //有边框,保持内部大小故在宽度高度上补全 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));//整体颜色,仿计算器 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); //上次运算 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); //本次运算 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){//只有一个"0" string=""; }elseif(string.charAt(string.length()-2)<'1'&&string.charAt(string.length()-2)>'9'){//"0"之前不是数字 string=string.substring(0,string.length()-1); } } setText(string+str); } } publicvoidremoveEnd(){//删除最后一个字符 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){//各个按钮的功能 //直接在最后面追加本次输入 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; //后跟一个"(" case"√": case"sin": case"cos": case"tan": case"ln": case"log": DispPanel.currentCaculation_TextField.append(string+"("); break; //一个数字只能输入一个"." case".": StringtempStr=DispPanel.currentCaculation_TextField.getText(); charc=tempStr.charAt(tempStr.length()-1); if(c<'0'||c>'9'){//最后不是数字,直接返回 return; } for(inti=0;i<tempStr.length();i++){ c=tempStr.charAt(tempStr.length()-1-i); if(c>='0'&&c<='9'){ continue; }elseif(c=='.'){//数字已经含有小数点,返回 return; }else{//最后一个数字内不含小数点 break; } } DispPanel.currentCaculation_TextField.append("."); break; //运算符问题 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; //运算符问题(可能负号) 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; //计算本次结果,并更新历史计算框 case"=": getResult(); break; } } privatevoidgetResult(){//按"="后的任务 Stringstring=DispPanel.currentCaculation_TextField.getText(); //输入密令,保存计算历史到本地 if(string.equals("88224466!!^^")){ // if(SaveOperate.saveToLocal()){//保存操作 if(newSaveOperate().saveToLocal()){ DispPanel.showHintInfo_TextField.setText("保存成功,,,继续Rock"); }else{ DispPanel.showHintInfo_TextField.setText("保存失败,可怜的娃>>>接着Rock"); } DispPanel.currentCaculation_TextField.setText("0"); } //输入密令,上传到服务器 elseif(string.equals("82824646!^!^")){ if(UploadOperate.uploadToServer()){//保存操作 DispPanel.showHintInfo_TextField.setText("上传成功,,,继续Rock"); }else{ DispPanel.showHintInfo_TextField.setText("上传失败,可怜的娃>>>接着Rock"); } DispPanel.currentCaculation_TextField.setText("0"); } //进行运算,更新上次运算显示框;若算式错误提示错误 else{ Stringresult=ArithmeticLogic.ALFunc(string); if(result.contains("Error")){//算式错误,识别错误码并进行提示 DispPanel.showHintInfo_TextField.setText(result.substring(6)); }else{//运算成功 DispPanel.currentCaculation_TextField.setText(result);//运算结果 string=string+"="+result; DispPanel.lastCaculation_TextField.setText(string); } } }}operation包ArithmeLogic.javapackagecom.lzj.caculator_javaexp.operation;importjava.util.Stack;/**publicstaticStringALFunc(Stringinput){}方法实现运算**>>>运算流程1:将输入的中缀表达式转为后缀表达式,即逆波兰式* * >>>注意:使用一个栈和一个队列,输出的放到队列里;* >>>我晕,怎么搞不定队列??那就俩栈吧,,,(-__-)b**>>>运算流程2:对流程1后的队列进行出队入栈,栈顶运算***>>>注意错误:* >>>扫描input输入串时可能括号不匹配(这叫输入错误)* >>>数字错误(限定小数点输入,故此暂未发现)* >>>运算符错误(暂未发现)* >>>计算逆波兰式时可能除数为0(实测结果为无穷)* >>>数字过大计算结果溢出,应该可以用BigInteger搞定(暂未溢出,java好强大)*>>>bug:* 500行上下所在* 我勒个去了,为毛stack.firstElement不是栈顶,,,好吧,懒了没有自己定义* 偷懒poppush了一次拿到栈顶元素**/publicclassArithmeticLogic{ privatestaticStringinput;//存放输入的算式字符串 privatestaticStringoutput;//最终输出的结果字符串 //运算时使用 privatestaticStack<String>s1; privatestaticStack<String>s2; privatestaticStack<Float>s3; privatestaticStringelem;//当前元素 privatestaticintfence;//字符串当前位置 publicstaticStringALFunc(Stringstring){ input=string; output=""; if(transToRPN()==false){//转换失败 returnoutput; } while(!s2.isEmpty()){//从s2压至s1,转为逆波兰式 s1.push(s2.pop()); } if(caculate()==false){//计算失败 returnoutput; } returnoutput; } //从中缀表达式转换为后缀表达式 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:表达式错误"; returnfalse; case1: returntrue; case2: s2.push(elem); break; case3: if(!dealCurrentOpr()){ returnfalse; } break; default: returnfalse; } } } //计算逆波兰式的结果 privatestaticbooleancaculate(){ s3=newStack<Float>(); while(!s1.isEmpty()){ Stringtemp=s1.pop(); if(temp.charAt(0)>='0'&&temp.charAt(0)<='9'){//运算数 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{//运算符 switch(temp){ case"+": if(s3.size()<2){ output="Error:表达式错误"; returnfalse; } try{ s3.push(s3.pop()+s3.pop()); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"-": if(s3.size()==0){ output="Error:表达式错误"; 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:表达式错误"; returnfalse; } try{ s3.push(s3.pop()*s3.pop()); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"÷": if(s3.size()<2){ output="Error:表达式错误"; returnfalse; } Floattemp1=s3.pop(); if(temp1==0){ output="Error:除数为零"; returnfalse; } try{ s3.push(s3.pop()/temp1); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"^": if(s3.size()<2){ output="Error:表达式错误"; 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:表达式错误"; returnfalse; } Floattemp3=s3.pop(); if(temp3%1!=0||temp3<1){ output="Error:表达式错误"; 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:表达式错误"; 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:表达式错误"; returnfalse; } try{ s3.push((float)Math.sin(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"cos": if(s3.isEmpty()){ output="Error:表达式错误"; returnfalse; } try{ s3.push((float)Math.cos(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"tan": if(s3.isEmpty()){ output="Error:表达式错误"; returnfalse; } try{ s3.push((float)Math.tan(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"ln": if(s3.isEmpty()){ output="Error:表达式错误"; returnfalse; } try{ s3.push((float)Math.log(s3.pop())); break; }catch(Exceptione){ output="Error:"+e.getMessage(); returnfalse; } case"log": if(s3.isEmpty()){ output="Error:表达式错误"; 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; } } /* *从输入字符串内读取一个运算数或运算符 * *>>>返回-1:出现错误>>>返回-2:结尾为操作数>>>返回-3:结尾为操作符>>>返回1:结束>>>返回2:操作数 *>>>返回3:操作符 * */ privatestaticintgetElement(){ charc=input.charAt(fence); if(c>='0'&&c<='9'){//数字 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;//操作数 } 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{//运算符 switch(c){ case'!': case'^': case'√': case'(': case')': case'+': case'-': case'×': case'÷': elem=""+c; if(fence==input.length()-1){ return-3;//结尾是运算符 } fence++; c=input.charAt(fence); return3;//运算符 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;//运算符结尾 } fence++; c=input.charAt(fence); return3;//运算符 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;//运算符结尾 } fence++; c=input.charAt(fence); return3;//运算符 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;//运算符结尾 } fence++; c=input.charAt(fence); return3;//运算符 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;//结尾是运算符 } fence++; c=input.charAt(fence); return3;//运算符 }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;//运算符结尾 } fence++; c=input.charAt(fence); return3; } default: return-1; } } } //转换逆波兰式时对当前操作符进行处理 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{//其他符号,根据优先级不同开始操作 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字符串扫描完毕后,将符号栈内容压入逆波兰式栈 privatestaticvoidpushRemain(){ while(!s1.isEmpty()){ s2.push(s1.pop()); } } //返回当前运算符的优先级 privatestaticintgetPriority(Stringopr){ s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论