第九周實驗報告一

來源:互聯網
上載者:User

/* (程式頭部注釋開始)
* 程式的著作權和版本聲明部分
* Copyright (c) 2011, 煙台大學電腦學院學生 
* All rights reserved.
* 檔案名稱:                              
* 作    者: 鮑增凱                          
* 完成日期:     2012    年   5  月    7   日

* 版 本 號:

01.#include <iostream>     02.using namespace std;    03.class Complex    04.{    05.public:    06.    Complex(){real=0;imag=0;}   07.  08.    Complex(double r,double i){real=r;imag=i;}  09.  10.  11.    friend Complex operator+(Complex &c1,Complex &c2);                   12.  13.    friend Complex operator-(Complex &c1,Complex &c2);   14.  15.    friend Complex operator*(Complex &c1,Complex &c2);   16.  17.    friend Complex operator/(Complex &c1,Complex &c2);   18.  19.  20.    friend Complex operator+(Complex &c1,double d);   21.  22.    friend Complex operator+(double d,Complex &c1);    23.  24.    friend Complex operator-(Complex &c1,double d);   25.  26.    friend Complex operator-(double d,Complex &c1);    27.  28.    friend Complex operator*(Complex &c1,double d);    29.  30.    friend Complex operator*(double d,Complex &c1);    31.  32.    friend Complex operator/(Complex &c1,double d);    33.  34.    friend Complex operator/(double d,Complex &c1);    35.  36.  37.    Complex operator-();   38.  39.    void display();    40.  41.    friend ostream & operator <<(ostream &,Complex &);              //友元函式宣告輸出資料流   42.   43.    friend istream & operator >>(istream &,Complex &);              //友元函式宣告輸入資料流   44.private:    45.    double real;    46.    double imag;    47.};    48.//下面定義成員函數     49.Complex operator+(Complex &c1,Complex &c2)    50.{    51.    return Complex(c1.real+c2.real,c1.imag+c2.imag);    52.}    53.    54.Complex operator-(Complex &c1,Complex &c2)    55.{    56.    return Complex(c1.real-c2.real,c1.imag-c2.imag);    57.}    58.    59.Complex operator*(Complex &c1,Complex &c2)    60.{    61.    Complex c;    62.    c.real=c1.real*c2.real-c1.imag*c2.imag;    63.    c.imag=c1.real*c2.imag-c1.imag*c2.real;    64.    return c;    65.}    66.    67.Complex operator/(Complex &c1,Complex &c2)    68.{     69.    Complex c;    70.    c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.imag*c2.imag+c2.real*c2.real);    71.    c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.imag*c2.imag+c2.real*c2.real);    72.    return c;    73.}    74.    75.Complex operator+(Complex &c1,double d)    76.{    77.    return Complex(c1.real+d,c1.imag);    78.}    79.    80.Complex operator+(double d,Complex &c1)    81.{    82.    return Complex(c1.real+d,c1.imag);    83.}    84.    85. Complex operator-(Complex &c1,double d)    86. {    87.     return Complex(c1.real-d,c1.imag);    88. }    89.    90. Complex operator-(double d,Complex &c1)    91.{    92.     return Complex(c1.real-d,c1.imag);    93. }    94.    95. Complex operator*(Complex &c1,double d)    96. {    97.     return Complex(c1.real*d,c1.imag*d);    98. }    99.    100. Complex operator*(double d,Complex &c1)    101. {    102.     return Complex(c1.real*d,c1.imag*d);    103. }    104.    105. Complex operator/(Complex &c1,double d)    106. {    107.     return Complex(c1.real/d,c1.imag/d);    108. }    109.    110. Complex operator/(double d,Complex &c1)    111. {    112.     return Complex(c1.real/d,c1.imag/d);    113. }    114.    115. Complex Complex:: operator-()    116. {    117.     return Complex(-real,-imag);    118. }    119.    120.   121.   122. ostream & operator <<(ostream & output,Complex &c)  123. {  124.     output<<"("<<c.real;  125.     if(c.imag>=0)output<<"+";  126.     output<<c.imag<<"i)"<<endl;  127.     return output;  128. }  129.  130. istream & operator >>(istream & input,Complex &c)  131. {  132.     cout<<"請輸入:"<<endl;  133.     input>>c.real>>c.imag;  134.  135.     return input;  136. }  137.  138.int main()    139.{    140.    Complex c1,c2,c3;    141.    double d=3;   142.    cin>>c1;  143.    cout<<"c1="<<c1<<endl;    144.    145.    cin>>c2;  146.    cout<<"c2="<<c2<<endl;      147.    148.    c3=c1+c2;    149.    cout<<"c1+c2="<<c3<<endl;      150.    151.    c3=c1-c2;    152.    cout<<"c1-c2="<<c3<<endl;     153.    154.    c3=c1*c2;    155.    cout<<"c1*c2="<<c3<<endl;    156.    157.    c3=c1/c2;    158.    cout<<"c1/c2="<<c3<<endl;    159.  160.    c3=c1+d;    161.    cout<<"c1+d="<<c3<<endl;      162.    163.    c3=d+c1;    164.    cout<<"d+c1="<<c3<<endl;      165.    166.    c3=c1-d;    167.    cout<<"c1-d="<<c3<<endl;     168.    169.    c3=d-c1;    170.    cout<<"d-c1="<<c3<<endl;      171.    172.    c3=c1*d;    173.    cout<<"c1*d="<<c3<<endl;    174.           175.    c3=d*c1;    176.    cout<<"d*c1="<<c3<<endl;      177.    178.    c3=c1/d;    179.    cout<<"c1/d="<<c3<<endl;    180.    181.    c3=d/c1;    182.    cout<<"d/c1="<<c3<<endl;      183.        184.    c3=-c1;    185.    cout<<"-c1="<<c3<<endl;      186.    187.    c3=-c2;    188.    cout<<"-c2="<<c3<<endl;      189.    190.    system("pause");    191.    return 0;    192.}    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.