回分類題庫
d226: 101OOP Lab Exercise: Complex Number Class Overloading
出處:

Difficulity : N/A
Accepted : 47 Times | Submit :80 Times | Clicks : 1184
Accepted : 47 Users | Submit : 49 Users | Accepted rate : 96%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2012-10-29 13:31

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.

+,- : Complex Number add and sub.

Ex. (a+bi)+(c+di)=(a+c)+(b+d)i 

*: multiplication of two Complex Numbers.

Ex. (a+bi)*(c+di)=(ac-bd)+(ad+bc)i

/ : division of two Complex Numbers

Ex. (a+bi)/(c+di)=((ac+bd)/(cc+dd))+((bc-ad)/(cc+dd))i

++, -- : Both Real part and Imaginary part are added(subtracted) by one. (Prefix and postfix are needed.)

==, != : 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.

>>, << :

Input(>>) two doubles for the Complex Number.

And output(<<) the following format : “a+bi” .

Notice:

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

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

#include<iostream>

using namespace std;

class Complex

{

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

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

        public:

        Complex();

        Complex(double Real,double Imaginary);

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

        private:

        double mReal;

        double mImaginary;     

};

Complex::Complex(){

        mReal = 0;

        mImaginary =0;

}

Complex::Complex( double Real, double Imaginary ){

        mReal = Real;

        mImaginary = Imaginary;

}

/* 

Start write your operator overloadings here.
>>, <<, +, -, *,/, +=, -=, ==, !=, <=, >=, >, <, ++(prefix), ++(postfix), --(prefix), --(postfix)

*/

int main()

{

        Complex C1,C2;

        while(cin>>C1>>C2){

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

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

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

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

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

                cout<<"C1/C2 = "<<C1/C2<<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;

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

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

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

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

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

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

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

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

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

                C1+=C2;

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

                C2-=C1;

                cout<<"C2-=C2; C2= "<<C2<<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 sucessfully 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 = 1+3i
C2 = 3+7i
C1+C2 = 4+10i
C1-C2 = -2-4i
C1*C2 = -18+16i
C1/C2 = 0.413793+0.0344828i
C1==C2? false
C1!=C2? true
C1<=C2? true
C1>=C2? false
C1<C2? true
C1>C2? false
C1++ = 1+3i
C1++ = 2+4i
++C1 = 4+6i
++C1 = 5+7i
C2-- = 3+7i
C2-- = 2+6i
--C2 = 0+4i
--C2 = -1+3i
C1+=C2; C1= 4+10i
C2-=C2; C2= -5-7i
C1 = 2+1i
C2 = 2+2i
C1+C2 = 4+3i
C1-C2 = 0-1i
C1*C2 = 2+6i
C1/C2 = 0.75-0.25i
C1==C2? false
C1!=C2? true
C1<=C2? true
C1>=C2? false
C1<C2? true
C1>C2? false
C1++ = 2+1i
C1++ = 3+2i
++C1 = 5+4i
++C1 = 6+5i
C2-- = 2+2i
C2-- = 1+1i
--C2 = -1-1i
--C2 = -2-2i
C1+=C2; C1= 4+3i
C2-=C2; C2= -6-5i
C1 = 5+5i
C2 = 5+5i
C1+C2 = 10+10i
C1-C2 = 0+0i
C1*C2 = 0+50i
C1/C2 = 1+0i
C1==C2? true
C1!=C2? false
C1<=C2? true
C1>=C2? true
C1<C2? false
C1>C2? false
C1++ = 5+5i
C1++ = 6+6i
++C1 = 8+8i
++C1 = 9+9i
C2-- = 5+5i
C2-- = 4+4i
--C2 = 2+2i
--C2 = 1+1i
C1+=C2; C1= 10+10i
C2-=C2; C2= -9-9i

Hint :


  

Author :

(管理員:oop10105)

  Solve it!   Status Forum (0)

C++
C
JAVA
42136. oop100703044 (6 ms , 430KB)
42115. oop100703012 (6 ms , 426KB)
42066. oop100703009 (6 ms , 496KB)
41961. oop100703045 (6 ms , 508KB)
41955. oop100703016 (6 ms , 500KB)
沒有解題記錄 沒有解題記錄

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