*程序頭部註釋開始
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱: 設計平面座標點類
* 作 者: 郭廣建
* 完成日期: 2012年 3 月 26 日
* 版 本 號: 1.0
題目敘述:
【任務3】設計平面座標點類,計算兩點之間距離、到原點距離、關於座標軸和原點的對稱點等
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) 形式輸出座標點
};
#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;
}
double CPoint::Distance(CPoint p) const
{
double dis;
dis = sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
return dis;
}
double CPoint::Distance0() const
{
double dis;
dis = sqrt(x *x + y * y);
return dis;
}
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
switch(style)
{
case axisx :cout <<"座標"<<'('<< x << ',' << y <<')' <<"關於x軸的對稱點是:" << "(" << x << ','<< -y <<")";break;
case axisy :cout <<"座標"<<'('<< x << ',' << y <<')' << "關於y軸的對稱點是:" << "(" << -x << ','<< y <<")";break;
case point :cout <<"座標"<<'('<< x << ',' << y <<')' <<"關於原點的對稱點是:" << "(" << -x << ','<< -y <<")";break;
}
cout <<endl;
return 0;
}
void CPoint::input()
{
char c;
cout << "以x,y形式輸入座標" <<endl;
cin >> x >> c >> y;
if( c != ',')
{
cout << "error!";
exit(0);
}
}
void CPoint::output()
{
cout << "以(x,y)的形式輸出座標點" <<endl;
cout << '(' << x << ',' << y << ')' <<endl;
}
void main()
{
CPoint CP1(2,3), CP2(3,4), CP3;
cout << "CP1(2,3)到CP2(3,4)的距離是:" << CP1.Distance(CP2) <<endl;
cout << "CP1(2,3)到原點的距離是:" <<CP1.Distance0() <<endl;
CP1.SymmetricAxis(axisx);
CP1.SymmetricAxis(axisy);
CP1.SymmetricAxis(point);
CP3.input();
CP3.output();
}
運行結果: