第6周-项目3-平面坐标点类

/*    
* 程序的版权和版本声明部分    
* Copyright (c)2013, 烟台大学计算机学院学生    
* All rightsreserved.    
* 文件名称: object.cpp    
* 作者:杨绍宁   
* 完成日期: 2013年  4  月 9 日    
* 版本号: v1.0    
* 输入描述:无    
* 问题描述:。    
* 程序输出:。    
*/        
#include <iostream>
#include<Cmath>
using namespace std;
class CPoint
{private:
  double x;  // 横坐标
  double y;  // 纵坐标
public:
  CPoint(double xx=0,double yy=0);
  double Distance(CPoint p) const;   // 两点之间的距离(一点是当前点,另一点为参数p)
  double Distance() 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()
{
	char c;
	cin>>x>>c>>y;
	if(c!=',')
	{
		cout<<"输入错误,请重新输入";
		exit(0);
	}
	
}
void CPoint::output() //以(x,y) 形式输出坐标点
{
	cout<<"("<<x<<","<<y<<")"<<endl;
}
double CPoint::Distance(CPoint p) const         // 两点之间的距离(一点是当前点,另一点为参数p)
{
	double s;
	s=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
	return s;
}
double CPoint::Distance() const        // 到原点的距离
{
	double s;
	s=sqrt(x*x+y*y);
	return s;

}
CPoint CPoint:: SymmetricAxis(char style)const//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
{
	CPoint p(x,y);
	switch(style)
	{
	case'x':
		p.y=-y;
		break;
	case'y':
		p.x=-x;
		break;
	case'o':
		p.x=-x;
		p.y=-y;
		break;
	}
	return p;
}
int main()
{
	CPoint p,p1,p2;
	cout<<"请输入第一个点;";
	p1.input();
	cout<<"请输入第二个点;";
	p2.input();
	cout<<"两地的距离为:"<<p1.Distance(p2);   //复制构造函数
	cout<<"到原点的距离"<<p1.Distance()<<endl;
	p=p1.SymmetricAxis('x');
	p.output();
	p=p1.SymmetricAxis('y');
	p.output();
	p=p1.SymmetricAxis('o');
	p.output();
	return 0;
}


结果:

第6周-项目3-平面坐标点类_第1张图片

感受:复制构造函数需要看看想想啊

你可能感兴趣的:(第6周-项目3-平面坐标点类)