Java语言程序设计——计算器的实现(简单易学版).doc

上传人:飞****2 文档编号:78745797 上传时间:2023-03-19 格式:DOC 页数:12 大小:40KB
返回 下载 相关 举报
Java语言程序设计——计算器的实现(简单易学版).doc_第1页
第1页 / 共12页
Java语言程序设计——计算器的实现(简单易学版).doc_第2页
第2页 / 共12页
点击查看更多>>
资源描述

《Java语言程序设计——计算器的实现(简单易学版).doc》由会员分享,可在线阅读,更多相关《Java语言程序设计——计算器的实现(简单易学版).doc(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、基本功能都实现了,配置一下环境就可以运行。本人初学程序做得非常简单,不足之处很多,纯属交流。主要思想就是利用RPN算法将中缀表达式转成后缀表达式(InToPost、OpToken),然后定义一个类(PostfixExp_Eval)计算后缀表达式。GUI什么的自己设计吧,我设计的很丑话不多说,上代码!第一个Java文件:Calculator.javapackage calculator;import java.awt.*;import java.awt.event.*;import javax.swing.JButton;public class Calculatorpublic static

2、void main(String args)MyPanel mypanel = new MyPanel();mypanel.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););mypanel.setVisible(true);class MyPanel extends Frame/private static final long serialVersionUID = 1L;private Label labExpression;private TextFie

3、ld input;private JButton btnNum = new JButton10;private JButton btnOpAdd = new JButton(+);private JButton btnOpDel = new JButton(-);private JButton btnOpMin = new JButton(*);private JButton btnOpDiv = new JButton(/);private JButton btnOpDot = new JButton(.);private JButton btnOpEql = new JButton(=);

4、private JButton btnOpPow = new JButton();private JButton btnOpC = new JButton(C);private JButton btnOpCE = new JButton(CE);private JButton btnOpLB = new JButton();private JButton btnOpRB = new JButton();private StringBuffer inexp = new StringBuffer();private boolean overwritable = false;private clas

5、s Btnclick implements ActionListenerpublic void actionPerformed(ActionEvent e)String operators = (+-*/);if(operators.indexOf(e.getActionCommand().charAt(0) != -1)inexp.append(input.getText();inexp.append(e.getActionCommand();labExpression.setText(inexp.toString();input.setText();overwritable = true;

6、elseif(overwritable)overwritable = false;input.setText();input.setText(input.getText() + e.getActionCommand();private class Cclick extends MouseAdapterpublic void mouseClicked(MouseEvent e)input.setText();inexp.delete(0, inexp.length();private class CEclick extends MouseAdapterpublic void mouseClick

7、ed(MouseEvent e)input.setText();String operators = +-*/;if(inexp.length()1 & operators.indexOf(inexp.charAt(inexp.length()-1) != -1)inexp = inexp.deleteCharAt(inexp.length()-1);labExpression.setText(inexp.toString();private class Eqlclick extends MouseAdapterpublic void mouseClicked(MouseEvent e)ine

8、xp.append(input.getText();labExpression.setText(inexp.toString();InToPost inE = new InToPost(inexp.toString();PostfixExp_Eval exp = new PostfixExp_Eval(inE.infix_postfix();trylabExpression.setText(labExpression.getText() + =);input.setText( + exp.Evaluate();catch(Exception e1)labExpression.setText(W

9、arnning- + e1.getMessage();finallyoverwritable = true;inexp.delete(0, inexp.length();private void com_init()labExpression = new Label();labExpression.setSize(300, 10);input = new TextField();Font myfont1 = new Font(Lucida Sans, Font.PLAIN, 24);Font myfont2 = new Font(Lucida Sans, Font.BOLD, 12);Btnc

10、lick bc = new Btnclick();input.setFont(myfont1);labExpression.setFont(myfont2);for(int i = 0; i0; i-)int x = (i-1) % 3;int y = 6 - (i-1) / 3);c.gridx = x;c.gridy = y;/c.fill = GridBagConstraints.BOTH;gridbag.setConstraints(btnNumi, c);add(btnNumi);c.gridx = 3;c.gridy = 4;/c.fill = GridBagConstraints

11、.BOTH;gridbag.setConstraints(btnOpAdd, c);add(btnOpAdd);c.gridx = 3;c.gridy = 5;/c.fill = GridBagConstraints.BOTH;gridbag.setConstraints(btnOpCE, c);add(btnOpCE);c.gridx = 3;c.gridy = 6;/c.fill = GridBagConstraints.BOTH;gridbag.setConstraints(btnOpC, c);add(btnOpC);c.gridx = 3;c.gridy = 7;/c.fill =

12、GridBagConstraints.BOTH;gridbag.setConstraints(btnOpEql, c);add(btnOpEql);c.gridx = 0;c.gridy = 7;/c.fill = GridBagConstraints.BOTH;c.gridheight = 1;c.gridwidth = 2;gridbag.setConstraints(btnNum0, c);add(btnNum0);c.gridx = 2;c.gridy = 7;c.gridheight = 1;c.gridwidth = 1;/c.fill = GridBagConstraints.B

13、OTH;gridbag.setConstraints(btnOpDot, c);add(btnOpDot);%分割线%第二个Java文件:InToPost.javapackage calculator;import java.util.Stack;enum charclass Operand, Operator, Open, Close, Space class InToPost private String infixExp = ;private String postfixExp = ;private Stack opStack = new Stack();private static c

14、harclass charType(char c)charclass type = charclass.Operand;String operators = +-*/$;if(operators.indexOf(c) != -1)type = charclass.Operator;else if(c = ()type = charclass.Open;else if(c = )type = charclass.Close;else if(java.lang.Character.isWhitespace(c)type = charclass.Space;return type;public St

15、ring infix_postfix()char BLANK = ;char top_char;OpToken op1, op2, temp = new OpToken($);opStack.push(temp);for(int i = 0; i= op1.Geticp()opStack.pop();top_char = op2.GetOp();postfixExp += top_char;opStack.push(op1);postfixExp += BLANK;break;case Operand:postfixExp += ch;break;case Open:op1 = new OpT

16、oken(ch);opStack.push(op1);break;case Close:postfixExp += BLANK;while(!opStack.empty()op1 = opStack.pop();top_char = op1.GetOp();if(top_char = ()break;postfixExp += top_char;break;while(!opStack.empty()op1 = opStack.pop();top_char = op1.GetOp();postfixExp += top_char;return postfixExp;public InToPos

17、t(String str)infixExp = str;%分割线%第三个Java文件:OpToken.javapackage calculator;class OpToken private char opChar;private int icp, isp;public OpToken()public OpToken(char ch)opChar = ch;switch(opChar)case $:isp = -1; break;case +:case -:icp = 1; isp = 1; break;case *:case /:icp = 2; isp = 2; break;case :i

18、cp = 4; isp = 3; break;case (:isp = 0;public char GetOp() return opChar;public int Getisp() return isp;public int Geticp() return icp;%分割线%第四个Java文件:PostfixExp_Eval.javapackage calculator;import java.io.*;import java.util.Stack;class Expression_error extends Exception/异常类private static final long se

19、rialVersionUID = 1L;Expression_error(String s)super(s);public class PostfixExp_Evalprivate String postfixExp = ;private Stack opndStack = new Stack();/操作数栈private Double doOperator(char op) throws Expression_errorDouble x = new Double(0.0), y = new Double(0.0), z = new Double(0.0);if(!opndStack.empt

20、y()y = opndStack.pop();if(!opndStack.empty()x = opndStack.pop();elsethrow new Expression_error(Lack of operand);switch(op)case +:z = x + y;break;case -:z = x - y;break;case *:z = x * y;break;case /:if(y = 0.0)throw new Expression_error(Expression_error: Divised by Zero!);z = x / y;break;case :if(x =

21、 0.0 & y = 0.0)throw new Expression_error(Expression_error: No Defined of 00!); z = 1.0;while(y 0.0)z *= x;y-;break;return z;public PostfixExp_Eval(String str)postfixExp = str;public double Evaluate() throws IOException, Expression_errorchar opch = ;double value = 0.0;StreamTokenizer in = new Stream

22、Tokenizer(new StringReader(postfixExp);mentChar($);in.ordinaryChars(,/);in.parseNumbers();in.nextToken();while(in.ttype != StreamTokenizer.TT_EOF)if(in.ttype = StreamTokenizer.TT_NUMBER)opndStack.push(in.nval);elseopch = (char)in.ttype;opndStack.push(doOperator(opch);in.nextToken();value = opndStack.pop().doubleValue();return value;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁