/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:孙永
* 完成日期: 2013年4 月 8 日
* 版本号: 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 Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)
double Distance0() const; // 到原点的距离
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
CPoint::CPoint(double xx,double yy)
{
x=xx;
y=yy;
}
void CPoint::input ()
{
cout<<"请以坐标(X,Y)的形式输入:";
char ch;
cin>>x>>ch>>y;
while(ch!=',')
cout<<"输入格式错误!"<<endl;
}
void CPoint::output ()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
double CPoint::Distance(CPoint p) const
{
return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
double CPoint::Distance0() const
{
return sqrt(x*x+y*y);
}
int main()
{
double distance;
CPoint p1,p2;
cout<<"请输入第一个点的坐标,";
p1.input();
cout<<"请输入第二个点的坐标,";
p2.input();
distance=p1.Distance(p2);
cout<<"两点之间的距离是:"<<distance<<endl;
distance=p1.Distance0();
cout<<"到原点之间的距离是:"<<distance<<endl;
return 0;
}
运行结果: