/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张传新
* 完成日期: 2012 年 3 月 26 日
* 版 本 号: 1
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:……
* 算法设计:……
*/
#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=1,double yy=2):x(xx),y(yy){}
double Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)
double Distance0() const; // 到原点的距离
CPoint SymmetricAxis(SymmetricStyle style); // 返回对称点
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
void main()
{
CPoint a1;
CPoint a2;
a1.input();
a1.output();
a1.Distance0();
a1.Distance(a2);
a1.SymmetricAxis(axisx);
a1.SymmetricAxis(axisy);
a1.SymmetricAxis(point);
system("pause");
}
void CPoint::input() //以x,y 形式输入坐标点
{
char a;
cout<<"请以“x,y ”形式输入坐标点:"<<endl;
cin>>x>>a>>y;
if(a!=',')
{
exit(0);
}
}
void CPoint::output() //以(x,y) 形式输出坐标点
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
double CPoint::Distance0() const // 到原点的距离
{
cout<<"到原点的距离为:"<<endl;
cout<<sqrt(x*x+y*y)<<endl;
return 0;
}
double CPoint::Distance(CPoint p) const // 两点之间的距离(一点是当前点,另一点为参数p)
{
cout<<"与默认点的距离为:"<<endl;
cout<<sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y))<<endl;
return 0;
}
CPoint CPoint::SymmetricAxis(SymmetricStyle style) // 返回对称点
{
if(style == axisx)
{
y = -y;
}
if(style == axisy)
{
x = -x;
}
if(style == point)
{
x = -x;
y = -y;
}
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;
default:break;
}
return 0;
}
运行结果:
经验积累:
1.本程序用到了枚举,我又复习了一遍,效果不错。
2.以前对于this指针理解略有偏差,但经过同学指点,终于会了。
上机感言:
贺老师的作业真不错,不仅让我以前不会的学会了,而且让我刚学的也略有小成。
说实话刚看到这个题,头都大了,但这次我突然冷静下来,决定一个人闷头做,而且
告诫自己把程序分成几块来做,果然这种方法很起作用,虽然用时不短但我成功了
心里好爽,哈哈。。希望以后还能继续保持这种心态。。加油!