复数的简单运算

模板: 方法名(A的实部,A的虚部,B的实部,B的虚部,结果的实部,结果的虚部)

公式:乘法  (a*c - b* d) + (b * c + a * d)i

            除法   (a* c + b* d )/(c*c + d* d) + (b * c - a * d )/(c * c + d* d)i


代码如下:

package com.njupt.acm;

public class Test2 {

	public static void main(String[] args) {
          
		double a = 4 , b = 6 , c = 2 , d = -1;
		double[] e = {0} , f = {0};
		
		cAdd(a,b,c,d , e , f);
		show(a, b, c, d, e, f);
		
		cSub(a,b,c,d,e,f);
		show(a, b, c, d, e, f);
		
		cMul(a,b,c,d,e,f);
		show(a, b, c, d, e, f);
		
		cDiv(a,b,c,d,e,f);
		show(a, b, c, d, e, f);
		
	}

	public static void cAdd(double a, double b, double c, double d, double[] e,
			double[] f) {

		e[0] = a + c;
		f[0] = b + d;
	}

	public static void cSub(double a, double b, double c, double d, double[] e,
			double[] f) {
		e[0] = a - c;
		f[0] = b - d;
	}

	public static void cMul(double a, double b, double c, double d, double[] e,
			double[] f) {
		e[0] = a * c - b * d;
		f[0] = b * c + a * d;
	}

	public static void cDiv(double a, double b, double c, double d, double[] e,
			double[] f) {
		double sq = c * c + d * d;
		e[0] = (a * c + b * d) / sq;
		f[0] = (b * c - a * d) / sq;
	}
	
	public static void show( double a , double b , double c , double d , double[] e , double[] f){
		System.out.println("("+a +" + "+ b + "i) + " + "(" + c + " + "+ d + "i) = " + e[0] +" + "+ f[0] + "i" );
	}
}

 


你可能感兴趣的:(java,复数的简单运算)