Advertisement
uopspop

Untitled

Apr 28th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. template <typename T>
  7. class Vector{
  8.     private:
  9.         // 資料(變數部分)
  10.         T _x;
  11.         T _y;
  12.          
  13.     public:
  14.         // 操作(函式部分)
  15.        
  16.         Vector(){
  17.             _x = 0;
  18.             _y = 0;
  19.         }
  20.        
  21.         Vector(T x, T y){
  22.             _x = x;
  23.             _y = y;
  24.         }
  25.        
  26.        
  27.         // this指標: 指向自己這個物件
  28.         Vector& setX(T x){
  29.             // (*this)._x = x;
  30.             this->_x = x;
  31.            
  32.             return *this;
  33.         }
  34.        
  35.         Vector& setY(T y){
  36.             _y = y;
  37.             return *this;
  38.         }
  39.        
  40.         T getX() const{
  41.             return _x;
  42.         }
  43.        
  44.         T getY() const{
  45.             return _y;
  46.         }
  47.        
  48.         double length() const{
  49.             return sqrt( _x * _x + _y * _y );
  50.         }
  51.        
  52.         Vector operator+(const Vector &b) const{
  53.             double newX = _x + b._x;
  54.             double newY = _y + b._y;
  55.             Vector c(newX, newY);
  56.             return c;
  57.         }
  58.        
  59.         bool operator<(const Vector &b) const{
  60.             if( length() < b.length() ){
  61.                 return true;
  62.             }
  63.             else if( length() > b.length() ){
  64.                 return false;
  65.             }
  66.             else if( _x < b._x ){
  67.                 return true;
  68.             }
  69.             else if( _x > b._x ){
  70.                 return false;
  71.             }
  72.             else if( _y < b._y ){
  73.                 return true;
  74.             }
  75.             else if( _y > b._y ){
  76.                 return false;
  77.             }
  78.             else{
  79.                 return false;
  80.             }
  81.         }
  82.        
  83.         void print() const{
  84.             cout << "(" << _x << "," << _y << ")" << endl;
  85.         }
  86. };
  87.  
  88. template <typename T>
  89. istream &operator>>(istream &in, Vector<T> &v){
  90.     int x, y;
  91.     in >> x >> y;
  92.     v.setX(x).setY(y);
  93.    
  94.     return in;
  95. }
  96.  
  97. template <typename T>
  98. ostream &operator<<(ostream &out, Vector<T> &v){
  99.     out << "(" << v.getX() << "," << v.getY() << ")";
  100.    
  101.     return out;
  102. }
  103.  
  104.  
  105. int main(){
  106.    
  107.     Vector<int> v;
  108.     Vector<int> v2;
  109. //  operator>>(cin, v)>>(cin, v2);
  110.     cin >> v >> v2;
  111.     cout << "v: " << v << endl;
  112.     cout << "v2: " << v2 << endl;
  113.    
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement