/** 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:设计平面坐标点类
* 作 者: 计114-3 杨坛坛
* 完成日期: 2012 年 3 月 28 日
* 版本号: VC 2008
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:计算两点之间的距离,到原点的距离,关于坐标轴和原点的对称点等
* 程序输出:
* 问题分析:
* 算法设计:
*/
#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)形式输出坐标点
};
int main()
{
Cpoint c1, c2;
cout << "请输入c1的坐标:";
c1.input();
cout << "请输入c2的坐标:";
c2.input();
cout << "两点之间的距离为:"<< c2.Distance(c1) << endl;
cout << "到原点的距离为:" << c2.Distance0() << endl;
c2 = c2.SymmetricAxis(axisx);
cout << "c2关于x轴的对称点为:";
c2.output();
c2 = c2.SymmetricAxis(axisy);
cout << "c2关于y轴的对称点为:";
c2.output();
c2 = c2.SymmetricAxis(point);
cout << "c2关于原点的对称点为:";
c2.output();
return 0;
}
Cpoint::Cpoint(double xx , double yy )
{
x = xx;
y = yy;
}
void Cpoint::input()
{
char ch;
while (1)
{
cin >> x >> ch >> y;
if(ch == ',')
{
break;
}
else
{
cout << "格式不正确,请重新输入" << endl;
}
}
}
void Cpoint::output()
{
cout << "(" << x << "," << y << ")" <<endl;
}
double Cpoint::Distance(Cpoint p) const
{
double d;
d = sqrt ((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
return d;
}
double Cpoint::Distance0() const
{
double d;
d = sqrt (x * x + y * y);
return d;
}
Cpoint 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;
}
return p;
}