2012C++程序設計實驗報告【6.3】

/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱:任務三

* 作 者: 王琳
* 完成日期: 2012年 3 月27日
* 版 本 號: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);  
	double Distance(CPoint p) const;   // 兩點之間的距離  
	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;  
}  
 
// 輸入座標點  
void CPoint::input()  
{  
	char c;  
   cout<<"請輸入座標點(格式x,y ):";  
	while(1)  
   {  
	   cin>>x>>c>>y;  
	   if (c==',') break;  
		cout<<"輸入的數據格式不符合規範,請重新輸入\n";  
	}  
}  
  
// 輸出座標點  
void CPoint::output()  
{  
	cout<<"("<<x<<", "<<y<<")"<<endl;  
}  
  
// 求兩點之間的距離  
double CPoint::Distance(CPoint p) const  
{  
	double d;  
	d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));//將(p.x-x)更改爲(p.x-this->x)可以更便於理解,d是當前點*this和參數給出的點p間的距離  
	return d;  
}  
  
// 求點到原點的距離  
double CPoint::Distance0() const  
{  
	double d;  
	d=sqrt(x*x+y*y);  
	return d;  
}  
  
// 求對稱點  
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const  
{  
	CPoint p(this->x,this->y);//用複製構造函數時,寫作p(*this)  
	switch(style)  
	{  
	case axisx:  
		p.y=-y; break;  
	case axisy:  
	   p.x=-x; break;  
	case point:  
		p.x=-x;p.y=-y;  
	}  
	return p;  
}  
  
void main( )  
{  
	double distance;  
	CPoint p1,p2,p;  
   cout<<"第1個點p1,";  
	p1.input();  
	cout<<"第2個點p2,";  
	p2.input();  
	distance=p1.Distance(p2);  
	distance=p1.Distance0();  
	cout<<"p1到原點的距離爲:"<<distance<<endl;  
	p=p1.SymmetricAxis(axisx);  
	cout<<"p1關於x軸的對稱點爲:";  
   p.output();  
	p=p1.SymmetricAxis(axisy);  
   cout<<"p1關於y軸的對稱點爲:";  
   p.output();  
	p=p1.SymmetricAxis(point);  
	cout<<"p1關於原點的對稱點爲:";  
	p.output();  
	system("pause");  
}  


運行結果:

 

 感想:對於這個程序還是出現了不少問題啊,不過最後還是出來結果了,繼續努力啊!

 

 

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