回分類題庫
d167: OOP Lab Exercise: 2D Vector class Overloading
出處:

Difficulity : N/A
Accepted : 67 Times | Submit :183 Times | Clicks : 1646
Accepted : 64 Users | Submit : 70 Users | Accepted rate : 91%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2010-04-26 23:45

Content :

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

+, - : Vector add and sub.
* : Dot product of two vector. (A double.)
++, -- : Both x and y are added(subtracted) by one. (Prefix and postfix are needed.)
==, != : If V1’s x is same to V2’s and V1’s y is same to V2’s then V1==V2, else V1 != V2.
>, <: If the length of V1 is greater than V2’s then V1 > V2. If V2’s length is greater than V1’s then V2 > V1.
>=, <= : V1 >= V2 if and only if V1 > V2 or V1 == V2. V1 <= V2 if and only if V1 < V2 or V1 == V2.
+=, -= : V1 += V2 means V1 = V1 + V2, V1 -= V2 means V1 = V1 - V2.
>>, << : Input(>>) two doubles x and y for the vector. And output(<<) the following format : “(x,y)” .

Notice:

1.  You have to include std_lib_facilities.h.
2.  You have to submit the code with given Vector class and given main function.
3.  You have to submit with your oop account.

Code:   http://www.cs.nccu.edu.tw/~g9803/oop/Vector_ex.cpp

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

#include "std_lib_facilities.h"

class myVector{
    /*
    Some friends here.
    
    */

    public:
        myVector();
        myVector(double, double);
        double getX()const;
        double getY()const;
        void setX(double);
        void setY(double);
    private:
        double magnitudeSqr()const;
        double x;
        double y;
};

myVector::myVector():x(0), y(0){
}

myVector::myVector(double xx, double yy):x(xx), y(yy){
}

double myVector::getX()const{
    return x;
}

double myVector::getY()const{
    return y;
}

void myVector::setX(double xx){
    x = xx;
}

void myVector::setY(double yy){
    y = yy;
}

double myVector::magnitudeSqr()const{
    return (x*x+y*y);
}

/*

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

*/

int main(){
    myVector a, b;
    while(cin >> a >> b){
        cout << "P1: " << a << endl;
        cout << "P2: " << b << endl;
        cout << "P1+P2: " << a+b << endl;
        cout << "P1-P2: " << a-b << endl;
        cout << "P1*P2: " << a*b << endl;
        cout << "P1++: " << a++ << endl;
        cout << "P1++: " << a++ << endl;
        cout << "P1++: " << a++ << endl;
        cout << "++P1: " << ++a << endl;
        cout << "++P1: " << ++a << endl;
        cout << "++P1: " << ++a << endl;
        cout << "P1--: " << a-- << endl;
        cout << "P1--: " << a-- << endl;
        cout << "P1--: " << a-- << endl;
        cout << "--P1: " << --a << endl;
        cout << "--P1: " << --a << endl;
        cout << "--P1: " << --a << endl;
        a += b;
        cout << "P1+=P2: " << a << endl;
        a -= b;
        cout << "P1-=P2: " << a << endl;
        cout << "P1==P2: " << ((a==b)?"true":"false") << endl;
        cout << "P1!=P2: " << ((a!=b)?"true":"false") << endl;
        cout << "P1>P2: " << ((a>b)?"true":"false") << endl;
        cout << "P1<P2: " << ((a<b)?"true":"false") << endl;
        cout << "P1>=P2: " << ((a>=b)?"true":"false") << endl;
        cout << "P1<=P2: " << ((a<=b)?"true":"false") << endl;
    }
    return 0;
}

Input :

There are several sets of input. Each line is a set and contains four doubles X1, Y1, X2, Y2 which indicated 2D Vector P1's and P2's x, y value. 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 Vector with x = 3 and y = 5, your << operator will output (3,5) . There is no space between them.

Sample Input :

3 0 0 4
3 4 12 13

Sample Output :

P1: (3,0)
P2: (0,4)
P1+P2: (3,4)
P1-P2: (3,-4)
P1*P2: 0
P1++: (3,0)
P1++: (4,1)
P1++: (5,2)
++P1: (7,4)
++P1: (8,5)
++P1: (9,6)
P1--: (9,6)
P1--: (8,5)
P1--: (7,4)
--P1: (5,2)
--P1: (4,1)
--P1: (3,0)
P1+=P2: (3,4)
P1-=P2: (3,0)
P1==P2: false
P1!=P2: true
P1>P2: false
P1<P2: true
P1>=P2: false
P1<=P2: true
P1: (3,4)
P2: (12,13)
P1+P2: (15,17)
P1-P2: (-9,-9)
P1*P2: 88
P1++: (3,4)
P1++: (4,5)
P1++: (5,6)
++P1: (7,8)
++P1: (8,9)
++P1: (9,10)
P1--: (9,10)
P1--: (8,9)
P1--: (7,8)
--P1: (5,6)
--P1: (4,5)
--P1: (3,4)
P1+=P2: (15,17)
P1-=P2: (3,4)
P1==P2: false
P1!=P2: true
P1>P2: false
P1<P2: true
P1>=P2: false
P1<=P2: true

Hint :


  

Author :


  Solve it!   Status Forum (0)

C++
C
JAVA
20759. oop98703053 (4 ms , 506KB)
20758. oop98703014 (4 ms , 498KB)
20750. oop98703037 (4 ms , 502KB)
20739. oop98703020 (4 ms , 506KB)
20729. oop98703009 (4 ms , 504KB)
沒有解題記錄 沒有解題記錄

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