6.3 設計平面座標點類

/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱:         第六週 任務三                    
* 作    者:            楊森                 
* 完成日期:     2012    年   3    月    27    日
* 版 本 號:     v1.0    

* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程序輸出:
* 程序頭部的註釋結束

#include<iostream>   
  
#include<cmath>   
  
using namespace std;  
  
enum SymmetricStyle {axisx, axisy, point};    //分別表示按x軸, y軸, 原點對稱   
class CPoint  
{  
private:  
    double x;      // 橫座標   
    double y;     // 縱座標   
public:  
    CPoint(double xx=0, double yy=0);  
    double Distance(CPoint p) const;       // 兩點之間的距離(一點是當前點,另一點爲參數p)   
    double Distance0() const;             // 到原點的距離   
    CPoint SymmetricAxis(SymmetricStyle style) const;        // 返回對稱點   
    void input();        //以x,y 形式輸入座標點   
    void output();      //以(x,y) 形式輸出座標點   
};  
  
CPoint::CPoint(double xx, double yy):x(xx),y(yy){}  
  
int main()  
{  
    CPoint c1, c2;  
  
    c1.input();  
  
    c2.input();  
  
    c1.output();  
  
    c2.output();  
  
    cout << "兩點間的距離是:" << c1.Distance(c2) << endl;  
  
    cout << "點到原點的距離是:" << c2.Distance0() << endl;  
  
    c1.SymmetricAxis(axisx);  
  
    c1.SymmetricAxis(axisy);  
  
    c1.SymmetricAxis(point);  
  
    system("pause");  
}  
  
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const  
{  
    switch(style)  
    {  
    case(axisx):  
        {  
            cout << "點" << "(" << x << "," << y << ")"<< "關於X軸對稱點是: (" << x << "," << - y << ")" << endl;  
            break;  
        }  
    case(axisy):  
        {  
            cout << "點" << "(" << x << "," << y << ")" << "關於Y軸對稱點是: (" << - x << "," << y << ")" << endl;  
            break;  
        }  
    default:  
        {  
            cout << "點" << "(" << x << "," << y << ")"<< "關於原點對稱點是: (" << - x << "," << - y << ")" << endl;  
            break;  
        }  
    }  
    return (x, y);  
}  
  
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);  
}  
  
  
void CPoint::input()  
{  
    char c;  
  
    cout << "請輸入點座標:(格式:x,y)" << endl;  
  
    do  
    {  
        cin >> x >> c >> y;  
  
        if(c == ',')  
        {  
            break;  
        }  
  
        cout << "格式不正確,請重新輸入:" << endl;  
  
    }while(1);  
}  
  
void CPoint::output()  
{  
    cout << "點座標(" << x << "," << y << ")" << endl;  
}  

小感: const 還需要深入理解和好好運用啊
發佈了44 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章