用java实现简易计算器

本篇参考了网上两篇资料尤其是栈推

Calculator类

package calframe;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Calculator extends JFrame implements Tetx{
    JPanel mianban;//面板
    JTextArea text;
    JButton j0,j1,j2,j3,j4,j5,j6,j7,j8,j9;
    JButton add,sqrt,subtract,mul,div,squ,cos,bot,deng,back,qing,sin,tan,yu;
    private Container panel;
    public Calculator()
    {
        this.setLayout(new BorderLayout());
j0=new JButton("0");j1=new JButton("1");j2=new JButton("2");j3=new JButton("3");j4=new JButton("4");
        j5=new JButton("5");
        j6=new JButton("6");
        j7=new JButton("7");
        j8=new JButton("8");
        j9=new JButton("9");
   
    bot=new JButton(".");
    bot.setForeground(Color.red);
    sqrt=new JButton("sqrt");
    sqrt.setForeground(Color.red);
    squ=new JButton("x^2");
    yu=new JButton("%");
    cos=new JButton("cos");
    cos.setForeground(Color.red);
    sin=new JButton("sin");
    sin.setForeground(Color.green);
    tan=new JButton("tan");
    tan.setForeground(Color.yellow);
    deng=new JButton("=");
   add=new JButton("+");
   subtract=new JButton("-");
   mul=new JButton("*");
   div=new JButton("/");
   qing=new JButton("c");
   qing.setForeground(Color.red);//设置按键颜色
   back=new JButton("back");
  //定义按钮
   
      text=new JTextArea(2,10);//文本域
      mianban=new JPanel();
     mianban.setLayout(new GridLayout(6,4));
     mianban.add(yu);mianban.add(cos);mianban.add(sin);mianban.add(tan);//面板上端第一行
     
     mianban.add(qing);mianban.add(div);mianban.add(mul);mianban.add(back);//面板上端第二行
     
     mianban.add(j7); mianban.add(j8); mianban.add(j9); mianban.add(subtract);
     
     mianban.add(j4);mianban.add(j5);mianban.add(j6);mianban.add(add);
     
     mianban.add(j1);mianban.add(j2);mianban.add(j3);mianban.add(sqrt);
     
     mianban.add(squ);mianban.add(j0);mianban.add(bot);mianban.add(deng);
     this.add(text,BorderLayout.NORTH);//把text放在最上方
        this.add(mianban);
     setVisible(true);//设置为可设置的
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     Event();//不同键触发的事件
    }
    
    public void Text(JButton button){
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                
                String str = button.getText();
                text.setText(text.getText()+str);
            }
        });    
    }
    private void Event() {    
      Text(j0);Text(j1);Text(j2);Text(j3); Text(j4); Text(j5); Text(j6);Text(j7);Text(j8);Text(j9);
      Text(bot);Text(add);Text(subtract);Text(mul); Text(div); Text(yu);//需要显示在文本域的按键
       
      cos.addActionListener(new ActionListener()
              {
                @Override
                public void actionPerformed(ActionEvent e) {    
                    String str=text.getText();
                    double n;
                    n=Double.parseDouble(str);
                    n=Math.cos(n*Math.PI/180);
                    text.setText(""+n);
                }
              }
              );
      sin.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e) {
            
            String str=text.getText();
            double n;
            n=Double.parseDouble(str);
            n=Math.sin(n*Math.PI/180);
            text.setText(""+n);
        }
      }
      );
      tan.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e) {    
            String str=text.getText();
            double n;
            n=Double.parseDouble(str);
            n=Math.tan(n*Math.PI/180);
            text.setText(""+n);
        }
      }
      );
      qing.addActionListener(new ActionListener()
              {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    text.setText("");
                }
              }
              );
      back.addActionListener(new ActionListener()//往后倒退一格
              {
          public void actionPerformed(ActionEvent arg0) {
                String str=text.getText(),ch;
                ch=str.substring(0, str.length()-1);//将其长度减1,即从0开始检索到-1位
              text.setText(""+ch);    
            }
              }
              );
      
      sqrt.addActionListener(new ActionListener()
              {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    
                    String str=text.getText();
                    double n;
                    n=Double.parseDouble(str);
                    n=Math.sqrt(n);//利用函数进行开方
                text.setText(""+n);
                }
              }
              );
      squ.addActionListener(new ActionListener()
              {

                //按钮事件
                public void actionPerformed(ActionEvent e) {
                    String str=text.getText();
                    double n;
                    n=Double.parseDouble(str);
                    n*=n;
                    text.setText(""+n);        
                }
              }
              );
    deng.addActionListener(new ActionListener()
            {
        public void actionPerformed(ActionEvent e)
        {
            String str=text.getText(),str0="";//利用str字符串存取text1内容
            char ch,c;
            int i=0,top1=-1,top2=-1,j=-1,flog=0,l,str1[]=new int[120];
            double  t,suz[]=new double[122],cha,cha1[]=new double[120];
            l=str.length();
            c=str.charAt(0);
            if(c=='-')
            {
                t=0;
                   suz[++top1]=t;
            }//如果第一个为负号就在前面加0
            for( i=0;i<l;i++)
            {
                ch=str.charAt(i);
                if(ch>='0'&&ch<='9'||ch=='.')//先保存一个数字
                {
                    str0=str0+ch;flog=1;//设计flog为1让接下来判断进行存数字
                }
                else
                {
                    if(flog==1)//把保存的数字字符串转成数字,并放入数字栈中
                    {
                       t=Double.parseDouble(str0);
                       suz[++top1]=t;
                       flog=0; str0="";//注意初始化
                    }
                    if(ch=='+'||ch=='-')//符号的优先级
                        cha=1;
                    else
                        cha=2;    //优先级更大
                    
                    if(top2>=0&&cha1[top2]>=cha)//满足条件时计算,满足此时的优先级大于之前的
                    {
                        if(str1[j]==1)
                            suz[top1-1]=suz[top1]+suz[top1-1];
                        if(str1[j]==2)
                            suz[top1-1]=suz[top1-1]-suz[top1];
                        if(str1[j]==3)
                            suz[top1-1]=suz[top1]*suz[top1-1];
                        if(str1[j]==4)
                            suz[top1-1]=suz[top1-1]/suz[top1];
                        top2--;top1--;
                        j--;i--;    
                    }
                    else
                    {
                    cha1[++top2]=cha;//把符号优先级大的放入符号栈中
                    if(ch=='+')
                        str1[++j]=1;// 把符号保存在符号栈中,1表示符号为+,下面同理
                    if(ch=='-')
                        str1[++j]=2;
                    if(ch=='*')
                        str1[++j]=3;
                    if(ch=='/')
                        str1[++j]=4;
                    if(ch=='%')
                        str1[++j]=5;
                     }
                    
                }
                
            }
            t=Double.parseDouble(str0);//最后一个是数字
               ++top1;suz[top1]=t;
               
               for(;j>=0;j--)//计算最后的结果
               {
                   if(str1[j]==1) 
                    suz[top1-1]=suz[top1]+suz[top1-1];
                if(str1[j]==2)
                    suz[top1-1]=suz[top1-1]-suz[top1];
                if(str1[j]==3)
                    suz[top1-1]=suz[top1]*suz[top1-1];
                if(str1[j]==4)
                    suz[top1-1]=suz[top1-1]/suz[top1];
                if(str1[j]==5)
                    suz[top1-1]=suz[top1-1]%suz[top1];
                top1--;
               }
               text.setText(""+suz[0]);
        }
            }
            );
    }
}

 

Main类可以自由定义

package calframe;

public class Demo {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Calculator Calculator = new Calculator();
        Calculator.setBounds(400, 300, 500, 400);
        Calculator.setTitle("XXX的计算器");
        
    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值