创建个计算器的类,类里有加号减号乘号除号的方法,用测试实现,请问高手怎样做,我今天做了很长时间没做出来。

解决方案 »

  1.   

    google下
           难道你 当兵 不会goolge啊
      

  2.   


    public class Arith
    {
    //默认除法运算精度
    private static final int DEF_DIV_SCALE = 10;
    //构造器私有,让这个类不能实例化
    private Arith() {} 
    //加法运算
    public static double add(double v1,double v2)
    {
    BigDecimal b1 = BigDecimal.valueOf(v1);
    BigDecimal b2 = BigDecimal.valueOf(v2);
    return b1.add(b2).doubleValue();
    }

    //精确的减法运算。
    public static double sub(double v1,double v2)
    {
    BigDecimal b1 = BigDecimal.valueOf(v1);
    BigDecimal b2 = BigDecimal.valueOf(v2);
    return b1.subtract(b2).doubleValue();

     //精确的乘法运算。
              public static double mul(double v1,double v2)
    {
    BigDecimal b1 = BigDecimal.valueOf(v1);
    BigDecimal b2 = BigDecimal.valueOf(v2);
    return b1.multiply(b2).doubleValue();


     //(相对)精确的除法运算
              //当发生除不尽的情况时,精确到
     //小数点以后10位的数字四舍五入。
     
    public static double div(double v1,double v2)
    {
    BigDecimal b1 = BigDecimal.valueOf(v1);
    BigDecimal b2 = BigDecimal.valueOf(v2);
    return b1.divide(b2 , DEF_DIV_SCALE , BigDecimal.ROUND_HALF_UP).doubleValue();

    public static void main(String[] args)
    {
    System.out.println("0.05 + 0.01 = " + Arith.add(0.05 , 0.01));
    System.out.println("1.0 - 0.42 = " + Arith.sub(1.0 , 0.42));
    System.out.println("4.015 * 100 = " + Arith.mul(4.015 , 100));
    System.out.println("123.3 / 100 = " + Arith.div(123.3 , 100));
    }
    }
      

  3.   

    import java.io.*;
     class Op
    {
    private float i;
    private float j;
    public Op(float i,float j)
    {
    this.i=setI(i);
    this.j=setJ(j);
    }
    public float setI(float i)
    {
    return i;
    }
    public float setJ(float j)
    {
    return j;
    }
    public void plus()
    {
    System.out.println("i+j = "+(i+j));
    }
    public void sub()
    {
    System.out.println("i-j = "+(i-j));
    }
    public void mul()
    {
    System.out.println("i*j = "+(i*j));
    }
    public void div() 
    {
    System.out.println("i/j = "+(i/j));
    }
    };
    public class Calculator
    {
    public static void main(String args[])
    {

    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
    int i=0;
    int j=0;
    String str=null;
    String s=null;
    try
    {
    System.out.println("请输入第一个数字");
    str=buf.readLine();
    i=Integer.parseInt(str);
    label1:
        for(;true;)
    {


    System.out.println("请输入第二个数字");
    str=buf.readLine();
    j=Integer.parseInt(str);
    Op o=new Op(i,j);

    label2:
    for(;true;)
    {
    System.out.println("请输入所需的操作:加,减,乘,除");
    s=buf.readLine();
    if(s.equals("+"))
    {
    o.plus();
    }
    else if(s.equals("-"))
    {
    o.sub();
    }
    else if(s.equals("*"))
    {
    o.mul();
    }
    else if(s.equals("/"))
    {
    if(j==0)
    {
    System.out.println("请输入一个不为零的除数");
    continue label1;
    }
    else
    {
      o.div();
    }
    }
    else
    {
    System.out.println("请输入正确的操作");
    continue label2;
    }
    break;
    }
    break;
    }
    }
    catch (Exception e)
    {
    System.out.println("输入的数据格式有误,请重新输入一个数字");
    }

    }
    };
    多多交流
      

  4.   

    用面向对象方法做, 传参(int num1, int num2, char sign)
     判断  
     即可
      

  5.   

    什么叫做“测试实现”?我只是用swing做过计算器!!
      

  6.   

    用递归处理,也可以用堆栈
    我自己刚学的时候也写了个,利用递归解析表达式的
    http://blog.sina.com.cn/s/blog_4ab057eb0100b0vp.html