1. 程式人生 > >第九周專案一~~~一目二目運算子過載和的過載

第九周專案一~~~一目二目運算子過載和的過載

/*
* Copyright (c) 2013, 煙臺大學計算機學院
* All rights reserved.
* 作    者:紀麗娜
* 完成日期:2014 年 4 月21 日
* 版 本 號:v1.0
* 問題描述:

(1)再定義一目運算子 -,-c相當於0-c。
(2)定義Complex類中的<<和>>運算子的過載,
    實現輸入和輸出,改造原程式中對運算結果顯示方式,
    使程式讀起來更自然。
*/
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(double r,double i)
    {
        real=r;
        imag=i;
    }
    Complex operator+(Complex &c2);
    Complex operator-(Complex &c2);
    Complex operator*(Complex &c2);
    Complex operator/(Complex &c2);
    //一目運算子
    Complex operator-();
    //c+c1
    friend Complex operator+(double c,Complex &c1);
    friend Complex operator-(double c,Complex &c1);
    friend Complex operator*(double c,Complex &c1);
    friend Complex operator/(double c,Complex &c1);
    //c1+c
    friend Complex operator+(Complex &c2,double c);
    friend Complex operator-(Complex &c2,double c);
    friend Complex operator*(Complex &c2,double c);
    friend Complex operator/(Complex &c2,double c);
    friend ostream & operator<<(ostream & output,Complex &c);
    friend istream & operator>>(istream & input,Complex &c);
private:
    double real;
    double imag;
};
//下面定義成員函式
//一目
Complex Complex::operator-()
{
    return(0-*this);
}
//二目運算子
Complex Complex::operator+(Complex &c2)
{
    Complex c;
    c.real=real+c2.real;
    c.imag=imag+c2.imag;
    return c;
}
Complex Complex::operator-(Complex &c2)
{
    Complex c;
    c.real=real-c2.real;
    c.imag=imag-c2.imag;
    return c;
}
Complex Complex::operator*(Complex &c2)
{
    Complex c;
    c.real=(real*c2.real-imag*c2.imag);
    c.imag=(real*c2.imag+imag*c2.real);
    return c;
}
Complex Complex::operator/(Complex &c2)
{
    Complex c;
    c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
    c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
    return c;
}

//c+c1
Complex operator+(double c,Complex &c1)
{
    return  Complex(c+c1.real,c1.imag);
}
Complex operator-(double c,Complex &c1)
{
    return  Complex(c-c1.real,c1.imag);
}
Complex operator*(double c,Complex &c1)
{
    return  Complex(c*c1.real,c*c1.imag);
}
Complex operator/(double c,Complex &c1)
{
    Complex c2;
    c2.real=(c*c1.real)/(c1.real*c1.real+c1.imag*c1.imag);
    c2.imag=-1*((c*c1.imag)/(c1.real*c1.real+c1.imag*c1.imag));
    return c2;
}
//c1+c
Complex operator+(Complex &c2,double c)
{
    return  Complex(c2.real+c,c2.imag);
}
Complex operator-(Complex &c2,double c)
{
    return  Complex(c2.real-c,c2.imag);
}
Complex operator*(Complex &c2,double c)
{
    return  Complex(c*c2.real,c*c2.imag);
}
Complex operator/(Complex &c2,double c)
{
    return  Complex(c2.real/c,c2.imag/c);
}
//輸出
ostream & operator<<(ostream & output,Complex &c)
{
     if(c.real==0)
     {output<<c.imag<<"i"<<endl;
     }else
    {
        if(c.imag<0)
     {output<<c.real<<c.imag<<"i"<<endl;
     }else if(c.imag==0)
     {output<<c.real<<endl;
     }else
     {output<<c.real<<"+"<<c.imag<<"i"<<endl;}
    }
    return output;
}
//輸入
istream & operator>>(istream & input,Complex &c)
{
    cout<<"input real part and imaginary part of complex numble:";
    input>>c.real>>c.imag;
    return input;
}
//下面定義用於測試的main()函式
int main()
{
    Complex c1,c2,c3;
    cin>>c1>>c2;
    double c;
    cout<<"c1="<<c1;
    cout<<"c2="<<c2;
    c3=-c1;
    cout<<"-c1="<<c3;//不能直接輸出-c1無法識別先賦值再輸出!
    c3=-c2;
    cout<<"-c2="<<c3;
    c3=c1+c2;
    cout<<"c1+c2="<<c3;
    c3=c1-c2;
    cout<<"c1-c2="<<c3;
    c3=c1*c2;
    cout<<"c1*c2="<<c3;
    c3=c1/c2;
    cout<<"c1/c2="<<c3;
    cout<<"請輸入一個double型別的資料與c1進行運算:"<<endl;
    cin>>c;
    c3=c+c1;
    cout<<"c+c1="<<c3;
    c3=c-c1;
    cout<<"c-c1="<<c3;
    c3=c*c1;
    cout<<"c*c1="<<c3;
    c3=c/c1;
    cout<<"c/c1="<<c3;
    cout<<"請輸入一個double型別的資料與c2進行運算:"<<endl;
    cin>>c;
    c3=c2+c;
    cout<<"c2+c="<<c3;
    c3=c2-c;
    cout<<"c2-c="<<c3;
    c3=c2*c;
    cout<<"c2*c="<<c3;
    c3=c2/c;
    cout<<"c2/c="<<c3;
    return 0;
}

心得:喵喵喵喵。。。。。

           要回家了啊,

        內心有點激動\(≧▽≦)/