回分類題庫
d287: 105OOP Overloading:Complex Number - Part2
出處:

Difficulity : N/A
Accepted : 60 Times | Submit :74 Times | Clicks : 729
Accepted : 51 Users | Submit : 51 Users | Accepted rate : 100%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2016-11-06 17:43

Content :

Given a Complex class header file and some implementation. You are asked to overload the following operators:

Complex Number has two part - Real part and Imaginary part.

 

==, != : If C1’s Real part is same to C2’s and C1’s Imaginary part is same to C2’s then C1==C2, else C1 != C2.

 

>, <: If C1’s Real part is greater than C2’s then C1 > C2. And If C1’s Real part is same to C2’ and C1’s Imaginary part is greater than C2’s then C1 > C2.

 

>=, <= : C1 >= C2 if and only if C1 > C2 or C1 == C2. C1 <= C2 if and only if C1 < C2 or C1 == C2.

 

+=, -= : C1 += C2 means C1 = C1 + C2, C1 -= C2 means C1 = C1 - C2.

 

Notice:

*You have to submit the code with given Complex class and given main function.

 

/************************Code Below*************************/

#include<iostream> 

 

using namespace std; 

 

class Complex 

public: 

    friend ostream& operator<<(ostream&, const Complex&); 

    friend istream& operator>>(istream&, Complex&); 

 

    Complex(); 

    Complex(double Real,double Imaginary); 

       

    /* Fill in your code here... */ 

       

private: 

    double Real; 

    double Imaginary; 

}; 

Complex::Complex()  

    Real = 0; 

    Imaginary =0; 

Complex::Complex( double Real, double Imaginary ) 

    Real = Real; 

    Imaginary = Imaginary; 

istream& operator>> ( istream& is, Complex& num ) 

    double a, b; 

    is >> a >> b; 

    num.Real = a; 

    num.Imaginary = b; 

    return is; 

 

ostream& operator<< ( ostream& out, const Complex& num ) 

    out << num.Real; 

    if( num.Imaginary >= 0 ) 

        out << '+' << num.Imaginary << 'i'; 

    else 

        out << num.Imaginary << 'i'; 

    return out; 

 

/*

        Start to write your operator overloading here.

        +=, -=, ==, !=, <=, >=, >, <

*/

 

int main() 

    Complex C1,C2; 

    while( cin >> C1 >> C2 ) 

    { 

       cout<<"C1==C2? "<<((C1==C2)?"true":"false")<<endl;

       cout<<"C1!=C2? "<<((C1!=C2)?"true":"false")<<endl;

       cout<<"C1<=C2? "<<((C1<=C2)?"true":"false")<<endl;

       cout<<"C1>=C2? "<<((C1>=C2)?"true":"false")<<endl; 

       cout<<"C1<C2? "<<((C1<C2)?"true":"false")<<endl;

       cout<<"C1>C2? "<<((C1>C2)?"true":"false")<<endl;

       C1+=C2;

       cout<<"C1+=C2; C1= "<<C1<<endl;

       C2-=C1;

       cout<<"C2-=C1; C2= "<<C2<<endl;

       cout<<"=========="<<endl;

    } 

    return 0; 

}

Input :

There are several sets of input. Each line is a set and contains four doubles which indicated Complex Number C1's and C2's real part, imaginary part. You are asked to complete the program in order to get this code segment successfully compiled and accepted by system.

Output :

You don't need to worry this. Just makes sure a Complex Number with real = 3 and imaginary = 5, your << operator will output 3+5i. There is no space between them.

 

And if a Complex Number with real = 3 and imaginary = -5, your << operator will output 3-5i (Not “3--5i”).

Sample Input :

1 3 3 7
2 1 2 2
5 5 5 5

Sample Output :

C1==C2? false
C1!=C2? true
C1<=C2? true
C1>=C2? false
C1<C2? true
C1>C2? false
C1+=C2; C1= 4+10i
C2-=C1; C2= -1-3i
==========
C1==C2? false
C1!=C2? true
C1<=C2? true
C1>=C2? false
C1<C2? true
C1>C2? false
C1+=C2; C1= 4+3i
C2-=C1; C2= -2-1i
==========
C1==C2? true
C1!=C2? false
C1<=C2? true
C1>=C2? true
C1<C2? false
C1>C2? false
C1+=C2; C1= 10+10i
C2-=C1; C2= -5-5i
==========

Hint :


  

Author :

(管理員:oopTA)

  Solve it!   Status Forum (0)

C++
C
JAVA
77170. oop102701008 (4 ms , 500KB)
75800. oop104703010 (4 ms , 508KB)
75549. oop103304042 (4 ms , 506KB)
75496. oop_100703015 (4 ms , 502KB)
76441. oop104703010 (6 ms , 502KB)
沒有解題記錄 沒有解題記錄

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡