第六週實驗報告(三)平面座標類

/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生 
* All rights reserved.
* 文件名稱: 設計平面座標類,計算兩點之間距離、到原點的距離、關於座標軸和原點的對稱點等                             
* 作    者:  晁陽                            
* 完成日期:  2012       年   03   月   26 日
* 版 本 號:t 1.1         

常對象(常變量)、指針、數組,這些熟悉的名字在面向對象中再次出現,本來就有一些對概念的朦朧感,這次又遇到就像在生活中碰到同學,只知道他是自己的童鞋,但是具體是那個專業哪個班的就不知道啦!只好回去翻翻前面的。現在新的算法接觸的不多,主要新的概念,新的功能挺多的,如果不仔細分析不容易記憶,分辨,和利用。

#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) 形式輸出座標點
};

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 axisx:
		cout << "點(" << x << "," << y << ")" << "關於x軸的座標爲:(" << x << "," << -y << ")" << endl;
	case axisy:
		cout << "點(" << x << "," << y << ")" << "關於y軸的座標爲:(" << -x << "," << y << ")" << endl;
	case point:
		cout << "點(" << x << "," << y << ")" << "關於原點的座標爲:(" << -x << "," << -y << ")" << endl;
	}
	return 0;
}

void CPoint:: input()
{
	char ch = ',';
	cout << "請輸入點的座標:";
    cin >> x >> ch >> y;
	while (1)
	{
		if(ch != ',')
		{
			cout << "格式有誤,請重新輸入!";
			cin >> x >> ch >> y;
		}
		else
		{
			break;
		}
	}
}

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

void main()
{
	CPoint p1,p2;
	p1.input();
	p2.input();

	cout << "兩點間的距離爲:" << p1.Distance(p2) << endl;
	cout << "點p1距離原點的距離爲:" << p1.Distance0() << endl;
    cout << "點p2距離原點的距離爲:" << p2.Distance0() << endl;
    p1.SymmetricAxis(axisx);
	p2.SymmetricAxis(axisx);
	cout <<endl;
	system("pause");
	
}



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