/*
* 程序的版权和版本声明部分:
* Copyright (c) 2013.烟台大学计算机学院。
* All rights reserved.
* 文件名称:体验常成员函数
* 作 者:冯冬影
* 完成日期:2014 年 4月 1 日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分:
* 输入描述:
* 问题描述:
* 程序输出:
*/
#include <iostream>
#include <Cmath>
using namespace std;
class CPoint
{
private:
double x; // 横坐标
double y; // 纵坐标
public:
CPoint(double xx=0,double yy=0);
double Distance1(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)
double Distance0() const; // 到原点的距离
CPoint SymmetricAxis(char style) const;//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
CPoint::CPoint(double xx,double yy)
{
x=xx;
y=yy;
}
double CPoint::Distance1(CPoint p) const
{
double n;
n=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
return n;
}
double CPoint::Distance0() const
{
double m;
m=sqrt(x*x+y*y);
return m;
}
void CPoint::input() //以x,y 形式输入坐标点
{
cout<<"请输入一个点的坐标:";
cin>>x>>y;
}
void CPoint::output() //以(x,y) 形式输出坐标点
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
CPoint CPoint::SymmetricAxis(char style) const //style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
{
CPoint p(x,y);
if(style=='x')
p.y=-y;
else if(style=='y')
p.x=-x;
else if(style=='o')
{
p.x=-x;
p.y=-y;
}
return p;
}
int main()
{
CPoint p1,p3;
CPoint p2(5,4);
p1.input();
cout<<"p1到原点的距离为:"<<p1.Distance0()<<endl;
cout<<"p1,p2两点之间的距离为:"<<p1.Distance1(p2)<<endl;
cout<<"p1关于x轴对称的点的坐标为:";
p3= p1.SymmetricAxis('x');
p3.output();
cout<<"p1关于y轴对称的点的坐标为:";
p3=p1.SymmetricAxis('y');
p3.output();
cout<<"p1关于原点对称的点的坐标为:";
p3=p1.SymmetricAxis('o');
p3.output();
return 0;
}
运行结果