* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 坐标
* 作 者: 李瑞
* 完成日期: 2012 年3 月 25日
* 版 本 号: v1.0
* 对任务及求解方法的描述部分
* 输入描述:……
* 问题描述:……
* 程序输出:……
* 程序头部的注释结束
/*【任务3】设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等*/
#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){}
int main()
{
CPoint p1, p2;
p1.input();
p2.input();
p1.output();
p2.output();
cout << "两点间的距离是:" << p1.Distance(p2) << endl;
cout << "点到原点的距离是:" << p2.Distance0() << endl;
p1.SymmetricAxis(axisx);
p1.SymmetricAxis(axisy);
p1.SymmetricAxis(point);
system("pause");
return 0;
}
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
switch(style)
{
case(axisx):
{
cout << "(" << x << "," << y << ")"<< "关于x轴对称点是: (" << x << "," << - y << ")" << endl;
break;
}
case(axisy):
{
cout << "(" << x << "," << y << ")" << "关于y轴对称点是: (" << - x << "," << y << ")" << endl;
break;
}
default:
{
cout << "(" << x << "," << y << ")"<< "关于原点对称点是: (" << - x << "," << - y << ")" << endl;
break;
}
}
return (x, y);
}
double CPoint::Distance(CPoint p) const // 计算两点之间的距离(一点是当前点,另一点为参数p)
{
double dis;
dis = sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
return dis;
}
double CPoint::Distance0() const //计算当前点到原点的距离
{
double dis;
dis = sqrt(x * x + y * y);
return dis;
}
void CPoint::input() //以x,y 形式输入坐标点
{
char comma;
cout << "请输入点坐标,格式: x,y" << endl;
while(1)
{
cin >> x >> comma >> y ;
if(comma != ',')
{
cout << "格式不正确,请重新输入:" << endl;
}
else
{
break;
}
}
}
void CPoint::output()
{
cout << "点(" << x << "," << y << ")" << endl;
}
运行结果:
上机感言:
由于给出了 点类 中的各个成员函数, 所以思路还是比较清晰的。只是在编写具体的函数时想的不是太全面,比如 void CPoint::input() 函数,我第一遍写的时候其实是没有想到判断格式的(我认为只要说明了格式,用户就不会输错,其实不然。)还得考虑的更细心点才行啊。