当前位置:编程学习 > JAVA >>

怎么用做一个java计算器

要求:
使用Scanner就是从控制台输出一串字符    然后判断格式是否是正确
根据运算符的优先级别   有括号的先运算括号。。。。
求代码 --------------------编程问答-------------------- 网上有代码,用到栈的。自己动手,丰衣足食。 --------------------编程问答--------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
//计算器

public class calculatortest extends WindowAdapter implements ActionListener
{
 Frame f;
 MenuBar mb;
 Menu me,ms,mh;
 //TextField tf;
 JTextField tf;
 Panel p1,p2;
 Button bc1,bm1,bm2,bt1,bc2;             //5个功能按钮
 Button b7,b8,b9,bc,bs,b4,b5,b6,bx1,bb,b1,b2,b3,bj1,bx2,b0,bf1,bd,bj2,bf2;
 String m,M,s,temp,SHU,H;
  int x=0,h;
  char a;//记录加减乘除
  int b;//记录等号
  double n,i,j,d;
 public void display()
 {
  f=new Frame("计算器");
  f.setSize(300,300);
  f.setLocation(400,400);
  f.setLayout(new BorderLayout());
  p1=new Panel();
  p2=new Panel();
  
  tf=new JTextField("0",27);
  tf.setHorizontalAlignment(JTextField.RIGHT);
  p1.add(tf);           //添加文本行
  tf.setEditable(false);
  p2=new Panel(new GridLayout(5,5,3,3));    //25个按钮
  //以下定义25个按钮
  bc1=new Button("");
  bc1.setEnabled(false);//不可用
  bm1=new Button("MR");//记忆
  bm2=new Button("MC");
  bt1=new Button("退格");
  bc2=new Button("CE");
  b7=new Button("7");
         b8=new Button("8");
  b9=new Button("9");
  bc=new Button("/");
  bs=new Button("sqrt");
  b4=new Button("4");
  b5=new Button("5");
  b6=new Button("6");
  bx1=new Button("*");
  bb=new Button("%");
  b1=new Button("1");
  b2=new Button("2");
  b3=new Button("3");
  bj1=new Button("-");
  bx2=new Button("1/x");
  b0=new Button("0");
  bf1=new Button("+/-");
  bd=new Button(".");
  bj2=new Button("+");
  bf2=new Button("=");
  //以下为添加按钮
  p2.add(bc1);
  p2.add(bm1);
  p2.add(bm2);
  p2.add(bt1);
  p2.add(bc2);
  p2.add(b7);
  p2.add(b8);
  p2.add(b9);
  p2.add(bc);
  p2.add(bs);
  p2.add(b4);
  p2.add(b5);
  p2.add(b6);
  p2.add(bx1);
  p2.add(bb);
  p2.add(b1);
  p2.add(b2);
  p2.add(b3);
  p2.add(bj1);
  p2.add(bx2);
  p2.add(b0);
  p2.add(bf1);
  p2.add(bd);
  p2.add(bj2);
  p2.add(bf2);
  f.add(p1,BorderLayout.NORTH);
  f.add(p2,BorderLayout.CENTER);
  //以下为25个按钮注册监听程序
  bc1.addActionListener(this);
  bm1.addActionListener(this);
  bm2.addActionListener(this);
  bt1.addActionListener(this);
  bc2.addActionListener(this);
  b7.addActionListener(this);
  b8.addActionListener(this);
  b9.addActionListener(this);
  bc.addActionListener(this);
  bs.addActionListener(this);
  b4.addActionListener(this);
  b5.addActionListener(this);
  b6.addActionListener(this);
  bx1.addActionListener(this);
  bb.addActionListener(this);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
  bj1.addActionListener(this);
  bx2.addActionListener(this);
  b0.addActionListener(this);
  bf1.addActionListener(this);
  bd.addActionListener(this);
  bj2.addActionListener(this);
  bf2.addActionListener(this);
  f.addWindowListener(this);
  addmyMenu();//添加菜单
  f.setVisible(true);
 }
 public void addmyMenu()
 {
  mb=new MenuBar();
  f.setMenuBar(mb);
  me=new Menu("编辑(E)");
  ms=new Menu("查看(V)");
  mh=new Menu("帮助(H)");
  mb.add(me);
  mb.add(ms);
  mb.add(mh);
 }
 public void windowClosing(WindowEvent e)
 {
  System.exit(0);
 }
 
 public void actionPerformed(ActionEvent e)
 {
  
  if(e.getSource()==b1||e.getSource()==b2||e.getSource()==b3||e.getSource()==b4||e.getSource()==b5||e.getSource()==b6||e.getSource()==b7||e.getSource()==b8||e.getSource()==b9||e.getSource()==b0)
  {
  if(x==0){
   m=tf.getText();
   if(m.startsWith("0"))
           tf.setText(e.getActionCommand());
    else
     tf.setText(m+e.getActionCommand());}
       if(x==1){
        tf.setText("");
        tf.setText(tf.getText()+e.getActionCommand());}
     
            
     }
     if(e.getSource()==bc2)//归零
     {       b=0;
      tf.setText("0");
     }
     /*if(e.getSource()==bm1)
     {
      M=tf.getText();
     }
     if(e.getSource()==bm2)
     {
      tf.setText(M);
     }*/
     if(e.getSource()==bd)//小数点
     {
      if(tf.getText().indexOf('.')==0)
            tf.setText(tf.getText()+e.getActionCommand());
      else
         ;
     }
     if(e.getSource()==bj1||e.getSource()==bj2||e.getSource()==bc||e.getSource()==bb||e.getSource()==bx1)//加减乘除余
     {       
      a=e.getActionCommand().charAt(0);
      x=1;
      M=tf.getText();}
      /*if(temp.startsWith("0"))
                   tf.setText("0");
              else
                   ;
      
      SHU=tf.getText();
      tf.setText("0");
     }*/
     if(e.getSource()==bf2)//等号
     {       
      
      H=tf.getText();
      x=0;
      i=Double.parseDouble(M);
      j=Double.parseDouble(H
      );
      switch(a)
      {
       case '+':n=i+j;tf.setText(Double.toString(n));break;
       case '-':n=i-j;tf.setText(Double.toString(n));break;
       case '*':n=i*j;tf.setText(Double.toString(n));break;
       case '/':n=i/j;tf.setText(Double.toString(n));break;
       case '%':n=i%j;tf.setText(Double.toString(n));break;
      }
     }
     if(e.getSource()==bs)//sqrt
     {
      d=Double.parseDouble(tf.getText());
      d=Math.sqrt(d);
      //m=Double.toString(d);
      tf.setText(m=Double.toString(d));
     }
     if(e.getSource()==bf1)//取反
     {
      d=Double.parseDouble(tf.getText());
      d=-d;
      tf.setText(d+"");
     }
     
     if(e.getSource()==bx2)//取倒数
     {
          n=Double.parseDouble(tf.getText());
          n=1/n;
          tf.setText(Double.toString(n));
      }
      if(e.getSource()==bt1)//退格
      {      
       m=tf.getText();
       if(m.startsWith("0"))
              ;
             if(m.length()==1)
          tf.setText("0");
         if(m.length()>1) 
         {
          //m=tf.getText();
        tf.setText(m.substring(0,m.length()-1));
       }
        }
               
 }
 
     public static void main(String args[])
     {
      (new calculatortest()).display();
     }
     
}

--------------------编程问答-------------------- 没看懂题目意思  谢谢。。。。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,