#include<iostream>
using namespace std;
class complex{//定义一个复数类
public:
complex(){real=0;imag=0;}
complex(float r,float i){real=r;imag=i;}
complex(double r){real=r;imag=0;}//类型转换构造函数
operator double (){return real;}//类型转换函数
friend complex operator+(complex c1,complex c2);//重载运算符+
friend complex operator-(complex c1,complex c2);//重载运算符-
friend complex operator*(complex c1,complex c2);//重载运算符*
friend complex operator/(complex c1,complex c2);//重载运算符/
friend ostream&operator<<(ostream&ouput,complex &c);//重载运算符<<
friend istream&operator>>(istream&input,complex &c);//重载运算符>>
private:
float real;
float imag;
};
complex operator+(complex c1,complex c2){
complex c;
c.real=c1.real+c2.real;
c.imag=c1.imag+c2.imag;
return c;
}
complex operator-(complex c1,complex c2){
complex c;
c.real=c1.real-c2.real;
c.imag=c1.imag-c2.imag;
return c;
}
complex operator*(complex c1,complex c2){
complex c;
c.real=c1.real*c2.real-c1.imag*c2.imag;
c.imag=c1.imag*c2.real+c1.real*c2.imag;
return c;
}
//complex operator/(complex c1,complex c2)//先不定义函数
ostream &operator<<(ostream &ouput,complex &c){
ouput<<c.real<<"+"<<c.imag<<"i";
return ouput;
}
istream&operator>>(istream&input,complex &c){
input>>c.real>>c.imag;
return input;
}
int main(){
complex c1(1,2),c2(2,3),c3;
double a=3;
cout<<"c1+c2="<<c1+c2<<endl;
cout<<"c1-c2="<<c1-c2<<endl;
cout<<"c1*c2="<<c1*c2<<endl;
cout<<"c1+a="<<c1+complex(a)<<endl;
cout<<"a+c1="<<complex(a)+c1<<endl;
cin>>c3;
cout<<"c3="<<c3<<endl;
return 0;
}