Complex.java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;
public class Complex implements Cloneable
{
//dataField
private double realPart;
private double imaginaryPart;
//constructor
public Complex()
{
realPart = 0;
imaginaryPart = 0;
}
public Complex(double a)
{
realPart = a;
}
public Complex(double a, double b)
{
realPart = a;
imaginaryPart = b;
}
//accessor
public double getRealPart()
{
return realPart;
}
public double getImaginaryPart()
{
return imaginaryPart;
}
//method
public Complex add(Complex a, Complex b)
{
Complex tempC = new Complex(a.realPart + b.realPart, a.imaginaryPart + b.imaginaryPart);
return tempC;
}
public Object substract(Complex a, Complex b)
{
Complex tempC = new Complex(a.realPart - b.realPart, a.imaginaryPart - b.imaginaryPart);
return tempC;
}
public Complex multiply(Complex a, Complex b)
{
Complex tempC = new Complex(a.realPart * b.realPart - a.imaginaryPart * b.imaginaryPart, a.realPart * b.imaginaryPart + a.imaginaryPart * b.realPart);
return tempC;
}
public Complex divide(Complex a, Complex b)
{
double tempA = a.realPart * b.realPart + a.imaginaryPart * b.imaginaryPart;
double tempB = a.imaginaryPart * b.realPart - a.realPart * b.imaginaryPart;
double temp = Math.pow(b.imaginaryPart, 2) + Math.pow(b.realPart, 2);
Complex tempC = new Complex(tempA/temp, tempB/temp);
return tempC;
}
public double abs(Complex a)
{
return Math.sqrt(a.realPart * a.realPart + a.imaginaryPart * a.imaginaryPart);
}
@Override
public String toString()
{
if (this.realPart == 0 && this.imaginaryPart != 0) return formatDouble(imaginaryPart) + "i";
else if (this.imaginaryPart > 0) return formatDouble(realPart) + "+" + formatDouble(imaginaryPart) + 'i';
else if (this.imaginaryPart < 0) return formatDouble(realPart) + "-" + formatDouble(imaginaryPart) + 'i';
else return "" + formatDouble(realPart);
}
@Override
public Complex clone()
{
Complex tempC;
try
{
tempC = (Complex)super.clone();
}
catch(CloneNotSupportedException ex)
{
return null;
}
return tempC;
}
//美化浮点显示
public static String formatDouble(double d)
{
BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP);
double num = bg.doubleValue();
if (Math.round(num) - num == 0)
{
return String.valueOf((long) num);
}
return String.valueOf(num);
}
public static void main(String[] args)
{
System.out.println("输入第一个复数的实部和虚部(空格隔开)");
Scanner input = new Scanner(System.in);
double real_1 = input.nextDouble();
double imaginary_1 = input.nextDouble();
Complex c_1 = new Complex(real_1, imaginary_1);
System.out.println("输入第二个复数的实部和虚部(空格隔开)");
//double real_2 = input.nextDouble();
//double imaginary_2 = input.nextDouble();
Complex c_2 = new Complex(input.nextDouble(), input.nextDouble());
System.out.println(c_1.toString() + " + " + c_2.toString() + " = " + c_1.add(c_1, c_2));
System.out.println(c_1.toString() + " - " + c_2.toString() + " = " + c_1.substract(c_1, c_2));
System.out.println(c_1.toString() + " * " + c_2.toString() + " = " + c_1.multiply(c_1, c_2));
System.out.println(c_1.toString() + " / " + c_2.toString() + " = " + c_1.divide(c_1, c_2));
}
}