classImaginNo{
privatedoublea;//实部
privatedoubleb;//虚部
//设置当前复数的值
publicvoidset(doublea,doubleb){
this.a=a;
this.b=b;
}
//获取实部
publicdoublegetA(){
returnthis.a;
}
//获取虚部
publicdoublegetB(){
returnthis.b;
}
//加法
publicvoidadd(doublea,doubleb){
this.a+=a;
this.b+=b;
}
//减法
publicvoidminus(doublea,doubleb){
this.a-=a;
this.b-=b;
}
//乘法
publicvoidmultiply(doublea,doubleb){
this.a=this.a*a-this.b*b;
this.b=this.a+a;
}
//除法
publicvoiddivide(doublea,doubleb){
if(a*b!=0){
this.a=(this.a*a+this.b*b)/(a*a+b*b);
this.b=(this.b*a-this.a*b)/(a*a+b*b);
}
else{
System.out.println("除数不能为0");
}
}
publicvoidprint(){
System.out.println("z="+this.a+"+("+this.b+")i");
}
}
publicclassImNum{
publicstaticvoidmain(String[]args){
ImaginNoz=newImaginNo();
z.set(2,5);
z.add(-1,2);
z.print();
z.minus(2,3);
z.print();
z.multiply(3,2);
z.print();
z.divide(4,2);
z.print();
System.out.println("z的实部为:"+z.getA());
System.out.println("z的虚部为:"+z.getB());
}
}