C++如何定义一个复数类,并支持四则运算

定义复数类支持四则运算

第1个回答  2018-12-14
#include<iostream.h>
class complex
{
private:
double real;
double imag;
public:
complex()
{
real=imag=0;
}
complex(double rpart,double ipart)
{
real=rpart;
imag=ipart;
}

complex operator+(const complex &com)
{
complex temp;
temp.real=real+com.real;
temp.imag=imag+com.imag;
return temp;
}

complex operator-(const complex &com)
{
complex temp;
temp.real=real-com.real;
temp.imag=imag-com.imag;
return temp;
}
complex operator*(const complex &com)
{
complex temp;
temp.real=real*com.real-imag*com.imag;
temp.imag=real*com.imag+imag*com.real;
return temp;
}
complex operator/(const complex &com)
{
complex temp;
temp.real=(real*com.real+imag*com.imag)/(com.real*com.real+com.imag*com.imag);
temp.imag=(imag*com.real-real*com.imag)/(com.real*com.real+com.imag*com.imag);
return temp;
}

void display()
{
if(imag>=0)
cout<<"("<<real<<"+"<<imag<<"i)";
else
cout<<"("<<real<<imag<<"i)";
}
};

int main(void)
{
double a,b,c,d;
cout<<"输入两个复数,即a,b,c,d的值:";
cin>>a>>b>>c>>d;
complex n1(a,b),n2(c,d);
cout<<"两个复数值为:";
cout<<"A=";
n1.display();
cout<<" B=";
n2.display();
cout<<endl;

cout<<endl<<endl<<"两个复数的加法:"<<endl;
complex result1;
result1=n1+n2;
n1.display();
cout<<"+";
n2.display();
cout<<"=";
result1.display();

cout<<endl<<endl<<"两个复数的减法:"<<endl;
complex result2;
result2=n1-n2;
n1.display();
cout<<"-";
n2.display();
cout<<"=";
result2.display();

cout<<endl<<endl<<"两个复数的乘法:"<<endl;
complex result3;
result3=n1*n2;
n1.display();
cout<<"×";
n2.display();
cout<<"=";
result3.display();

cout<<endl<<endl<<"两个复数的除法:"<<endl;
if(c==0&&d==0)
cout<<"不能进行运算,除数不能为0"<<endl;
else
{
complex result4;
result4=n1/n2;
n1.display();
cout<<"÷";
n2.display();
cout<<"=";
result4.display();
}
cout<<endl;
return 0;
}本回答被网友采纳

C++如何定义一个复数类,并支持四则运算
cout<<"输入两个复数,即a,b,c,d的值:";cin>>a>>b>>c>>d;complex n1(a,b),n2(c,d);cout<<"两个复数值为:";cout<<"A=";n1.display();cout<<" B=";n2.display();cout<<endl;cout<<endl<<endl<<"两个复数的加法:"<<endl;complex result1;result1=n1+n2;n1.display(...

C++复数 类 实现,包含四则运算
type takeIm(){return Im;}\/\/取虚部 complex takeconj(){complex t(Re,-Im);return t;}\/\/求共轭复数 complex operator =(complex&a){ Re=a.Re;Im=a.Im;return*this;} complex operator +(){ return*this;} complex operator -(){ complex t(-Re,-Im);return t;} complex operator ...

设计一个关于复数的结构类型,并实现复数的四则运算
1)由输入的实部和虚部生成一个复数 2)两个复数求和;3)两个复数求差;4)两个复数求积,5)从已知复数中分离出实部;6)从已知复数中分离出虚部。运算结果以相应的复数或实数的表示形式显示(最好用结构体的方法)要是能用c++和stl,可以这样写#include include void main(){ using namespace std;comple...

设计一个关于复数的结构类型,并实现复数的四则运算
int fuhao;printf("input the sign 1:+,2:-,3:*,4:\/");scanf("%d",&fuhao);switch (fuhao){ case 1:{ if((m->y+n->y)>=0)printf("%d+%di",(m->x+n->x),(m->y+n->y));else printf("%d%di",(m->x+n->x),(m->y+n->y));} break;case 2:if((m->y...

C++编写复数计算类
{ public:Complex();Complex(float a,float b){ real = a;imag = b;} ~Complex();void InputData();friend Complex operator-(Complex & com1,Complex & com2);friend Complex operator+(Complex & com1,Complex & com2);\/\/ Complex operator=(Complex & com1);void DisplayData();...

编写一个复数类
1、由于没有指定语言,假设你问的是C++语言吧。2、C++语言实现的复数类代码 include <iostream.h>class plural{\/* 定义私有变量——实部,虚部; *\/ double real; double imag;public: plural( double r = 0.0, double i = 0.0 ) { real = r; imag = i; } \/* 初始化; *\/ do...

急!C++定义一个复数类Complex,使下面的代码能够工作?
private:double re;double im;public:Complex(double re,double im=0){ this->re = re;this->im = im;} void add(const Complex& c){ this->re += c.re;this->im += c.im;} void sub(const Complex& c){ this->re -= c.re;this->im -= c.im;} void show(){ if(this-...

用C++编写一个复数计算器。为复数定义一个类,形式a+bi。a,b为double...
){ cout<<"1 加法\\n";cout<<"2 减法\\n";cout<<"3 乘法\\n";cout<<"4 除法\\n";cout<<"0 退出\\n";cout<<"请选择:";} void main(){ do{ menu();int choose;cin>>choose;complex c1,c2,c3;switch(choose){ case 1:cout<<"请输入第一个操作数:";c1.input();...

复数运算类:公式推导+代码实现(C++)
在复数运算中,通过代码实现和公式推导,我们探索了复数的正弦值、余弦值、乘幂、n次方根、指数和对数。首先,我们实现了一个复数类,并通过重载运算符简化了加减乘除操作。接着,我们深入探讨了如何计算复数的正弦和余弦值。通过定义复数域的正弦和余弦函数,并代入复数z,我们推导出了计算结果。在C++代码...

求C++程序代码: 建立 一个复数类 Complex
} \/\/这两个成员函数跟你在类里声明的不一样,你声明的是无参的,定义就不能有参数,类里帮你改成有参数的了 void Complex::out_real2(){ \/\/不需要参数,类里你声明的就是无参的 cout<<"复数的实部为:"<<real<<endl;} void Complex::out_imaginary2() \/\/不需要参数 { cout<...

相似回答