6_3平面坐标点类

2014-11-24 03:12:42 · 作者: · 浏览: 0

[cpp]
//
// main.cpp
// 6_3.cpp
//
// Created by 纪 子龙 on 13-4-10.
// Copyright (c) 2013年 纪 子龙. All rights reserved.
//
#include
#include
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(char style)const;//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
CPoint::CPoint(double xx,double yy)
{
x=xx;
y=yy;
}
void CPoint::input()
{
std::cout<<"pelease input x,y"< std::cin>>x>>y;

}
void CPoint::output()
{
std::cout<<"("< }
double CPoint::Distance(CPoint p) const//此处是一个常成员函数
{
double d;
d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
return d;
}
double CPoint::Distance0() const
{
double d;
d=sqrt(x*x+y*y);
return d;
}
CPoint CPoint::SymmetricAxis(char style)const
{
CPoint p;
p.x=x;
p.y=y;//该处也可用复制构造函数,要写作p(*this)
switch (style)
{
case 'x':
p.y=-y;
break;
case 'y':
p.x=-x;
break;
case 'o':
p.x=-x;
p.y=-y;
break;
default:
break;
}
return p;
}
int main( )
{
double distance;
CPoint a,b,p;
std::cout<<"请输入点a坐标";
a.input();
std::cout<<"请输入点b坐标";
b.input();
distance=a.Distance(b);
std::cout<<"两点的距离为:"< distance=a.Distance0();
std::cout<<"a到原点的距离为:"< p=a.SymmetricAxis('x');
std::cout<<"a关于x轴的对称点为:";
p.output();
p=a.SymmetricAxis('y');
std::cout<<"a关于y轴的对称点为:";
p.output(); p=a.SymmetricAxis('o');
std::cout<<"a关于原点的对称点为:";
p.output();
return 0;
}
运行结果:

//
// main.cpp
// 6_3.cpp
//
// Created by 纪 子龙 on 13-4-10.
// Copyright (c) 2013年 纪 子龙. All rights reserved.
//
#include
#include
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(char style)const;//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
};
CPoint::CPoint(double xx,double yy)
{
x=xx;
y=yy;
}
void CPoint::input()
{
std::cout<<"pelease input x,y"< std::cin>>x>>y;

}
void CPoint::output()
{
std::cout<<"("< }
double CPoint::Distance(CPoint p) const//此处是一个常成员函数
{
double d;
d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
return d;
}
double CPoint::Distance0() const
{
double d;
d=sqrt(x*x+y*y);
return d;
}
CPoint CPoint::SymmetricAxis(char style)const
{
CPoint p;
p.x=x;
p.y=y;//该处也可用复制构造函数,要写作p(*this)
switch (style)
{
case 'x':
p.y=-y;
break;
case 'y':
p.x=-x;
break;
case 'o':
p.x=-x;
p.y=-y;
break;
default:
break;
}
return p;
}
int main( )
{
double distance;
CPoint a,b,p;
std::cout<<"请输入点a坐标";
a.input();
std::cout<<"请输入点b坐标";
b.input();
distance=a.Distance(b);
std::cout<<"两点的距离为:"< distance=a.Distance0();
std::cout<<"a到原点的距离为:"< p=a.SymmetricAxis('x');
std::cout<<"a关于x轴的对称点为:";
p.output();
p=a.SymmetricAxis('y');
std::cout<<"a关于y轴的对称点为:";
p.output(); p=a.SymmetricAxis('o');
std::cout<<"a关于原点的对称点为:";
p.output();
return 0;
}
运行结果:

\