定义一个复数类Complex,它有2个私有数据成员:m_real(描述实部),m_image(描述虚部)和若干个公有成员
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定义一个复数类Complex,它有2个私有数据成员:m_real(描述实部),m_image(描述虚部)和若干个公有成员相关的知识,希望对你有一定的参考价值。
①构造函数重载,可调用不同的构造函数创建不同的对象;②重载运算符“+”,使之能用于两个复数的加法运算;③定义一个成员函数display()打印复数,如(2+3i)。④在main函数中测试该类的语句。
参考技术A #include <iostream>using namespace std;
class Complex
private:
double m_real,m_image;
public:
Complex():m_real(0),m_image(0)
Complex(double r,double i):m_real(r),m_image(i)//重载构造函数
void display()
cout << m_real << '+' << m_image << 'i' << endl;
friend Complex operator+(Complex&,Complex&);//重载运算符“+”
;
Complex operator+(Complex& A,Complex&B)//重载运算符“+”
double r = A.m_real+B.m_real;
double i = A.m_image+B.m_image;
return Complex(r,i);
int main()
Complex A,B(1,2);//调用不同的构造函数创建不同的对象
A.display();
B.display();
Complex C = A+B;
C.display();
system("pause");
用java 编写一个复数类
2)完成以下的操作:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减等。
提示:
1)建立数据类、复数类
2)数据、复数信息的初始化
3)复数信息的输出
4)求复数的绝对值
5)实现复数的加、减、乘、除、乘方、自加、自减等运算
protected int a;
protected int b;
public Complex(int a, int b)
this.a = a;
this.b = b;
public String toString()
return this.a + " + " + this.b + "i";
public static Complex addition(Complex complex1, Complex complex2)
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
public static Complex subtract(Complex complex1, Complex complex2)
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
public static Complex multiplication(Complex complex1, Complex complex2)
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
public static Complex division(Complex complex1, Complex complex2) throws Exception
if (complex2.a == 0)
throw new Exception("complex2.a is 0");
if (complex2.b == 0)
throw new Exception("complex2.b is 0");
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
//测试用的类
public class ComplexTest
public static void main(String[] args) throws Exception
// TODO Auto-generated method stub
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
追问
你这程序 包括以上的要求吗? 有没有乘方 复数的绝对值
追答只有加 减 乘 除, 其它的功能和这些类似。
追问能不能给我完整下 哥们 我们交作业 老师还问里 麻烦你了 。。。谢了。。。
追答//绝对值
public static double abs(Complex complex)
return (Math.sqrt(complex.a * complex.a + complex.b * complex.b));
//乘方
public static Complex cPow(Complex complex, int n)
double r, j;
r = Math.sqrt(complex.a * complex.a + complex.b * complex.b);
j = Math.atan(complex.b / complex.a);
double a = Math.pow(r, n) * Math.cos(n * j);
double b = Math.pow(r, n) * Math.sin(n * j);
return new Complex(a, b);
//自加
public static Complex addSelf(Complex complex)
return new Complex(++complex.a, ++complex.b);
//自减
public static Complex minSelf(Complex complex)
return new Complex(--complex.a, --complex.b);
以上方法都放到类Complex里面。还有把类Complex里面的int定义的变量全部改成double。
//测试用的类:
...
System.out.println(complex1 + " 绝对值: " + Complex.abs(complex1));
System.out.println(complex2 + " 绝对值: " + Complex.abs(complex2));
System.out.println(complex1 + " 乘方: " + Complex.cPow(complex1, 3));
System.out.println(complex1 + " 自加: " + Complex.addSelf(complex1));
System.out.println(complex2 + " 自减: " + Complex.minSelf(complex2));
以上是关于定义一个复数类Complex,它有2个私有数据成员:m_real(描述实部),m_image(描述虚部)和若干个公有成员的主要内容,如果未能解决你的问题,请参考以下文章
C++定义一个复数类Complex,包括实数部分real和虚数部分imaginary两个私有数据成员
用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算