计算器JAVA.doc_第1页
计算器JAVA.doc_第2页
计算器JAVA.doc_第3页
计算器JAVA.doc_第4页
计算器JAVA.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.*;import javax.swing.*;import java.awt.event.*;public class Jsq extends JFrame implements ActionListener JTextField text = new JTextField(10); /创建单行文本框String s = ; boolean i=true;public Jsq() JLabel w1=new JLabel(计算器,JLabel.CENTER); /创建标签Container con=getContentPane();/获取窗口内容面板con.setLayout(new BorderLayout();/设置边界布局管理器JPanel p1 = new JPanel();/创建面板JPanel p2 = new JPanel();JButton b1 = new JButton(1);/创建按钮JButton b2 = new JButton(2);JButton b3 = new JButton(3);JButton b4 = new JButton(4);JButton b5 = new JButton(5);JButton b6 = new JButton(6);JButton b7 = new JButton(7);JButton b8 = new JButton(8);JButton b9 = new JButton(9);JButton b10 = new JButton(0);JButton b11 = new JButton(+);JButton b12 = new JButton(-);JButton b13 = new JButton(*);JButton b14 = new JButton(/);JButton b15 = new JButton(=); JButton b16 = new JButton(.); JButton b17 = new JButton(清空); JButton b18 = new JButton(); JButton b19 = new JButton(); p1.setLayout(new BorderLayout();/设置P1p1.add(w1, BorderLayout.NORTH);p1.add(text, BorderLayout.CENTER);p1.add(b17,BorderLayout.SOUTH); p1.add(b18,BorderLayout.WEST); p1.add(b19,BorderLayout.EAST);p2.setLayout(new GridLayout(4,4);/设置P2p2.add(b9);p2.add(b8);p2.add(b7);p2.add(b14);p2.add(b6);p2.add(b5);p2.add(b4);p2.add(b13);p2.add(b3);p2.add(b2);p2.add(b1);p2.add(b12); p2.add(b10); p2.add(b15); p2.add(b16); p2.add(b11); p2.setBackground(Color.black);con.add(p1, BorderLayout.NORTH);/将P1,P2等组件添加入内容面板con.add(p2, BorderLayout.CENTER);setSize(400,600);/设置窗口setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/关闭窗口时终止程序运行b1.addActionListener(this);/注册监听器b2.addActionListener(this);b3.addActionListener(this);b4.addActionListener(this);b5.addActionListener(this);b6.addActionListener(this);b7.addActionListener(this);b8.addActionListener(this);b9.addActionListener(this);b10.addActionListener(this);b11.addActionListener(this);b12.addActionListener(this);b13.addActionListener(this);b14.addActionListener(this);b15.addActionListener(this); b16.addActionListener(this); b18.addActionListener(this); b19.addActionListener(this); b17.addActionListener(new ShanchuHandler();/注册并创建删除监听器 text.setText(0);public void actionPerformed(ActionEvent e) if(e.getActionCommand()= 9) if(i=true) text.setText(9); i=false; else text.setText(text.getText()+9); if(e.getActionCommand()= 8) if(i=true) text.setText(8); i=false; else text.setText(text.getText()+8); if(e.getActionCommand()= 7) if(i=true) text.setText(7); i=false; else text.setText(text.getText()+7); if(e.getActionCommand()= 6) if(i=true) text.setText(6); i=false; else text.setText(text.getText()+6); if(e.getActionCommand()= 5) if(i=true) text.setText(5); i=false; else text.setText(text.getText()+5); if(e.getActionCommand()= 4) if(i=true) text.setText(4); i=false; else text.setText(text.getText()+4); if(e.getActionCommand()= 3) if(i=true) text.setText(3); i=false; else text.setText(text.getText()+3); if(e.getActionCommand()= 2) if(i=true) text.setText(2); i=false; else text.setText(text.getText()+2); if(e.getActionCommand()= 1) if(i=true) text.setText(1); i=false; else text.setText(text.getText()+1); if(e.getActionCommand()= 0) if(i=true) text.setText(0); i=false; else text.setText(text.getText()+0); if(e.getActionCommand()= () if(i=true) text.setText(); i=false; else text.setText(text.getText()+(); if(e.getActionCommand()= ) if(i=false) text.setText(text.getText()+); if(e.getActionCommand()= .) if(i=false) text.setText(text.getText()+.); if(e.getActionCommand()= +) text.setText(text.getText()+); i=false; if(e.getActionCommand()= -) text.setText(text.getText()+-); i=false; if(e.getActionCommand()= *) text.setText(text.getText()+*); i=false; if(e.getActionCommand()= /) text.setText(text.getText()+/); i=false; if(e.getActionCommand()= =) text.setText(String.valueOf(Calculator(text.getText(); i=true; public double Calculator(String f)/科学计算 int i=0,j=0; char c; StringBuffer s=new StringBuffer(); s.append(f); s.append(=); String formula=s.toString(); char anArray; anArray=new char50; Stack mystack=new Stack(); while(formula.charAt(i)!=) c=formula.charAt(i); switch(c) case (: mystack.push(new Character(c); i+; break; case ): while(mystack.peek().charValue()!=() anArrayj+=mystack.pop().charValue(); mystack.pop(); i+; break; case +: case -: while(!mystack.empty()&mystack.peek().charValue()!=() anArrayj+=mystack.pop().charValue(); mystack.push(new Character(c); i+; break; case *: case /: while(!mystack.empty()&(mystack.peek().charValue()=*|mystack.peek().charValue()=/) anArrayj+=mystack.pop().charValue(); mystack.push(new Character(c); i+; break; case : i+; break; default: while(c=0&c=9)|c=.) anArrayj+=c; i+; c=formula.charAt(i); anArrayj+=#; break; while(!(mystack.empty() anArrayj+=mystack.pop().charValue(); i=0; int count; double a,b,d; Stack mystack1 =new Stack(); while(i=0&c=0&c=9) count+; d=d+(c-0)/Math.pow(10,count); i+; c=anArrayi; if(c=#) mystack1.push(new Double(d); i+; break; return(mystack1.peek().doubleValue();private class Shan

温馨提示

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

评论

0/150

提交评论