C++编程,定义一个复数类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++编程,定义一个复数类相关的知识,希望对你有一定的参考价值。

定义一个复数类,并重载运算符,以实现复数的加法与减法运算,及显示运算结果

参考技术A //complex类
#include<iostream.h>
#include<math.h>

class complex
double real,imag;
public:
complex()real=5;imag=5;//缺省构造函数
complex(double r)real=r;imag=0;//只给实部赋值的构造函数
complex(double r,double i)real=r;imag=i;//同时给实、虚部赋值的函数
double displayreal()return real;//返回复数实部
double displayimag()return imag;//返回复数虚部
complex operator+(complex c);//实现复数相加
complex operator-(complex c);//实现复数相减
complex operator*(complex c);//实现复数相乘
double cab(complex c);//求复数绝对值(模)
complex sqr(complex c);//求复数平方根
friend ostream &operator<<(ostream &out,complex &obj);//实现复数的输出
;
complex complex::operator+(complex c)//实现复数相加

real+=c.real;
imag+=c.imag;
return *this;


complex complex::operator-(complex c)//实现复数相减

real-=c.real;
imag-=c.imag;
return *this;


complex complex::operator*(complex c)//实现复数相乘

real=real*c.real-imag*c.imag;
imag=real*c.imag+imag*c.real;
return *this;


double complex::cab(complex c)//求复数绝对值(模)

double ri;
ri=sqrt(c.real*c.real+c.imag*c.imag);
return ri;


complex complex::sqr(complex c)//求复数平方根

real=sqrt((cab(c)+c.real)/2);
imag=sqrt((cab(c)-c.real)/2);
return *this;


ostream &operator<<(ostream &out,complex &obj)//实现复数的输出

if(obj.imag==0)out<<obj.real;
else out<<obj.real<<"+"<<obj.imag<<"i";
return out;


void answer(double a,double b,double c)//求根函数

complex answer1,answer2;
double an=b*b-4*a*c;
if(an>=0)

answer1=complex((-b+sqrt(an))/(2*a));
answer2=complex((-b-sqrt(an))/(2*a));

else

answer1=complex(-b/(2*a),sqrt(-an)/(2*a));
answer2=complex(-b/(2*a),-sqrt(-an)/(2*a));

cout<<"The answer is:"<<endl;
cout<<answer1<<" and "<<answer2<<endl;

int main()//主函数

complex a,b(2),c(6,9);//以下测试类中定义的各个函数,你可以删除的
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a+b;
cout<<"a+b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a-b;
cout<<"a-b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a*b;
cout<<"a*b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
cout<<"cab(a)="<<a.cab(a)<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
cout<<"sqr(a)="<<a.sqr(a)<<endl;//以上测试类中定义的各个函数

answer(1,1,1);//方程解为虚数的情况
answer(1,3,1);//方程解为实数的情况
return 0;
本回答被提问者采纳

用java 编写一个复数类

2)完成以下的操作:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减等。
提示:
1)建立数据类、复数类
2)数据、复数信息的初始化
3)复数信息的输出
4)求复数的绝对值
5)实现复数的加、减、乘、除、乘方、自加、自减等运算

参考技术A public class Complex

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));

参考技术B 这哥们回答的不错!

以上是关于C++编程,定义一个复数类的主要内容,如果未能解决你的问题,请参考以下文章

C++定义一个复数类Complex,包括实数部分real和虚数部分imaginary两个私有数据成员

YTU 2439: C++习题 复数类--重载运算符+

YTU 2443: C++习题 复数类--重载运算符3+

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

YTU 2441: C++习题 复数类--重载运算符2+