packagenewpackage;
importjava.io.*;
publicclassComplexNumber{
publicdoubleRe;
publicdoubleIm;
publicvoidInitComplexnum()throwsIOException
{
InputStreamReaderreadRealPartNumber=newInputStreamReader(System.in);
BufferedReaderinputRealPartNumber=newBufferedReader(readRealPartNumber);
System.out.println("请输入复数的实部:");
StringstrRealPartNumber=inputRealPartNumber.readLine();
doublerealPartNumber=Double.parseDouble(strRealPartNumber);
InputStreamReaderreadVirtualPartNumber=newInputStreamReader(System.in);
BufferedReaderinputVirtualPartNumber=newBufferedReader(readVirtualPartNumber);
System.out.println("请输入复数的虚部:");
StringstrVirtualPartNumber=inputVirtualPartNumber.readLine();
doublevirtualPartNumber=Double.parseDouble(strVirtualPartNumber);
Re=realPartNumber;
Im=virtualPartNumber;
}//从键盘上输入并创建一个复数
publicvoidprintComplexnum()
{
if(Re!=0)
{
if(Im==0)System.out.printf("%fn",Re);
elseSystem.out.printf("%f+%fin",Re,Im);
}
elseSystem.out.printf("%fin",Im);
}
}//在屏幕上打印复数以上为复数类
classarithmetic
{
voidPlusComplexnum(ComplexNumberc1,ComplexNumberc2,ComplexNumberhe)
{
he.Re=c1.Re+c2.Re;
he.Im=c1.Im+c2.Im;
System.out.println("复数的和");
he.printComplexnum();
}//复数的加法
voidMinusComplexnum(ComplexNumberc1,ComplexNumberc2,ComplexNumbercha)
{
cha.Re=c1.Re-c2.Re;
cha.Im=c1.Im-c2.Im;
System.out.println("复数的差");
cha.printComplexnum();
}//复数的减法
voidMultiplicationComplexnum(ComplexNumberc1,ComplexNumberc2,ComplexNumberji)
{
ji.Re=c1.Re*c2.Re-c1.Im*c2.Im;
ji.Im=c1.Im*c2.Re+c1.Re*c2.Im;
System.out.println("复数的积");
ji.printComplexnum();
}//复数的乘法
voidShangComplexnum(ComplexNumberc1,ComplexNumberc2,ComplexNumbershang)
{
if(((c1).Re*(c1).Re+(c2).Re*(c2).Re)==0)System.out.println("您输入的除数为零!!!");
else
{
(shang).Re=((c1).Re*(c2).Re+(c1).Im*(c2).Im)/((c1).Re*(c1).Re+(c2).Re*(c2).Re);
(shang).Im=((c1).Re*(c2).Im-(c1).Im*(c2).Re)/((c1).Re*(c1).Re+(c2).Re*(c2).Re);
System.out.println("复数的商");
shang.printComplexnum();
}//复数的除法
}
}//以上为实现复数四则运算的类
packagenewpackage;
importjava.io.*;
publicclassTest{
publicstaticvoidmain(String[]args)throwsIOException
{ComplexNumberT=newComplexNumber();
ComplexNumberA=newComplexNumber();
ComplexNumberB=newComplexNumber();
A.InitComplexnum();B.InitComplexnum();
A.printComplexnum();B.printComplexnum();
arithmeticC=newarithmetic();
C.MinusComplexnum(A,B,T);
C.MultiplicationComplexnum(A,B,T);
C.PlusComplexnum(A,B,T);
C.ShangComplexnum(A,B,T);
}
}//通过测试在Netbeans上运行主函数Test类