#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;image=0;}
Complex(double r,double i){real=r;image=i;}
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);
void display();
private:
double real;
double image;
};
Complex operator+(Complex &c1,Complex &c2)
{
return Complex(c1.real+c2.real,c1.image+c2.image);
}
Complex operator-(Complex &c1,Complex &c2)
{
return Complex(c1.real-c2.real,c1.image-c2.image);
}
Complex operator*(Complex &c1,Complex &c2)
{
return Complex(c1.real*c2.real,c1.image*c2.image);
}
Complex operator/(Complex &c1,Complex &c2)
{
return Complex(c1.real/c2.real,c1.image/c2.image);
}
void Complex::display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
return 0;
}
第八周项目1扩展
最新推荐文章于 2021-08-11 09:58:15 发布