赠送了好多附加的运算符啊,你要给分啊
#include<iostream>
using namespace std;
class CComplex
{
private:
double real;
double imaginary;
public:
CComplex(double re=0.0,double im=0.0):real(re),imaginary(im){}
//因为本类中没有任何动态内存分配问题,所以,编译器自动提供的默认拷贝构造函数也能正确的完成工作,因而
//不需另外定义。
//加法运算符重载:
CComplex operator+ (const CComplex& aComplex)const;
CComplex operator+ (double realnum)const ;
friend CComplex operator+ (double realnum,const CComplex& aComplex);
//减法运算符重载:
CComplex operator- (const CComplex& aComplex)const;
CComplex operator- (double realnum)const ;
friend CComplex operator- (double realnum,const CComplex& aComplex);
//负号运算符重载:
CComplex operator-()const;
//乘法运算符重载:
CComplex operator* (const CComplex& aComplex) const;
CComplex operator*(double realnum) const;
friend CComplex operator*(double realnum,const CComplex& aComplex);
//除法运算符重载:
CComplex operator/ (const CComplex& aComplex) const;
CComplex operator/(double realnum) const;
friend CComplex operator/(double realnum,const CComplex& aComplex);
//标准输入输出流重载:
friend istream& operator>>(istream& in, CComplex& aComplex);
friend ostream& operator<<(ostream& out,const CComplex& aComplex);
//布尔运算符重载:
bool operator==(const CComplex& aComplex) const
{
return real==aComplex.real&&imaginary==aComplex.imaginary;
}
bool operator!=(const CComplex& aComplex) const
{
return real!=aComplex.real||imaginary!=aComplex.imaginary;
}
~CComplex(){}
};
//负号运算符重载:
CComplex CComplex::operator -() const
{
return CComplex(-real,-imaginary);
}
//加法运算符重载:
CComplex CComplex::operator +(const CComplex &aComplex) const
{
return CComplex(real+aComplex.real,imaginary+aComplex.imaginary);
}
CComplex CComplex::operator +(double realnum) const
{
return CComplex(realnum+real,imaginary);
}
CComplex operator+( double realnum, const CComplex& aComplex)
{
return CComplex(realnum+aComplex.real,aComplex.imaginary);
}
//减法运算符重载:
CComplex CComplex::operator -(const CComplex &aComplex) const
{
return CComplex(real-aComplex.real,imaginary-aComplex.imaginary);
}
CComplex CComplex::operator -(double realnum) const
{
return CComplex(real-realnum,imaginary);
}
CComplex operator-( double realnum, const CComplex& aComplex)
{
return CComplex(realnum-aComplex.real,-aComplex.imaginary);
}
//乘法运算符重载:
CComplex CComplex::operator *(const CComplex &aComplex) const
{
return CComplex(real*aComplex.real-imaginary*aComplex.imaginary,real*aComplex.imaginary+imaginary*aComplex.real);
}
CComplex CComplex::operator *(double realnum) const
{
return CComplex(real*realnum,imaginary*realnum);
}
CComplex operator*( double realnum, const CComplex& aComplex)
{
return CComplex(realnum*aComplex.real,realnum*aComplex.imaginary);
}
//除法运算符重载:
CComplex CComplex::operator /(const CComplex &aComplex) const
{
return ((*this)*CComplex(aComplex.real,-aComplex.imaginary))/(aComplex.real*aComplex.real+aComplex.imaginary*aComplex.imaginary);
}
CComplex CComplex::operator /(double realnum) const
{
return CComplex(real/realnum,imaginary/realnum);
}
CComplex operator/( double realnum, const CComplex& aComplex)
{
return realnum*CComplex(aComplex.real,-aComplex.imaginary)/(aComplex.real*aComplex.real+aComplex.imaginary*aComplex.imaginary);
}
//标准输入输出流重载:
istream& operator>>(istream& in, CComplex& aComplex)
{
cout<<"Please enter two number: ";
in>>aComplex.real>>aComplex.imaginary;
return in;
}
ostream& operator<<(ostream& out,const CComplex& aComplex)
{
out<<aComplex.real<<"+"<<aComplex.imaginary<<"i";
return out;
}
int main()
{
const CComplex a=CComplex(1,2);
const CComplex b=CComplex(3,2);
if(a!=b)
cout<<2-a*b+a/b+3<<" ";
system("pause");
return 0;
}
温馨提示:内容为网友见解,仅供参考
C++ 定义一个复数类 求和,求积,求商,求差
using namespace std;class CComplex { private:double real;double imaginary;public:CComplex(double re=0.0,double im=0.0):real(re),imaginary(im){} \/\/因为本类中没有任何动态内存分配问题,所以,编译器自动提供的默认拷贝构造函数也能正确的完成工作,因而 \/\/不需另外定义。\/\/加法运算符重...
如何用c语言编一个复数的四则运算
要求实现下列六种基本运算1)由输入的实部和虚部生成一个复数2)两个复数求和;3)两个复数求差;4)两个复数求积,5)从已知复数中分离出实部;6)从已知复数中分离出虚部。运算结果以相应的复数或实数的表示形式显示(最好用结构体的方法)要是能用c++和stl,可以这样写#include <complex>#include <iostream...
(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算...
include <iostream>using namespace std;class Complex{public: Complex(double r=0,double i=0):real(r),imag(i) { }; friend Complex operator +(Complex &c1,Complex &c2); friend Complex operator +(Complex &c,double i); friend Complex operator +(double i,Complex &c); friend ostr...
C++如何定义一个复数类,并支持四则运算
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=...
求C++程序代码: 建立 一个复数类 Complex
} \/\/这两个成员函数跟你在类里声明的不一样,你声明的是无参的,定义就不能有参数,类里帮你改成有参数的了 void Complex::out_real2(){ \/\/不需要参数,类里你声明的就是无参的 cout<<"复数的实部为:"<<real<<endl;} void Complex::out_imaginary2() \/\/不需要参数 { cout<...
用C++编写一个复数计算器。为复数定义一个类,形式a+bi。a,b为double...
c3.imag=c1.real*c2.imag+c1.imag*c2.real;return c3;} complex div(complex c1,complex c2){ complex c3;int t;c3.real=(c1.real*c2.real+c1.imag*c2.imag)\/t;c3.imag=(c1.imag*c2.real-c1.real*c2.imag)\/t;t=c2.real*c2.real-c2.imag*c2.imag;return c3;} void ...
c++中怎样求复数相加?
定义复数类,中间加入运算符重载等。
c++ 复数求幂
c++有complex类型(即复数类型)和重载的各种数学函数(包括pow求幂),在<complex>头文件中的std命名空间下,直接使用即可。
C++ 一个复数类,运算符重载 + ,实现复数和复数的相加。
\/\/实数 float b;\/\/虚数};Complex& Complex::operator+(Complex &x){ this->a += x.a; this->b += x.b; return *this;}int main(){ Complex com1(3,2),com2(2,1),com3(3,0),com4(0,2),com5(0,0); Complex com; com = com1+com2+com3+com4...
急!C++定义一个复数类Complex,使下面的代码能够工作?
&& this->im>0)cout << this->re << "+" << this->im << "i" << endl;} };int main(){ double re,im;cin >> re >> im;Complex c1(re,im);cin >> re;Complex c2 = re;c1.show();c2.show();c1.add(c2);c1.show();c2.sub(c1);c2.show();return 0;} ...