輸出一點的對稱點,兩點間的距離

/* (程序頭部註釋開始) * 程序的版權和版本聲明部分 * Copyright (c) 2011, 煙臺大學計算機學院學生 * All rights reserved. * 文件名稱:                              * 作    者:   苗影                           * 完成日期:     2012    年   3    月     28   日 * 版 本 號:         

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

#include<iostream>
#include<Cmath>
using namespace std;
enum SymmetricStyle { axisx,axisy,point};//分別表示按x軸, y軸, 原點對稱
class CPoint
{
private:
	mutable double x;  // 橫座標
	mutable 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;
}
//以x,y 形式輸入座標點
void CPoint::input()
{
	char c;
	double xx,yy;
	while(1)
	{
		cout<<"請按照x,y格式輸入"<<endl;
		cin>>xx>>c>>yy;
		if(c!=',')
			cout<<"格式不對,請重新輸入"<<endl;
		else
			break;
	}
	x=xx;
	y=yy;
}
// 到原點的距離
double CPoint::Distance0()const 
{
	double s;
	s=sqrt(x*x+y*y);
	return s;
}
// 兩點之間的距離(一點是當前點,另一點爲參數p)
double CPoint::Distance(CPoint p) const
{
	double s;
	s=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));//注意p座標的表達方式
	return s;
} 
// 返回對稱點
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
	switch(style)
	{
	case axisx:
		y=-y;
		cout<<'('<<x<<','<<y<<')'<<endl;break;
	case axisy:
		x=-x;
		cout<<'('<<x<<','<<y<<')'<<endl;break;
	case point:
		x=-x;
		y=-y;
		cout<<'('<<x<<','<<y<<')'<<endl;break;
	}
	return 0;
}
//以(x,y) 形式輸出座標點
void CPoint::output()
{
	cout<<'('<<x<<','<<y<<')';
}
int main()
{
	CPoint c1(2,3),c2(2,1),c3;
	cout<<"這兩點";
	c1. output();
	c2.output();
	cout<<"之間的距離爲:"<<c1. Distance(c2);
	cout<<endl;
	c1. output();
	cout<<"到原點的距離爲:"<<c1.Distance0();
	cout<<endl;
	c2. output();
	cout<<"到原點的距離爲:"<<c2.Distance0();
	cout<<endl;
	c3.input();
	cout<<"這兩點";
	c1. output();
	c3.output();
	cout<<"之間的距離爲:"<<c3. Distance( c1);
	cout<<endl;
	c3. output();
	cout<<"到原點的距離爲:"<<c3.Distance0();
	cout<<endl;
	c3.output();
	cout<<"關於x軸的對稱點:";
	c3.SymmetricAxis(axisx);
	cout<<endl;
	c1.output();
	cout<<"關於y軸的對稱點:";
	c1.SymmetricAxis(axisy);
	cout<<endl;
	c2.output();
	cout<<"關於原點的對稱點:";
	c2.SymmetricAxis(point);
	cout<<endl;
	return 0;
}
 
經驗積累:我以前很不習慣用switch語句,不過習慣了它還是比較方便的.

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