Java面向对象静态方法及域值控制&域值四则运算"以计算器实验为例"(初级)

*Create a class named Calculator.
A calculator has a method to add digits to a running total.
The user can also press operator keys such as + or * and then enter digits of a second
number.
When the user presses the = button, the calculator computes the result based on the numbers entered so far and the operator chosen. The user can then make further computations.
建立一个名为计算器的组,要求用户可以点击"+", “-”, "", “/” 进行计算,当用户按下”=“ 按钮时,计算结果会显示在输入栏里。
图片示例:
Java面向对象静态方法及域值控制&域值四则运算
对于刚刚完成对象静态方法的同学来说, 实现四则混合运算,实现可执行文件面还是有些困难。此博暂时只针对 乘除 / 加减 运算,并由内置代码实现。但其中对于结果的处理计算与显示,还是有许多值得学习的地方。
实验代码1(乘除实验):

public class _CalculatorExperiment {
    public static void main(String[] args) {
    	_Calculator calc = new _Calculator();
        calc.addDigit(1);
        calc.setOperator("*");
        calc.addDigit(6);
        calc.setOperator("/");
        calc.addDigit(2);
        double result = calc.compute();
        //将double类型进行判别,判断是否需要输出double类型
        if (result%(int)result==0) {
        //整数判别
        	System.out.println(calc);

            System.out.println("result = " + (int)result);
        }else {
        System.out.println(calc);

        System.out.println("result = " + result);	}
    }
}

实验结果1(乘除实验):
Java面向对象静态方法及域值控制&域值四则运算
实验代码2(加减实验):

public class _CalculatorExperiment {
    public static void main(String[] args) {
    	_Calculator calc = new _Calculator();
        calc.addDigit(1);
        calc.setOperator("+");
        calc.addDigit(6);
        calc.setOperator("-");
        calc.addDigit(2);
        double result = calc.compute();
        //将double类型进行判别,判断是否需要输出double类型
        if (result%(int)result==0) {
        //整数判别
        	System.out.println(calc);

            System.out.println("result = " + (int)result);
        }else {
        System.out.println(calc);

        System.out.println("result = " + result);	}
    }
}

实验结果2(加减实验):
Java面向对象静态方法及域值控制&域值四则运算
完整对象实现代码:

public class _Calculator {
//域值预先设置
	private int num = 0;
	private double addsum = 0;
	private double multisum = 1;
	private String addresult ="";
	private String multiresult ="";
	//记录当前计算符类型,用于更改符号以及计算公式
	private boolean ifadd = false;
	private boolean ifdele = false;
	private boolean ifmulti = false;
	private boolean ifdevi = false;
	//定义两个符号,加减符号以及乘除符号,乘号为1,除号为-1;加号为1,
	private int signOfDevi = 1;
	private int operator = 1;
	public _Calculator() {
		
	}
	public void addDigit(int n) {
	//this.num 的初始值为0,根据输入的n值进行末尾赋值
		this.num*=10;
		this.num+=n;
	}
	public int getNum() {
		return this.num;
	}
	public String getaddresult() {
		return this.addresult;
	}
	public double getaddsum() {
		return this.addsum;
	}
	public double getmultisum() {
		return this.multisum;
	}
	public String getmultiresult() {
		return this.multiresult;
	}
	public void setOperator(String p) {
	//先根据加减还是乘除进行辨别,这里将两种不同的计算分开,并给予了不同的sum值:addsum与multisum
		if (p.equalsIgnoreCase("+")||p.equalsIgnoreCase("-")) {
			this.ifmulti = false;
			this.ifdevi = false;
			this.addsum+=this.operator*this.num;
			if (p.equalsIgnoreCase("+")) {
				this.ifadd = true;
				this.ifdele = false;
			this.operator = 1;
			
			this.addresult = this.addresult+this.num+"+";
			this.num = 0;
			}else if(p.equalsIgnoreCase("-")) {
				this.ifdele = true;
				this.ifadd = false;
			this.operator = -1;
			
			this.addresult = this.addresult+this.num+"-";
			//输入值复位
			this.num = 0;
		}
	}
		if (p.equalsIgnoreCase("*")||p.equalsIgnoreCase("/")) {
			this.ifadd = false;
			this.ifdele = false;
			//将除法转换为Math方法中的 -1次方 从而融入乘除法混合运算
			this.multisum*=Math.pow(this.num, this.signOfDevi);
			if (p.equalsIgnoreCase("*")) {
			this.signOfDevi = 1;
			this.ifdevi =false;
			this.ifmulti = true;
	
			
			this.multiresult = this.multiresult+this.num+"*";
			//输入值复位
			this.num = 0;
		}
		if (p.equalsIgnoreCase("/")) {
			this.signOfDevi = -1;
			this.ifmulti = false;
			this.ifdevi = true;
		
			
			this.multiresult = this.multiresult+this.num+"/";
			//输入值复位
			this.num = 0;
		}
		}
	}
	public double compute() {
		if (this.ifadd) {
			this.addsum+=this.operator*this.num;
			this.addresult = this.addresult+this.num+"+";
			
			return addsum;	
		}else if (this.ifmulti){
			this.multisum*=Math.pow(this.num, this.signOfDevi);
			this.multiresult = this.multiresult+this.num+"*";
			
			return  multisum;	
		}else if (this.ifdevi) {
			this.multisum*=Math.pow(this.num, this.signOfDevi);
			this.multiresult = this.multiresult+this.num+"/";
			
			return  multisum;
		}else if (this.ifdele){
			this.addsum+=this.operator*this.num;
			this.addresult = this.addresult+this.num+"-";
			
			return  addsum;	
		}else {
			return 0;
		}
		 
	}
	public String toString() {
	//在结果显示时,省去最后一位的运算符
		if (this.ifadd||this.ifdele) {
			String result = this.addresult.substring(0, this.addresult.length()-1);
			return result;
		}else  {
			String result =this.multiresult.substring(0, this.multiresult.length()-1);
			return result ;
		}
	}
}

此计算器代码实现有许多值得学习的点,其中涉及到的逻辑算法可以应用在完全实现混合四则运算计算器甚至高等数学计算器中,在下一则Blog会进一步探讨。此次只讨论最初级的计算器实现,却也是最关键的部分。

你可能感兴趣的:(Java)