首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

JAVA简易计算器(可判断运算符的优先级)制作人:败家子raining

2012-10-28 
JAVA简易计算器(可判断运算符的优先级)制作人:浪子rainingpackage beanimport java.awt.*import java.aw

JAVA简易计算器(可判断运算符的优先级)制作人:浪子raining

package bean;

import java.awt.*;

import java.awt.Image;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

?

import javax.swing.*;

import java.util.*;

public class Calculate extends JFrame implements ActionListener {

??? //??????????????????????????????????????????????????? 10? 11? 12? 13? 14? 15? 16? 17?? 18

?????? String bn[]={"0","1","2","3","4","5","6","7","8","9","+","-","*","/",".","CE","C","=","+/-"};

??? JButton btn[]=new JButton[19];

??? Panel p[]=new Panel[6];

??? TextField text;

??? Box box;

??? FlowLayout fv;

?????? Calculate(String title)

??? {

??? ?????? super(title);

??? ?????? this.setDefaultCloseOperation(EXIT_ON_CLOSE);

??? ?????? box=Box.createVerticalBox();

??? ?????? fv=new FlowLayout();

??? ?????? fv.setVgap(20);

/*

??? ?????? //下面是设置计算器图标的方法:

??? ?????? java.net.URL imgURL = Calculate.class.getResource("images/icon.jpg");?? //绝对路径:"./images/icon.jpg",相对路径:"images/icon.jpg"

??? ?????? ImageIcon imgIcon = new ImageIcon(imgURL);

??? ?????? Image img = imgIcon.getImage();

??? ?????? this.setIconImage(img);

?*/

?? ? ????this.setLocationRelativeTo(null);???????

??? ?????? for(int i=0;i<bn.length;i++)

??? ?????? {

??? ????????????? btn[i]=new JButton(bn[i]);

??? ????????????? btn[i].addActionListener(this);

??? ????????????? if(i!=15&&i!=16)

??? ????????????? {

??? ???????????????????? btn[i].setPreferredSize(new Dimension(55,35));

? ??????????????? }

??? ????????????? else

??? ????????????? {

??? ???????????????????? ??btn[i].setPreferredSize(new Dimension(85,35));

??? ????????????? }

??? ????????????? if(i<6)

??? ????????????? {

??? ???????????????????? p[i]=new Panel();

??? ???????????????????? box.add(p[i]);

??? ????????????? }

??? ????????????? if(i==0)

??? ????????????? {

??? ???????????????????? text=new TextField("0",30);

??? ???????????????????? p[0].add(text);

??? ????????????? }

??? ?????? }

??? ?????? p[1].add(btn[7]);p[1].add(btn[8]);p[1].add(btn[9]);p[1].add(btn[10]);

??? ?????? p[2].add(btn[4]);p[2].add(btn[5]);p[2].add(btn[6]);p[2].add(btn[11]);

??? ?????? p[3].add(btn[1]);p[3].add(btn[2]);p[3].add(btn[3]);p[3].add(btn[12]);

??? ?????? p[4].add(btn[0]);p[4].add(btn[14]);p[4].add(btn[18]);p[4].add(btn[13]);

??? ?????? p[5].add(btn[15]);p[5].add(btn[16]);p[5].add(btn[17]);

??? ?????? this.add(box);

??? ?????? this.setBounds(520,200,280,320);

??? ?????? this.setResizable(false);

??? ?????? this.setVisible(true);

??? ?????? this.validate();

??? }

?????? int press=0;??????????????????????????? //用于判断按钮.的点击次数

?????? int press1=0;?????????????????????????? //用于运算符按钮的点击次数

?????? int press2=0;?????????????????????????? //用于记录+/-按钮的点击次数

?????? int strlen=0;?????????????????????????? //用于记录按下运算符时的字符串长度

?????? StringBuffer str=new StringBuffer("0"); //用于记载整条字符串

?????? StringBuffer str2=new StringBuffer("0"); //用于记载整条字符串

?????? double sum1=0;????????????????????????? //初始化总和

?????? public void actionPerformed(ActionEvent e) {

???? if(e.getSource()==btn[10])???????????? //按钮+

?????? ?{

??? ?????? ?this.yunsf(10);

?????? ?}

???? if(e.getSource()==btn[11])???????????? //按钮-

?????? ?{

??? ?????? ?this.yunsf(11);

?????? ?}

?????? ?if(e.getSource()==btn[12])???????????? //按钮*

???? {

????????????? ?this.yunsf(12);

?????? ?}

?????? ?if(e.getSource()==btn[13])???????????? //按钮/

?????? ?{

??????? this.yunsf(13);

?????? ?}???

?????? ?if(e.getSource()==btn[14])???????????? //按钮点.

?????? ?{

????????????? ?if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&press!=1)

????????????? ?{

???????????????????? ?str.append(btn[14].getText());

???????????????????? ?str2.append(btn[14].getText());

???????????????????? ?text.setText(str.toString());

???????????????????? ?press=1;

????????????? ?}

?????? ?}

?????? ?if(e.getSource()==btn[18])???????????? //按钮+/-

?????? ?{

????????????? ?if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/')

????????????? ?{

???????????????????? ?if(press2==0)

???????????????????? ?{

??????????????????????????? ?str.insert(strlen, "-");

??????????????????????????? ?str2.insert(strlen, "@");?????????????? //////////////////////////////

??????????????????????????? ?text.setText(str.toString());

??????????????????????????? ?press2=1;

???????????????????? ?}

???????????????????? ?else

???????????????????? ?{

??????????????????????????? ?str.deleteCharAt(strlen);

??????????????????????????? ?str2.deleteCharAt(strlen);

??????????????????????????? ?text.setText(str.toString());

??????????????????????????? ?press2=0;

???????????????????? ?}

????????????? ?}

?????? ?}

?????? ?if(e.getSource()==btn[15])???????????? //按钮CE

?????? ?{

????????????? ?if(strlen!=0)

????????????? ?{

????????????? ?? str.delete(strlen, str.length());

????????????? ?? str2.delete(strlen, str2.length());

????????????? ?? text.setText(str.toString());

????????????? ?}

????????????? ?else

????????????? ?{

???????????????????? ?str.delete(0,str.length());

???????????????????? ?str2.delete(0,str2.length());

???????????????????? ?str.append("0");

???????????????????? ?str2.append("0");

???????????????????? ?text.setText(str.toString());

???????????????????? ?press=0;

???????????????????? ?press2=0;

????????????? ?}

?????? ?}

?????? ?if(e.getSource()==btn[16])???????????? //按钮C

???? {

?????? ? ??? str.delete(0, str.length());

?????? ? ??? str2.delete(0, str2.length());

????????????? str.append("0");

????????????? str2.append("0");

????????????? text.setText(str.toString());

????????????? press=0;

????????????? press1=0;

????????????? press2=0;

????????????? strlen=0;

?????? ?}

?????? ?if(e.getSource()==btn[17])???????????? //按钮=

?????? ?{

????????????? ?if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&press!=1)

????????????? ?{

???????????????????? String txt=str2.toString();

????????????? ??? String shu[]=txt.split("\\+|\\-|\\*|\\/");??? //分解出字符串中的数字字符

????????????? ??? String fh[]=txt.split("@\\d+\\\56\\d+|@\\d+|\\d+\\\56\\d+|\\d+");???? //分解出字符串中的运算符

????????????? ??? for(int i=0;i<shu.length;i++)

????????????? ??? {

????????????? ??? ?????? if(shu[i].startsWith("@"))

????????????? ??? ?????? {

????????????? ??? ????????????? shu[i]="-"+shu[i].substring(1);??????????? ??? ?????????????

????????????? ??? ?????? }

????????????? ??? }

????????????? ??? calculate(shu,fh);??? ???

????????????? ?}

?????? ?}

?????? ?if(e.getSource()==btn[0])

?????? ?{

????????????? ?this.shuzi(0);

?????? ?}

?????? ?if(e.getSource()==btn[1])

?????? ?{

????????????? ?this.shuzi(1);

?????? ?}

?????? ?if(e.getSource()==btn[2])

?????? ?{

????????????? ?this.shuzi(2);

?????? ?}

?????? ?if(e.getSource()==btn[3])

?????? ?{

????????????? ?this.shuzi(3);

?????? ?}

?????? ?if(e.getSource()==btn[4])

?????? ?{

????????????? ?this.shuzi(4);

?????? ?}

?????? ?if(e.getSource()==btn[5])

?????? ?{

????????????? ?this.shuzi(5);

?????? ?}

?????? ?if(e.getSource()==btn[6])

?????? ?{

????????????? ?this.shuzi(6);

?????? ?}

?????? ?if(e.getSource()==btn[7])

?????? ?{

????????????? ?this.shuzi(7);

?????? ?}

?????? ?if(e.getSource()==btn[8])

?????? ?{

????????????? ?this.shuzi(8);

?????? ?}

?????? ?if(e.getSource()==btn[9])

?????? ?{

???????? this.shuzi(9);

?????? ?}

??????

?????? }

?????? public void yunsf(int i)????????? //制定运算符按钮规则

?????? {

????????????? ?if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&str.charAt(str.length()-1)!='.')????

????????????? ?{

???????????????????? ?str.append(btn[i].getText());

???????????????????? ?str2.append(btn[i].getText());

???????????????????? ?text.setText(str.toString());

???????????????????? ?press=0;

???????????????????? ?press1=1;

???????????????????? ?press2=0;

???????????????????? ?strlen=str.length();

????????????? ?}

?????? }

??? public void shuzi(int i)????????? //制定数字按钮规则

??? {

?????? ?? if(press1!=1&&press!=1&&str.charAt(0)=='0')

????????????? ?{

???????????????????? ?str.replace(0, 1,btn[i].getText());

???????????????????? ?str2.replace(0, 1,btn[i].getText());

????????????? ???? text.setText(str.toString());

????????????? ?}

????????????? ?else

????????????? ?{

???????????????????? ?str.append(btn[i].getText());

???????????????????? ?str2.append(btn[i].getText());

???????????????????? ?text.setText(str.toString());

????????????? ?}???

??? }

?????? public void calculate(String shu[],String fh[])? //制定运算规则

?????? {

????????????? for(int i=1;i<fh.length;i++)

????????????? {

???????????????????? if(fh[i].equals("*"))

???????????????????? {

??????????????????????????? double sum=0;

??????????????????????????? sum=Double.parseDouble(shu[i-1])*Double.parseDouble(shu[i]);

??????????????????????????? shu[i-1]=Double.toString(0);

??????????????????????????? shu[i]=Double.toString(sum);

??????????????????????????? fh[i]="#";???????????????????????????

???????????????????? }

???????????????????? if(fh[i].equals("/"))

???????????????????? {

??????????????????????????? double sum=0;

??????????????????????????? sum=Double.parseDouble(shu[i-1])/Double.parseDouble(shu[i]);

??????????????????????????? shu[i-1]=Double.toString(0);

??????????????????????????? shu[i]=Double.toString(sum);

??????????????????????????? fh[i]="#";???????????????????????????

???????????????????? }

????????????? }

????????????? for(int i=1;i<fh.length;i++)

????????????? {

???????????????????? if(fh[i].equals("-"))

???????????????????? {

??????????????????????????? double sum=0;

??????????????????????????? int n=i;

??????????????????????????? for(int j=i;j<fh.length;j++)

??????????????????????????? {

?????????????????????????????????? if(fh[j].equals("#"))

?????????????????????????????????? {

????????????????????????????????????????? n++;

?????????????????????????????????? }

??????????????????????????? }

??????????????????????????? sum=Double.parseDouble(shu[i-1])-Double.parseDouble(shu[n]);

??????????????????????????? shu[i-1]=Double.toString(0);

??????????????????????????? shu[n]=Double.toString(sum);

????????????? ????????????? fh[i]="+";

???????????????????? }

????????????? }

????????????? sum1=Double.parseDouble(shu[0]);

????????????? for(int i=1;i<shu.length;i++)

????????????? {

?????? ??? ?????? sum1+=Double.parseDouble(shu[i]);

????????????? }???????????

????????????? text.setText(Double.toString(sum1));???

????????????? str.delete(0, str.length());

????????????? str.append(text.getText());

????????????? strlen=0;

????????????? str2.delete(0, str2.length());

????????????? str2.append(text.getText());

????????????? press2=0;

?????? }

?????? public static void main(String args[])

?????? {

????????????? Calculate cal=new Calculate("JAVA计算器:浪子raining");

?????? }

}

热点排行