您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 编译原理-简单计算器程序设计实验
1.实验目的:用户可以直接输入表达式(没有空格),遇到等号后说明输入结束,输出运算结果,达到计算的目的。2.实验要求:设计、实现一个简单的计算器程序。3.实验环境:一台配置为1G的XP操作系统的PC机,myeclipse9.0.4.实验原理:用java语言编写界面和后台简单的计算处理。5.软件设计与编程:首先编写好计算器各按键的布局和界面;编写好后台简单的计算事务处理程序;在上部的文本框中显示计算过程和结果。对应源程序代码如下:packagejisuanqi;importjava.util.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.applet.*;publicclassCalculatorextendsJAppletimplementsActionListener{booleani=true;privateJButtonnum0=newJButton(0);privateJButtonnum1=newJButton(1);privateJButtonnum2=newJButton(2);privateJButtonnum3=newJButton(3);privateJButtonnum4=newJButton(4);privateJButtonnum5=newJButton(5);privateJButtonnum6=newJButton(6);privateJButtonnum7=newJButton(7);privateJButtonnum8=newJButton(8);privateJButtonnum9=newJButton(9);privateJButtonzuok=newJButton(();privateJButtonyouk=newJButton());privateJButtondian=newJButton(.);privateJButtonNULL=newJButton(N);privateJButtonplu=newJButton(+);privateJButtonmin=newJButton(-);privateJButtonmul=newJButton(x);privateJButtondiv=newJButton(/);privateJButtonequ=newJButton(=);privateJButtoncle=newJButton(C);//清除privateJTextFieldspace=newJTextField(20);publicvoidinit(){JPaneltext=newJPanel();text.setLayout(newFlowLayout());text.add(space);JPanelbuttons=newJPanel();buttons.setLayout(newGridLayout(5,4));buttons.add(num7);buttons.add(num8);buttons.add(num9);buttons.add(div);buttons.add(num4);buttons.add(num5);buttons.add(num6);buttons.add(mul);buttons.add(num1);buttons.add(num2);buttons.add(num3);buttons.add(min);buttons.add(num0);buttons.add(cle);buttons.add(NULL);buttons.add(plu);buttons.add(zuok);buttons.add(youk);buttons.add(dian);buttons.add(equ);(num9).addActionListener(this);(num8).addActionListener(this);(num7).addActionListener(this);(num6).addActionListener(this);(num5).addActionListener(this);(num4).addActionListener(this);(num3).addActionListener(this);(num2).addActionListener(this);(num1).addActionListener(this);(num0).addActionListener(this);(plu).addActionListener(this);(min).addActionListener(this);(mul).addActionListener(this);(div).addActionListener(this);(equ).addActionListener(this);(cle).addActionListener(this);(zuok).addActionListener(this);(youk).addActionListener(this);(dian).addActionListener(this);setLayout(newBorderLayout());add(North,text);add(South,buttons);space.setText(0);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==num9){if(i==true){space.setText(9);i=false;}elsespace.setText(space.getText()+'9');}if(e.getSource()==num8){if(i==true){space.setText(8);i=false;}elsespace.setText(space.getText()+'8');}if(e.getSource()==num7){if(i==true){space.setText(7);i=false;}elsespace.setText(space.getText()+'7');}if(e.getSource()==num6){if(i==true){space.setText(6);i=false;}elsespace.setText(space.getText()+'6');}if(e.getSource()==num5){if(i==true){space.setText(5);i=false;}elsespace.setText(space.getText()+'5');}if(e.getSource()==num4){if(i==true){space.setText(4);i=false;}elsespace.setText(space.getText()+'4');}if(e.getSource()==num3){if(i==true){space.setText(3);i=false;}elsespace.setText(space.getText()+'3');}if(e.getSource()==num2){if(i==true){space.setText(2);i=false;}elsespace.setText(space.getText()+'2');}if(e.getSource()==num1){if(i==true){space.setText(1);i=false;}elsespace.setText(space.getText()+'1');}if(e.getSource()==num0){if(i==true){space.setText(0);i=false;}elsespace.setText(space.getText()+'0');}if(e.getSource()==zuok){if(i==true){space.setText(();i=false;}elsespace.setText(space.getText()+'(');}if(e.getSource()==youk){if(i==false)space.setText(space.getText()+')');}if(e.getSource()==dian){if(i==false)space.setText(space.getText()+'.');}if(e.getSource()==plu){space.setText(space.getText()+'+');i=false;}if(e.getSource()==min){space.setText(space.getText()+'-');i=false;}if(e.getSource()==mul){space.setText(space.getText()+'*');i=false;}if(e.getSource()==div){space.setText(space.getText()+'/');i=false;}if(e.getSource()==equ){space.setText(String.valueOf(Calculator(space.getText())));i=true;}if(e.getSource()==cle){space.setText(0);i=true;}}publicdoubleCalculator(Stringf)//科学计算{inti=0,j=0;charc;StringBuffers=newStringBuffer();s.append(f);s.append('=');Stringformula=s.toString();char[]anArray;anArray=newchar[50];StackCharactermystack=newStackCharacter();while(formula.charAt(i)!='='){c=formula.charAt(i);switch(c){case'(':mystack.push(newCharacter(c));i++;break;case')':while(mystack.peek().charValue()!='('){anArray[j++]=mystack.pop().charValue();}mystack.pop();i++;break;case'+':case'-':while(!mystack.empty()&&mystack.peek().charValue()!='('){anArray[j++]=mystack.pop().charValue();}mystack.push(newCharacter(c));i++;break;case'*':case'/':while(!mystack.empty()&&(mystack.peek().charValue()=='*'||mystack.peek().charValue()=='/')){anArray[j++]=mystack.pop().charValue();}mystack.push(newCharacter(c));i++;break;case'':i++;break;default:while((c='0'&&c='9')||c=='.'){anArray[j++]=c;i++;c=formula.charAt(i);}anArray[j++]='#';break;}}while(!(mystack.empty()))anArray[j++]=mystack.pop().charValue();i=0;intcount;doublea,b,d;StackDoublemystack1=
本文标题:编译原理-简单计算器程序设计实验
链接地址:https://www.777doc.com/doc-6131745 .html