/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 平面坐标点类
* 作 者: 邹议鹏
* 完成日期: 2012 年 03 月 28 日
* 版 本 号: v.03
* 对任务及求解方法的描述部分
* 问题描述: 设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等
* 程序头部的注释结束
*/
#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):x(xx), y(yy){};
double Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)
double Distance0() const; // 到原点的距离
CPoint SymmetricAxis(SymmetricStyle style) const; // 返回对称点
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
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);
}
CPoint CPoint:: SymmetricAxis(SymmetricStyle style) const
{
switch (style)
{
case axisx:
cout << "点(" << x << "," << y << ")" << "关于x轴的坐标为:(" << x << "," << -y << ")" << endl;
case axisy:
cout << "点(" << x << "," << y << ")" << "关于y轴的坐标为:(" << -x << "," << y << ")" << endl;
case point:
cout << "点(" << x << "," << y << ")" << "关于原点的坐标为:(" << -x << "," << -y << ")" << endl;
}
return 0;
}
void CPoint:: input()
{
char ch = ',';
cout << "请输入点的坐标:";
cin >> x >> ch >> y;
while (1)
{
if(ch != ',')
{
cout << "格式有误,请重新输入!";
cin >> x >> ch >> y;
}
else
{
break;
}
}
}
void CPoint:: output()
{
cout << "点(" << x << "," << y << ")";
}
void main()
{
CPoint p1,p2;
p1.input();
p2.input();
cout << "两点间的距离为:" << p1.Distance(p2) << endl;
cout << "点p1距离原点的距离为:" << p1.Distance0() << endl;
cout << "点p2距离原点的距离为:" << p2.Distance0() << endl;
p1.SymmetricAxis(axisx);
p2.SymmetricAxis(axisx);
cout <<endl;
system("pause");
}
图: