/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: CPoint.cpp
* 作 者: 计114-3 王兴锋
* 完成日期: 2012年 3 月 26 日
* 版 本 号: V 1.0
* 对任务及求解方法的描述部分
* 输入描述: 输入点的坐标
* 问题描述: 设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等
* 程序输出: 按要求输出距离,对称点
* 程序头部的注释结束
*/
#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 = 0, double yy = 0);
double Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)
double Distance0() const; // 到原点的距离
CPoint SymmetricAxis(SymmetricStyle style) const; // 返回对称点
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
CPoint::CPoint(double xx, double yy)
{
x = xx, y = yy;
}
double CPoint::Distance(CPoint p) const
{
return sqrt((p.x-x)*(p.x-x) + (p.y-y)*(p.y-y));
}
double CPoint::Distance0() const
{
return sqrt(x*x + y*y);
}
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
double s_x = x, s_y = y;
switch(style)
{
case axisx:
s_y = -y;
break;
case axisy:
s_x = -x;
break;
case point:
s_x = -x;
s_y = -y;
break;
};
CPoint cp(s_x, s_y);//定义一个对象(点)将s_x,s_y的值初始化给此对象
return cp;//返回此对象(点)
}
void CPoint::input()
{
char ch;
do{
cout << "请输入点的坐标(例:x,y):" << endl;
cin >> x >> ch >> y;
}while(ch != ',');
}
void CPoint::output()
{
cout << "(" << x << "," << y << ")";
}
int main()
{
CPoint C;//定义当前点
C.input();
CPoint cp1(0.0, 1.0), cp2(1.0, 0.0);//定义参数点
//两点之间的距离
C.output();
cout << "到" ;
cp1.output();
cout << "的距离为:" << C.Distance(cp1) << endl;
C.output();
cout << "到" ;
cp2.output();
cout << "的距离为:" << C.Distance(cp2) << endl;
C.output();
cout << "到原点的距离为:" << C.Distance0() << endl;
cout << endl;
//对称点
C.output();
cout << "关于x轴的对称点为:";
C.SymmetricAxis(axisx).output();
cout << endl;
C.output();
cout << "关于y轴的对称点为:";
C.SymmetricAxis(axisy).output();
cout << endl;
C.output();
cout << "关于原点的对称点为:";
C.SymmetricAxis(point).output();
cout << endl;
system("pause");
return 0;
}
/*
此程序把对象的真正含义体现出来了,
首先是参数是对象,
再者,返回值类型是对象。
如果没有很好的理解对象的含义的话,
作这道题有点难理解其题意。
返回值类型是对象可以直接调用其方法。
以上均是我个人之见。
*/