#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指针运用不太会,代码部分借鉴..