第9周任务1

时间:2022-08-29 04:04:18
#include <iostream>      
using namespace std;
#include "99.h"

ostream& operator << (ostream& output,Complex&c)//注意加上output,c,声明时没加上
{
output<<"("<<c.real<<"+"<<c.imag<<"i)";
return output;
}
istream& operator >>(istream& input,Complex&c)
{
cout<<"请输入数据:";
input>>c.real>>c.imag;
return input;
}
Complex Complex::operator-()

{

return (0-*this);///////////////////////////////////this代表当前对象

}

Complex operator+(Complex &c1,Complex &c2)

{

Complex c;

c.real=c1.real+c2.real;

c.imag=c1.imag+c2.imag;

return c;

}



//复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.

Complex operator-(Complex &c1,Complex &c2)

{

Complex c;

c.real=c1.real-c2.real;

c.imag=c1.imag-c2.imag;

return c;

}



//复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

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;

}



//复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

Complex operator/(Complex &c1,Complex &c2)

{

Complex c;



c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

return c;

}



//复数相加:(a+bi)+(c+di)=(a+c)+(b+d)i.

Complex operator+(Complex &c1,double d)

{



Complex n(d,0);



return c1+n;

}



//复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.

Complex operator-(Complex &c1, double d)

{



Complex n(d,0);



return c1-n;

}



//复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

Complex operator*(Complex &c1,double d)

{



Complex n(d,0);

;

return c1*n;

}



//复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

Complex operator/(Complex &c1,double d)

{



Complex n(d,0);



return c1/n;

}

Complex operator+(double d,Complex &c1)

{

Complex n(d,0);



return n+c1;

}



//复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.

Complex operator-(double d,Complex &c1)

{



Complex n(d,0);



return n-c1;

}



//复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

Complex operator*(double d,Complex &c1)

{



Complex n(d,0);



return n*c1;

}



//复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

Complex operator/(double d,Complex &c1)

{



Complex n(d,0);



return n/c1;

}

void Complex::display()

{

cout<<"("<<real<<","<<imag<<"i)"<<endl;

}



int main()

{


//Complex c1(3,4),c2(5,-10),c3;
Complex c3,c1,c2;
cin>>c1>>c2;

double d=11;

cout<<"c1=";
cout<<c1<<endl;

//c1.display();

cout<<"c2=";
cout<<c2<<endl;

//c2.display();

c3=c1+c2;

cout<<"c1+c2=";

cout<<c1+c2<<endl;
//c3.display();

cout<<"d+c2=";

cout<<d+c2<<endl;

cout<<"c2+d=";

cout<<c2+d<<endl;

c3=c1-c2;

cout<<"c1-c2=";

//c3.display();
cout<<c3<<endl;
cout<<"c2-d";

(c2-d).display();

cout<<"d-c2";

(d-c2).display();

c3=c1*c2;

cout<<"c1*c2=";

c3.display();

cout<<"d*c2=";

(d*c2).display();

cout<<"c1*d=";

(c1*d).display();

c3=c1/c2;

cout<<"c1/c2=";

c3.display();

cout<<"d/c2";

(d/c2).display();

cout<<"c2/d";

(c2/d).display();

system("pause");

return 0;

}


第9周任务1