六週任務3

#include <iostream>
#include <cmath>

using namespace std;

enum SymmetricStyle { axisx , axisy , point };

class CPoint
{
private :
 double x ;
 double y ;

public :
 CPoint( double xx = 0 , double yy = 0 );//構造函數聲明

 double Distance ( CPoint p ) const ;//求兩點距離函數聲明

 double Distance0() const ;//求點到原點距離函數聲明

 CPoint SymmetricAxis ( SymmetricStyle ) const ;//求對稱點

    void input() ;//按x , y的形式輸入點

 void output() ;//按(x,y)輸出點的值

};

CPoint::CPoint( double xx , double yy )
{
 x = xx ;
 y = yy ;
}

void  CPoint::input()
{
 cout << "按 x , y 的形式輸入座標點" << endl ;
  cin >> x >> y ;
}

void  CPoint::output()
{
cout<< "點的座標爲:" <<"("<< x <<","<< y <<")"<< endl ;
}

double  CPoint::Distance0() const
{
 double H ;
  
 H = sqrt ( x * x + y * y ) ;
 return H;
}

double CPoint::Distance ( CPoint p ) const
{
 double h ;
 h = sqrt ((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y)) ; 
 return h ;
}

int main()
{
 CPoint cp1 , cp2 ;

 cp2.input() ;
 cp1.input() ;
 cp1.output() ;

 cout << "點到原點的距離爲:" << cp1.Distance0()<< endl ; 

 cout << "兩點之間的距離爲:" << cp1.Distance ( cp2 )<< endl ;

 cp1.SymmetricAxis( axisx );
 cout<< endl ;
 cp1.SymmetricAxis( axisy );
 cout<< endl ;
 cp1.SymmetricAxis( point );

 system ("pause") ;

 return  0 ;
}

CPoint CPoint::SymmetricAxis(SymmetricStyle style)  const  // 返回對稱點   
{  
    switch(style)  //利用switch語句選擇
    {  
    case axisx:  
        cout<<"點("<<x<<","<<y<<")"<<"關於y軸對稱的點爲:";   
        cout<<"("<<-x<<","<<y<<")";  
        break;  
    case axisy:  
        cout<<"點("<<x<<","<<y<<")"<<"關於x軸對稱的點爲:";  
        cout<<"("<<x<<","<<-y<<")";  
       break;  
    case point:  
        cout<<"點("<<x<<","<<y<<")"<<"關於原點對稱的點爲:";  
        cout<<"("<<-x<<","<<-y<<")";  
        break;  
    }  
    return 0;  
}  
 

   運行結果: 
感受:這裏的選擇結構有點困難,用時過長了,加油。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章