Unit9_problem1-复数大排练

来源:互联网 发布:javascript推箱子游戏 编辑:程序博客网 时间:2024/04/29 19:56
/*Univercity:烟台大学*@Class</A>计134~4*@Author:薛富磊*@Time:2014-4-28*@Function:复数*@Args:*@Return:*/#include<iostream>#include<Cmath>using namespace std;class Complex{public:    Complex(double r=0,double i=0):real(r),imag(i){}    Complex operator-();    //实现输入、输出的运算符重载    friend ostream& operator << (ostream& output, const Complex& c);    friend istream& operator >> (istream& input, Complex& c);    //实现加减乘除的运算符重载    friend Complex operator+(Complex &c1, Complex &c2);    friend Complex operator+(double d1, Complex &c2);    friend Complex operator+(Complex &c1, double d2);    friend Complex operator-(Complex &c1, Complex &c2);    friend Complex operator-(double d1, Complex &c2);    friend Complex operator-(Complex &c1, double d2);    friend Complex operator*(Complex &c1, Complex &c2);    friend Complex operator*(double d1, Complex &c2);    friend Complex operator*(Complex &c1, double d2);    friend Complex operator/(Complex &c1, Complex &c2);    friend Complex operator/(double d1, Complex &c2);    friend Complex operator/(Complex &c1, double d2);private:    double real;    double imag;};//实现输出的运算符重载ostream& operator << (ostream& output, const Complex& c){   cout<<"("<<c.real;    if(c.imag>=0) output<<"+";    cout<<c.imag<<"i)";    return output;}//实现输入的运算符重载istream& operator >> (istream& input, Complex& c){    int a,b;    char sign,i;    while(1)    {        cout<<"请输入复数(a+bi/a-bi):";        input>>a>>sign>>b>>i;        if(!((sign=='+'||sign=='-')&&i=='i'));        break;    }    c.real=a;    c.imag=(sign=='+')?b:-b;    return input;}Complex Complex::operator-(){    return(0-*this);}//复数相加:(a+bi)+(c+di)=(a+c)+(b+d)i.Complex operator+(Complex &c1, Complex &c2){    Complex t;    t.real=c1.real+c2.real;    t.imag=c1.imag+c2.imag;    return t;}Complex operator+(double d1, Complex &c2){    Complex t(d1,0);    return t+c2;}Complex operator+(Complex &c1, double d2){    Complex t(d2,0);    return c1+t;}//复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.Complex operator-(Complex &c1, Complex &c2){    Complex t;    t.real=c1.real-c2.real;    t.imag=c1.imag-c2.imag;    return t;}Complex operator-(double d1, Complex &c2){    Complex t(d1,0);    return t-c2;}Complex operator-(Complex &c1, double d2){    Complex t(d2,0);    return c1-t;}//复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.Complex operator*(Complex &c1, Complex &c2){    Complex t;    t.real=c1.real*c2.real-c1.imag*c2.imag;    t.imag=c1.imag*c2.real+c1.real*c2.imag;    return t;}Complex operator*(double d1, Complex &c2){    Complex t(d1,0);    return t*c2;}Complex operator*(Complex &c1, double d2){    Complex t(d2,0);    return c1*t;}//复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)iComplex operator/(Complex &c1, Complex &c2){    Complex t;    t.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);    t.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);    return t;}Complex operator/(double d1, Complex &c2){    Complex t(d1,0);    return t/c2;}Complex operator/(Complex &c1, double d2){    Complex t(d2,0);    return c1/t;}int main(){    Complex c1,c2,c3;    double d=25;    cout<<"c1: "<<endl;;    cin>>c1;    cout<<"c2: "<<endl;    cin>>c2;    cout<<"c1="<<c1<<endl;    cout<<"c2="<<c2<<endl;    cout<<"d="<<d<<endl;    //cout<<"-c1="<<(-c1);    c3=c1+c2;    cout<<"c1+c2="<<c3<<endl;    cout<<"c1+d="<<(c1+d)<<endl;    cout<<"d+c1="<<(d+c1)<<endl;    c3=c1-c2;    cout<<"c1-c2="<<c3<<endl;    cout<<"c1-d="<<(c1-d)<<endl;    cout<<"d-c1="<<(d-c1)<<endl;    c3=c1*c2;    cout<<"c1*c2="<<c3<<endl;    cout<<"c1*d="<<(c1*d)<<endl;    cout<<"d*c1="<<(d*c1)<<endl;    c3=c1/c2;    cout<<"c1/c2="<<c3<<endl;    cout<<"c1/d="<<(c1/d)<<endl;    cout<<"d/c1="<<(d/c1)<<endl;    return 0;}/*心得体会:           基本都是看的老师的           不过都看的懂  mmbb*/

0 0
原创粉丝点击