#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=0);
double Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)
double Distance0() const; // 到原点的距离
SymmetricAxis(SymmetricStyle style) const; // 返回对称点
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
double CPoint::Distance(CPoint p) const// 两点之间的距离(一点是当前点,另一点为参数p)
{
double d;
d=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
cout<<d<<endl;
return 0;
}
double CPoint::Distance0() const// 到原点的距离
{
double d;
d=sqrt(x*x+y*y);
cout<<d<<endl;
return 0;
}
void CPoint::input()//以x,y 形式输入坐标点
{
cout<<"请输入坐标:"<<endl;
char c;
while (1)
{
cin>>x>>c>>y;
if(c==',')
break;
else
cout<<"格式不对,重输"<<endl;
}
}
/*void CPoint::output()//以(x,y) 形式输出坐标点
{
cout<<"("<<x<<','<<y<<')'<<endl;
}*/
CPoint::CPoint(double xx,double yy)//构造函数定义,不要加上具体的值
{
x=xx;
y=yy;
}
CPoint::SymmetricAxis(SymmetricStyle style) const// 返回对称点
{
CPoint p(x,y);//
switch (style)
{
case axisx:p.y=-y;break;
case axisy:p.x=-x;break;
case point:p.x=-x;p.y=-y;break;
}
cout<<'('<<p.x<<','<<p.y<<')'<<endl;
return 0;
}
void main()
{
CPoint p,p1,p2;
p1.input();
p2.input();
cout<<"p1到原点 的距离:"<<endl;
p1.Distance0();
cout<<"p1到p点的距离:"<<endl;
p1.Distance(p);//不能再加上CPoint了。
cout<<"p2到p点 的距离:"<<endl;
p2.Distance(p);
cout<<"p1关于x轴的对称点:"<<endl;
p1.SymmetricAxis(axisx);
cout<<"p1到p2点 的距离:"<<endl;
p1.Distance(p2);
cout<<"p1关于原点的对称点为:"<<endl;
p1.SymmetricAxis(point);
}
第六周任务3
最新推荐文章于 2025-10-31 00:11:54 发布
175





