/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: miao.cpp
* 作 者: 苗向前
* 完成日期: 2012 年 03 月27 日
* 版 本 号: V6.3
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
enum SymmetricStyle { axisx,axisy,point};//分别表示按x轴, y轴, 原点对称
#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; // 到原点的距离
CPoint SymmetricAxis(SymmetricStyle style) const; // 返回对称点
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
CPoint::CPoint(double xx,double yy)
{
x = xx;
y = yy;
}
// 两点之间的距离(一点是当前点,另一点为参数p)
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
{
CPoint p(this->x,this->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;
}
//以x,y 形式输入坐标点
void CPoint::input()
{
while(1)
{
char c;
cin>>x>>c>>y;
if(c != ',')
{
cout<<"格式错误!重输入!";
}
else
break;
}
}
//以(x,y) 形式输出坐标点
void CPoint::output()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
void main()
{
double s;
CPoint p1,p2,p;
cout<<"请输入p1的坐标:"<<endl;
p1.input();
cout<<"请输入p2的坐标:"<<endl;
p2.input();
s = p1.Distance(p2);
cout<<"p1,p2两点的距离为:"<< s << endl;
s = p1.Distance0();
cout<<"p1到原点的距离为"<<s<<endl;
p=p1.SymmetricAxis(axisx);
cout<<"p1关于x轴对称点为";
p.output();
p=p1.SymmetricAxis(axisy);
cout<<"p1关于y轴对称点为";
p.output();
p=p1.SymmetricAxis(point);
cout<<"p1关于原点对称点为";
p.output();
system("pause");
}
运行结果:
上机感言:
关于对象当做参数的应用应该注意一下、当返回值应是一类数据时,应该往这方面考虑。两点距离的求法就像路人甲请路人乙回家做客一样、