#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=1,double yy=2):x(xx),y(yy){}
double Distance(CPoint p) const; // 兩點之間的距離(一點是當前點,另一點爲參數p)
double Distance0() const; // 到原點的距離
CPoint SymmetricAxis(SymmetricStyle style); // 返回對稱點
void input(); //以x,y 形式輸入座標點
void output(); //以(x,y) 形式輸出座標點
};
void main()
{
CPoint a1;
CPoint a2;
a1.input();
a1.output();
a1.Distance0();
a1.Distance(a2);
a1.SymmetricAxis(axisx);
a1.SymmetricAxis(axisy);
a1.SymmetricAxis(point);
system("pause");
}
void CPoint::input() //以x,y 形式輸入座標點
{
char a;
cout<<"請以“x,y ”形式輸入座標點:"<<endl;
cin>>x>>a>>y;
if(a!=',')
{
exit(0);
}
}
void CPoint::output() //以(x,y) 形式輸出座標點
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
double CPoint::Distance0() const // 到原點的距離
{
cout<<"到原點的距離:"<<endl;
cout<<sqrt(x*x+y*y)<<endl;
return 0;
}
double CPoint::Distance(CPoint p) const
cout<<"與默認點的距離爲:"<<endl;
cout<<sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y))<<endl;
return 0;
}
CPoint CPoint::SymmetricAxis(SymmetricStyle style) // 返回對稱點
{
if(style == axisx)
{
y = -y;
}
if(style == axisy)
{
x = -x;
}
if(style == point)
{
x = -x;
y = -y;
}
switch(style)
{
case axisx:cout<<"關於x軸的對稱點:("<<x<<","<<y<<")"<<endl;break;
case axisy:cout<<"關於y軸的對稱點:("<<x<<","<<y<<")"<<endl;break;
case point:cout<<"關於原點的對稱點:("<<x<<","<<y<<")"<<endl;break;
default:break;
}
return 0;
}
this指針運用不太會,代碼部分借鑑..
第六週實驗報告 任務三
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.