欢迎来到天天文库
浏览记录
ID:1357208
大小:67.50 KB
页数:12页
时间:2017-11-10
《java语言程序设计——计算器的实现(简单易学版)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、基本功能都实现了,配置一下环境就可以运行。本人初学~~程序做得非常简单,不足之处很多,纯属交流。主要思想就是利用RPN算法将中缀表达式转成后缀表达式(InToPost、OpToken),然后定义一个类(PostfixExp_Eval)计算后缀表达式。GUI什么的自己设计吧,我设计的很丑……话不多说,上代码!!!第一个Java文件:Calculator.javapackagecalculator;importjava.awt.*;importjava.awt.event.*;importjavax.swing.JButton;publiccl
2、assCalculator{publicstaticvoidmain(String[]args){MyPanelmypanel=newMyPanel();mypanel.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});mypanel.setVisible(true);}}classMyPanelextendsFrame{//privatestaticfinallongserialVersionUID=1
3、L;privateLabellabExpression;privateTextFieldinput;12privateJButtonbtnNum[]=newJButton[10];privateJButtonbtnOpAdd=newJButton("+");privateJButtonbtnOpDel=newJButton("-");privateJButtonbtnOpMin=newJButton("*");privateJButtonbtnOpDiv=newJButton("/");privateJButtonbtnOpDot=newJ
4、Button(".");privateJButtonbtnOpEql=newJButton("=");privateJButtonbtnOpPow=newJButton("^");privateJButtonbtnOpC=newJButton("C");privateJButtonbtnOpCE=newJButton("CE");privateJButtonbtnOpLB=newJButton("(");privateJButtonbtnOpRB=newJButton(")");privateStringBufferinexp=newStr
5、ingBuffer();privatebooleanoverwritable=false;privateclassBtnclickimplementsActionListener{publicvoidactionPerformed(ActionEvente){Stringoperators="(+-*/^)";if(operators.indexOf(e.getActionCommand().charAt(0))!=-1){inexp.append(input.getText());inexp.append(e.getActionComma
6、nd());labExpression.setText(inexp.toString());input.setText("");overwritable=true;}else{if(overwritable){overwritable=false;input.setText("");}input.setText(input.getText()+e.getActionCommand());}}}privateclassCclickextendsMouseAdapter12{publicvoidmouseClicked(MouseEvente)
7、{input.setText("");inexp.delete(0,inexp.length());}}privateclassCEclickextendsMouseAdapter{publicvoidmouseClicked(MouseEvente){input.setText("");Stringoperators="+-*/^";if(inexp.length()>1&&operators.indexOf(inexp.charAt(inexp.length()-1))!=-1){inexp=inexp.deleteCharAt(ine
8、xp.length()-1);labExpression.setText(inexp.toString());}}}privateclassEqlclickextendsMous
此文档下载收益归作者所有