第六週 項目三 點 類問題

*      
* 程序的版權和版本聲明部分      
* Copyright (c)2013, 煙臺大學計算機學院學生      
* All rightsreserved.      
* 文件名稱: object.cpp      
* 作    者: 袁靜      
* 完成日期:2013年4月17日      
* 版本號: v1.0      
* 輸入描述:由鍵盤輸入 點的座標    
* 問題描述:    
         點與點之間的距離,點關於x軸,y軸,原點的對稱點  
  
#include <iostream>
#include <cmath>
using namespace std;
class Cpoint
{
public:
    Cpoint(double xx=0,double yy=0);
    double Distance(Cpoint p) const;
    double Distance0() const;
    void input();
    void  output();
    Cpoint symmetericAxis(char style ) const;
public:
    double x;
    double y;

};
void Cpoint::input ()
{
    char ch;
    while (1)
    {
        cin>>x>>ch>>y;
        if(ch==',')
            break;
        else
        cout<<"格式輸入錯誤,請從新輸入:"<<endl;
    }
}
Cpoint::Cpoint(double xx,double yy)
{
    x=xx;
    y=yy;
}
void Cpoint::output()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
double Cpoint::Distance(Cpoint p)  const
{
    return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}
double Cpoint::Distance0() const
{
    return sqrt(x*x+y*y);

}
Cpoint Cpoint::symmetericAxis(char style) const
{
    Cpoint p(this->x,this->y);
    switch(style)
    {
    case'x':
        p.y=-y;
        break;
    case'y':
        p.x=-x;
        break;
    case'0':
        p.y=-y;
        p.x=-x;
        break;

    }
    return p;

}
int main()
{
    Cpoint  p1,p2,p;
    cout<<"請輸入第一個點的座標:"<<endl;
    p1.input();
    cout<<"請輸入第二個點的座標:"<<endl;
    p2.input();
    cout<<"兩點之間的距離爲:"<<p1.Distance(p2)<<endl;
    cout<<"第一個點與原點兩點的距離爲:"<<p1.Distance0()<<endl;
    cout<<"第一個點關於x的對稱點爲:"<<endl;
    p=p1.symmetericAxis('x');
    p.output();
    cout<<"第一個點關於y的對稱點爲:"<<endl;
    p=p1.symmetericAxis('y');
    p.output();
    cout<<"第一個點關於原點的對稱點爲:"<<endl;
    p=p1.symmetericAxis('0');
    p.output();

      return 0;
}

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