第6周實驗報告3

#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):x(xx), y(yy){}

	double Distance(CPoint p) const;   // 兩點之間的距離(一點是當前點,另一點爲參數p)

	double Distance0() const;          // 到原點的距離

	CPoint SymmetricAxis(SymmetricStyle style) const;   // 返回對稱點

	void input();  //以x,y 形式輸入座標點

	void output(); //以(x,y) 形式輸出座標點
};

int main ()
{
	CPoint c, p;

	c.input();

	p.input();

	double mm;

	mm = c.Distance(p);

	cout << "兩點之間的距離爲:" << mm << endl;

	mm = c.Distance0();

	c.output();

	cout << "到原點的距離爲" << mm << endl;

	cout << "關於x, y, 原點的對稱點分別是:" << endl;
	
	c.SymmetricAxis(axisx);

	c.SymmetricAxis(axisy);

	c.SymmetricAxis(point);

	system ("pause");

	return 0;
}

void CPoint::input()
{
	cout << "請輸入橫座標和縱座標(x,y):" << endl;

	double a = 0, b = 0;

	char q;

	while (250)
	{
		cin >> a >> q >> b;

		if (a != 0 && b != 0 && q == ',')
		{
			break;
		}
		cout << "error! try again." << endl;
	}

	x = a;

	y = b;
}

void CPoint::output()
{
	cout << '(' << x << ',' << y << ')';
}

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

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

CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
	switch (style)
	{
	case 0:cout << '(' << x << ',' << -y << ')';break;

	case 1:cout << '(' << -x << ',' << y << ')';break;

	case 2:cout << '(' << -x << ',' << -y << ')';break;

	default:cout << "error!" << endl;
	}
	return 0;
}


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