#include
using namespace std;
class Complex/*定义复数类Complex*/
{
private:
double real;/*复数的实部*/
double image;/*复数的虚部*/
public:
Complex(double r=0,double i=0);/*缺省构造函数*/
Complex(const Complex& other); /*拷贝构造函数*/
Complex operator+(const Complex& other);/*重载加法运算符*/
Complex operator-(const Complex& other);/*重载减法运算符*/
Complex operator*(const Complex& other);/*重载乘法运算符*/
Complex operator/(const Complex& other);/*重载除法运算符*/
Complex operator=(const Complex& other); /*重载=赋值运算符*/
Complex operator+=(const Complex& other);/*重载+=赋值运算符*/
Complex operator-=(const Complex& other);/*重载-=赋值运算符*/
Complex operator*=(const Complex& other);/*重载*=赋值运算符*/
Complex operator/=(const Complex& other);/*重载/=赋值运算符*/
Complex operator++(); /*重载前置++自增运算符*/
Complex operator++(int);/*重载后置++自增运算符*/
Complex operator--(); /*重载前置--自减运算符*/
Complex operator--(int);/*重载后置--自减运算符*/
bool operator==(const Complex& other);/*重载==关系运算符*/
bool operator!=(const Complex& other);/*重载!=关系运算符*/
void printf();/*输出复数*/
};
Complex::Complex(double r,double i)/*缺省时自动初始化*/
{
real=r;
image=i;
}
Complex::Complex(const Complex& other)/*用一个对象初始化另一个对象*/
{
real=other.real;
image=other.image;
}
Complex Complex::operator+(const Complex& other)/*重载+ */
{
Complex t;
t.real=real+other.real;
t.image=image+other.image;
return t;
}
Complex Complex::operator-(const Complex& other)/*重载- */
{
Complex t;
t.real=real-other.real;
t.image=image-other.image;
return t;
}
Complex Complex::operator*(const Complex& other)/*重载* */
{
Complex t;
t.real=real*other.real-image*other.image;
t.image=real*other.image+image*other.real;
return t;
}
Complex Complex::operator/(const Complex& other)/*重载/ */
{
Complex t;
double m;
m=other.real*other.real+other.image*other.image;
t.real=(real*other.real+image*other.image)/m;
t.image=(image*other.real-real*other.image)/m;
return t;
}
Complex Complex::operator=(const Complex& other)/*重载= */
{
real=other.real;
image=other.image;
return *this;
}
Complex Complex::operator+=(const Complex& other)/*重载+= */
{
real+=other.real;
image+=other.image;
return *this;
}
Complex Complex::operator-=(const Complex& other)/*重载-= */
{
real-=other.real;
image-=other.image;
return *this;
}
Complex Complex::operator*=(const Complex& other)/*重载*= */
{
double k=real;
real=real*other.real-image*other.image;
image=k*other.image+image*other.real;
return *this;
}
Complex Complex::operator/=(const Complex& other)/*重载/= */
{
double m,k=real;
m=other.real*other.real+other.image*other.image;
real=(real*other.real+image*other.image)/m;
image=(image*other.real-k*other.image)/m;
return *this;
}
Complex Complex::operator++()/*重载a++ */
{
real++;
image++;
return *this;
}
Complex Complex::operator++(int)/*重载++a */
{
Complex before=*this;
++(*this);
return before;
}
Complex Complex::operator--()/*重载a-- */
{
real--;
image--;
return *this;
}
Complex Complex::operator--(int)/*重载--a */
{
Complex before=*this;
--(*this);
return before;
}
bool Complex::operator==(const Complex& other)/*重载== */
{
return bool(real==other.real&&image==other.image);
}
bool Complex::operator!=(const Complex& other)/*重载!= */
{
return bool(real!=other.real||image!=other.image);
}
void Complex::printf()/*输出复数*/
{
cout<
cout<<"+"<
cout<
int main()
{
cout<<"Input the real and image:(x1,y1,x2,y2)"<
cin>>x1>>y1>>x2>>y2;/*输入两个复数的值*/
Complex a(x1,y1),b(x2,y2),c;
cout<<"a="<
c=a-b; c.printf();
c=a*b; c.printf();
c=a/b; c.printf();
cout<
c=a; c+=b; c.printf();
c=a; c-=b; c.printf();
c=a; c*=b; c.printf();
c=a; c/=b; c.printf();
cout<
c=a; c=c++; c.printf();
c=a; c=--c; c.printf();
c=a; c=c--; c.printf();
cout<
cout<<"a==b"<
cout<<"a!=b"<
}
#include
#include
class complex{
public:
complex(){}
complex(double r,double i):real(r),imag(i){}
complex(complex &c){};
void set();
// void display();
friend complex operator +(const complex &c1,const complex &c2);
friend complex operator -(const complex &c1,const complex &c2);
friend ostream & operator<< (ostream &out,const complex &c);
double getreal(){return real;}
double getimag(){return imag;}
private:
double real;
double imag;
};
complex operator +(const complex &c1,const complex &c2){
return (complex(c1.real+c2.real,c1.imag+c2.imag));
}
complex operator -(const complex &c1,const complex &c2){
return (complex(c1.real-c2.real,c1.imag-c2.imag));
}
ostream & operator<< (ostream &out,const complex &c){
out<<"("<
}
void complex::set(){
cout<<"请输入实部"<
cout<<"请输入虚部"<
}
/*void complex::display(){
cout<<"数值为:"<
*/
void main(){
int i;
while(1){
cout<<"请输入1或2来选择功能, 1进行测试,2退出"<
switch(i)
{
case 1: {
complex c1,c2;
cout<<"输入第一个虚数"<
cout<<"输入第二个虚数"<
cout<
};
}
}
//热心网友。。。。忘了登录
#include
#include
class complex{
public:
complex(){}
complex(double r,double i):real(r),imag(i){}
complex(complex &c){};
void set();
// void display();
friend complex operator +(const complex &c1,const complex &c2);
friend complex operator -(const complex &c1,const complex &c2);
friend ostream & operator<< (ostream &out,const complex &c);
double getreal(){return real;}
double getimag(){return imag;}
private:
double real;
double imag;
};
complex operator +(const complex &c1,const complex &c2){
return (complex(c1.real+c2.real,c1.imag+c2.imag));
}
complex operator -(const complex &c1,const complex &c2){
return (complex(c1.real-c2.real,c1.imag-c2.imag));
}
ostream & operator<< (ostream &out,const complex &c){
out<<"("<
}
void complex::set(){
cout<<"请输入实部"<
cout<<"请输入虚部"<
}
/*void complex::display(){
cout<<"数值为:"<
*/
void main(){
int i;
while(1){
cout<<"请输入1或2来选择功能, 1进行测试,2退出"<
switch(i)
{
case 1: {
complex c1,c2;
cout<<"输入第一个虚数"<
cout<<"输入第二个虚数"<
cout<
};
}
}
把这条语句complex(complex &c){};
注释掉就可以了 即 //complex(complex &c){};
其实你用Display 来显示输出 简单 又有效 别用ostream可能就没问题了
eat