/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者:王引琳
* 完成日期: 2012 年 3 月 28 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
【任务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;
}
double CPoint:: Distance(CPoint p) const
{
double d;
d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
cout<<"此两点之间的距离为:"<<d<<endl;
return 0;
}
double CPoint::Distance0() const
{
double d;
d=sqrt((x*x)+(y*y));
cout<<"到原点的距离为:"<<d<<endl;
return 0;
}
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
switch (style)
{
case axisx:cout<<"关于x轴对称是:"<<"("<<x<<","<<-y<<")"<<endl;break;
case axisy:cout<<"关于y轴对称是:"<<"("<<-x<<","<<y<<")"<<endl;break;
case point:cout<<"关于原点对称是:"<<"("<<-x<<","<<-y<<")"<<endl;break;
}
return 0;
}
void CPoint::input()
{
char c;
cout<<"请按x,y的形式输入:";
while(1)
{
cin>>x>>c>>y;
if(c != ',')
cout<<"格式错误!重新输入"<<endl;
else
break;
}
}
void CPoint::output()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
void main()
{
CPoint t1,t2,t3;
cout<<"请输入第一个点:";
t1.input();
t1.output();
cout<<"请输入第二个点:";
t2.input();
t2.output();
t1.Distance(t2);
t2.Distance0() ;
cout<<"请输入第三个点:";
t3.input();
t3.output();
t3.SymmetricAxis(axisx);
t3.SymmetricAxis(axisy);
t3.SymmetricAxis(point);
}
上机感言:枚举类型刚开始做时好抽象啊。。。只是用老师那个方法做时,(switch case axisx:p.y=-y;break;.......)最后为什么啥也输不出呢?