定义一个复数类Complex:
1)包含实部real、虚部imag,2)定义带参数构造函数初始化实部和虚部,3)定义不带参数构造函数初始化实部和虚部值为零,4)重载运算符+,实现两个复数相加,5)重载<...
1)包含实部real、虚部imag,
2)定义带参数构造函数初始化实部和虚部 ,
3)定义不带参数构造函数初始化实部和虚部值为零,
4)重载运算符+,实现两个复数相加,
5)重载<<输出运算符,输出复数。 展开
2)定义带参数构造函数初始化实部和虚部 ,
3)定义不带参数构造函数初始化实部和虚部值为零,
4)重载运算符+,实现两个复数相加,
5)重载<<输出运算符,输出复数。 展开
1个回答
推荐于2017-05-24 · 知道合伙人数码行家
关注
展开全部
参考代码如下:
public class Program
{
public static void main(String[] args)
{
class complex /*class complex begin*/
{
private double real,imagin;
public complex()
{
real=0;
imagin=0;
}
public complex(double a,double b)
{
real = a;
imagin = b;
}
public complex(complex C)
{
real=C.real;
imagin=C.imagin;
}
/*~complex(){}*/
double GetReal(){return real;}
double GetImagin(){return imagin;}
void SetReal(double realx){real=realx;}
void SetImagin(double imaginx){imagin=imaginx;}
public complex complexAdd(complex C)
{
double x,y;
x=real+C.real;
y=imagin+C.imagin;
SetReal(x);
SetImagin(y);
return this;
}
public complex complexMinus(complex C)
{
double x,y;
x=real-C.real;
y=imagin-C.imagin;
SetReal(x);
SetImagin(y);
return this;
}
public complex complexMulti(complex C)
{
double x,y,a,b,c,d;
a=real;
b=imagin;
c=C.real;
d=C.imagin;
x=-(a*c)+(b*d);
y=a*d+b*c;
SetReal(x);
SetImagin(y);
return this;
}
public complex complexdiv(complex C)
{
double a,b,c,d,x,y;
a=real;
b=imagin;
c=C.real;
d=C.imagin;
x=(a*c)/(c*c+d*d);
y=(a*d-c*b);
SetReal(x);
SetImagin(y);
return this;
}
public static boolean isEquals(complex C1,complex C2)
{
if(C1.real==C2.real && C1.imagin==C2.imagin)return true;
else return false;
}
public void StringToString()
{
System.out.println(real+"+"+imagin+"i");
}
};/*class complex end*/
complex a1 = new complex(3.2,4.5);
complex a2=new complex(a1);
complex a3 = new complex(a1.complexAdd(a2));
if(complex.isEquals(a1,a2)){System.out.println("Yes");}
else {System.out.println("Not");}
a1.StringToString();
a2.StringToString();
a3.StringToString();
}
}
public class Program
{
public static void main(String[] args)
{
class complex /*class complex begin*/
{
private double real,imagin;
public complex()
{
real=0;
imagin=0;
}
public complex(double a,double b)
{
real = a;
imagin = b;
}
public complex(complex C)
{
real=C.real;
imagin=C.imagin;
}
/*~complex(){}*/
double GetReal(){return real;}
double GetImagin(){return imagin;}
void SetReal(double realx){real=realx;}
void SetImagin(double imaginx){imagin=imaginx;}
public complex complexAdd(complex C)
{
double x,y;
x=real+C.real;
y=imagin+C.imagin;
SetReal(x);
SetImagin(y);
return this;
}
public complex complexMinus(complex C)
{
double x,y;
x=real-C.real;
y=imagin-C.imagin;
SetReal(x);
SetImagin(y);
return this;
}
public complex complexMulti(complex C)
{
double x,y,a,b,c,d;
a=real;
b=imagin;
c=C.real;
d=C.imagin;
x=-(a*c)+(b*d);
y=a*d+b*c;
SetReal(x);
SetImagin(y);
return this;
}
public complex complexdiv(complex C)
{
double a,b,c,d,x,y;
a=real;
b=imagin;
c=C.real;
d=C.imagin;
x=(a*c)/(c*c+d*d);
y=(a*d-c*b);
SetReal(x);
SetImagin(y);
return this;
}
public static boolean isEquals(complex C1,complex C2)
{
if(C1.real==C2.real && C1.imagin==C2.imagin)return true;
else return false;
}
public void StringToString()
{
System.out.println(real+"+"+imagin+"i");
}
};/*class complex end*/
complex a1 = new complex(3.2,4.5);
complex a2=new complex(a1);
complex a3 = new complex(a1.complexAdd(a2));
if(complex.isEquals(a1,a2)){System.out.println("Yes");}
else {System.out.println("Not");}
a1.StringToString();
a2.StringToString();
a3.StringToString();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询