项目1

来源:互联网 发布:淘宝介入需要什么证据 编辑:程序博客网 时间:2024/05/21 06:55
#include <iostream>using namespace std;class Complex{public:    Complex(){real=0;imag=0;}    Complex(double r,double i){real=r;imag=i;}    friend Complex  operator+(double c1,Complex &c2);    friend Complex  operator-(double c1,Complex &c2);    friend Complex  operator*(double c1,Complex &c2);    friend Complex  operator/(double c1,Complex &c2);    friend Complex  operator-(Complex &c2);    void display();private:    double real;    double imag;};Complex operator+(double c1,Complex &c2){   return Complex(c1+c2.real,c2.imag);}Complex  operator-(double c1,Complex &c2){   return Complex(c1-c2.real,c2.imag);}Complex  operator*(double c1,Complex &c2){   return Complex(c1*c2.real,c1*c2.imag);}Complex  operator/(double c1,Complex &c2){    double d=c2.imag*c2.imag+c2.real*c2.real;   return Complex(c1*c2.real/d,-c1*c2.imag/d);}Complex   operator-(Complex &c2){   return Complex(-c2.real,-c2.imag);}void Complex::display(){    if(imag>0)cout<<real<<"+"<<imag<<"i"<<endl;else    cout<<real<<imag<<"i"<<endl;}int main(){    Complex c1(1,2),c2(3,-4),c3(2,3),c4(4,-2),c5,c6,c7,c8,c9(2,-3),c10;    c5=1.2+c1;    c5.display();    c6=2.3-c2;    c6.display();    c7=2.1*c3;    c7.display();    c8=2.5/c4;    c8.display();    c10=-c9;    c10.display();    return 0;}


运行结果:

原创粉丝点击