您的位置:首页 > 其它

第五周项目1-体验常成员函数

2015-04-06 17:34 239 查看
/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:week5-.cpp
*作者:高赞
*完成日期:2015年 4 月 6 日
*版本号:v1.0
*
*
*/
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
private:
double x;  // 横坐标
double y;  // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy) {}
double Distance1(CPoint p) const; //两点之间的距离(一点是当前点——想到this了吗?,另一点为p)
double Distance0() const;          // 到原点(0,0)的距离
CPoint SymmetricAxis(char style) const;//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
void input()  //以x,y 形式输入坐标点
{
cin>>x>>y;
}
void output() //以(x,y) 形式输出坐标点
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
};
int main()
{
CPoint p1,p2(-2,-2),p3;
cout<<"输入p1坐标:";
p1.input();
cout<<"两点之间距离为"<<p1.Distance1(p2)<<endl
<<"p1到原点距离为"<<p1.Distance0()<<endl
<<"p1关于x轴对称点为";
p3=p1.SymmetricAxis('x');
p3.output();
cout<<"p1关于y轴对称点为";
p3=p1.SymmetricAxis('y');
p3.output();
cout<<"p1关于原点对称点为";
p3=p1.SymmetricAxis('o');
p3.output();
return 0;
}
double CPoint::Distance1(CPoint p) const
{
double a,b;
a=x-p.x;
b=y-p.y;
return sqrt(a*a+b*b);
}
double CPoint::Distance0() const
{
return sqrt(x*x+y*y);
}
CPoint CPoint::SymmetricAxis(char style) const
{
double x2,y2;
switch(style)
{
case 'x':
x2=x;
y2=-y;
break;
case 'y':
x2=-x;
y2=y;
break;
case 'o':
x2=-x;
y2=-y;
break;
default:
cout<<"error!"<<endl;
}
CPoint p2(x2,y2);
return p2;
}




最近要记得东西多了就容易记混,然后不明不白地犯各种低级错误
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: