用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算
复数具有实部和虚部两部分,如2+5i就是一个复数,其中2为实部,5i为虚部;两复数相加时,实部与实部相加,虚部与虚部相加。两复数相减时,实部与实部相减,虚部与虚部相减。如...
复数具有实部和虚部两部分,如2+5i就是一个复数,其中2为实部,5i为虚部;两复数相加时, 实部与实部相加, 虚部与虚部相加。两复数相减时,实部与实部相减, 虚部与虚部相减。
如(2+5i)+(3-4i)=5+1i
(2+5i)-(3-4i)=-1+9i
现在要求定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算,在main()函数中测试该类。
我需要全部代码,急用,谢谢了~ 展开
如(2+5i)+(3-4i)=5+1i
(2+5i)-(3-4i)=-1+9i
现在要求定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算,在main()函数中测试该类。
我需要全部代码,急用,谢谢了~ 展开
展开全部
public class ComplexDemo {
// main方法
public static void main(String[] a) {
Complex b = new Complex(2, 5);
Complex c = new Complex(3, -4);
System.out.println(b + "+" + c + "=" + b.add(c));
System.out.println(b + "-" + c + "=" + b.minus(c));
System.out.println(b + "*" + c + "=" + b.multiply(c));
System.out.println(b + "/" + c + "=" + b.divide(c));
}
}
// Complex类
class Complex {
private double m;// 实部
private double n;// 虚部
public Complex(double m, double n) {
this.m = m;
this.n = n;
}
// add
public Complex add(Complex c) {
return new Complex(m + c.m, n + c.n);
}
// minus
public Complex minus(Complex c) {
return new Complex(m - c.m, n - c.n);
}
// multiply
public Complex multiply(Complex c) {
return new Complex(m * c.m - n * c.n, m * c.n + n * c.m);
}
// divide
public Complex divide(Complex c) {
double d = Math.sqrt(c.m * c.m) + Math.sqrt(c.n * c.n);
return new Complex((m * c.m + n * c.n) / d, Math.round((m * c.n - n * c.m) / d));
}
public String toString() {
String rtr_str = "";
if (n > 0)
rtr_str = "(" + m + "+" + n + "i" + ")";
if (n == 0)
rtr_str = "(" + m + ")";
if (n < 0)
rtr_str = "(" + m + n + "i" + ")";
return rtr_str;
}
}
// main方法
public static void main(String[] a) {
Complex b = new Complex(2, 5);
Complex c = new Complex(3, -4);
System.out.println(b + "+" + c + "=" + b.add(c));
System.out.println(b + "-" + c + "=" + b.minus(c));
System.out.println(b + "*" + c + "=" + b.multiply(c));
System.out.println(b + "/" + c + "=" + b.divide(c));
}
}
// Complex类
class Complex {
private double m;// 实部
private double n;// 虚部
public Complex(double m, double n) {
this.m = m;
this.n = n;
}
// add
public Complex add(Complex c) {
return new Complex(m + c.m, n + c.n);
}
// minus
public Complex minus(Complex c) {
return new Complex(m - c.m, n - c.n);
}
// multiply
public Complex multiply(Complex c) {
return new Complex(m * c.m - n * c.n, m * c.n + n * c.m);
}
// divide
public Complex divide(Complex c) {
double d = Math.sqrt(c.m * c.m) + Math.sqrt(c.n * c.n);
return new Complex((m * c.m + n * c.n) / d, Math.round((m * c.n - n * c.m) / d));
}
public String toString() {
String rtr_str = "";
if (n > 0)
rtr_str = "(" + m + "+" + n + "i" + ")";
if (n == 0)
rtr_str = "(" + m + ")";
if (n < 0)
rtr_str = "(" + m + n + "i" + ")";
return rtr_str;
}
}
展开全部
定义2个成员变量,一个放实数,一个放虚数,再定义一个caculate方法,传进一个complex参数,计算后返回一个complex结果对象。
package test;
public class Complex {
private int shishu;
private int xushu;
public Complex(int shishu, int xushu) {
this.shishu = shishu;
this.xushu = xushu;
}
public Complex add(Complex complex) {
return new Complex(this.shishu + complex.getShishu(), this.xushu + complex.getXushu() );
}
public Complex cut(Complex complex) {
return new Complex(this.shishu - complex.getShishu(), this.xushu - complex.getXushu() );
}
public String toString() {
if (this.xushu == 0) {
return "" + this.shishu;
} else if (this.xushu > 0) {
return this.shishu + "+" + this.xushu + "i";
} else {
return this.shishu + "" + this.xushu + "i";
}
}
/**
* @param args
*/
public static void main(String[] args) {
Complex a = new Complex(5,2);
Complex b = new Complex(4,3);
System.out.println(a);
System.out.println(b);
System.out.println(a.add(b));
System.out.println(a.cut(b));
}
public int getShishu() {
return shishu;
}
public int getXushu() {
return xushu;
}
}
运行结果:
5+2i
4+3i
9+5i
1-1i
package test;
public class Complex {
private int shishu;
private int xushu;
public Complex(int shishu, int xushu) {
this.shishu = shishu;
this.xushu = xushu;
}
public Complex add(Complex complex) {
return new Complex(this.shishu + complex.getShishu(), this.xushu + complex.getXushu() );
}
public Complex cut(Complex complex) {
return new Complex(this.shishu - complex.getShishu(), this.xushu - complex.getXushu() );
}
public String toString() {
if (this.xushu == 0) {
return "" + this.shishu;
} else if (this.xushu > 0) {
return this.shishu + "+" + this.xushu + "i";
} else {
return this.shishu + "" + this.xushu + "i";
}
}
/**
* @param args
*/
public static void main(String[] args) {
Complex a = new Complex(5,2);
Complex b = new Complex(4,3);
System.out.println(a);
System.out.println(b);
System.out.println(a.add(b));
System.out.println(a.cut(b));
}
public int getShishu() {
return shishu;
}
public int getXushu() {
return xushu;
}
}
运行结果:
5+2i
4+3i
9+5i
1-1i
展开全部
用着这么麻烦吗?杀鸡你非得用拖拉机给它压死,至于吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询