回分類題庫
d261: 103OOP Lab Exercise: Complex Number Class Overloading
出處:

Difficulity : N/A
Accepted : 102 Times | Submit :149 Times | Clicks : 1061
Accepted : 94 Users | Submit : 95 Users | Accepted rate : 99%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2014-11-24 08:11

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.)

>>, << :
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>

#include<math.h> 

using namespace std;

class Complex

{

public:

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

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

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

/* ========== */

Complex();

    Complex(double Real,double Imaginary);

 

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++ = "<<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;

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 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++ = 1+3i
C1++ = 2+4i
++C1 = 4+6i
++C1 = 5+7i
C2-- = 3+7i
C2-- = 2+6i
--C2 = 0+4i
--C2 = -1+3i
==========
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++ = 2+1i
C1++ = 3+2i
++C1 = 5+4i
++C1 = 6+5i
C2-- = 2+2i
C2-- = 1+1i
--C2 = -1-1i
--C2 = -2-2i
==========
C1 = 5+5i
C2 = 5+5i
C1+C2 = 10+10i
C1-C2 = 0+0i
C1*C2 = 0+50i
C1/C2 = 1+0i
C1++ = 5+5i
C1++ = 6+6i
++C1 = 8+8i
++C1 = 9+9i
C2-- = 5+5i
C2-- = 4+4i
--C2 = 2+2i
--C2 = 1+1i
==========

Hint :


  

Author :

(管理員:oopTA)

  Solve it!   Status Forum (0)

C++
C
JAVA
75242. oop104703032 (4 ms , 500KB)
75303. oop104703024 (6 ms , 500KB)
75292. oop104703002 (6 ms , 498KB)
75290. oop104703027 (6 ms , 500KB)
75288. oop104703039 (6 ms , 494KB)
沒有解題記錄 沒有解題記錄

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