第六周任务三设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等

/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.* 文件名称:

* 作 者: 石丽君

* 完成日期: 2012年3 月27 日

* 版 本 号:

* 对任务及求解方法的描述部分

* 输入描述: 设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等

* 问题描述:

* 程序输出:

* 程序头部的注释结束*/

#include<iostream>
#include<Cmath>
using namespace std;
enum SymmetricStyle { axisx,axisy,point};//分别表示按x轴, y轴, 原点对称
class CPoint
{
private:
	mutable double x;  // 横坐标
	mutable 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;
}
//以x,y 形式输入坐标点
void CPoint::input()
{
	char c;
	double xx,yy;
	while(1)
	{
		cout<<"请按照x,y格式输入"<<endl;
		cin>>xx>>c>>yy;
		if(c!=',')
			cout<<"格式不对,请重新输入"<<endl;
		else
			break;
	}
	x=xx;
	y=yy;
}
// 到原点的距离
double CPoint::Distance0()const 
{
	double s;
	s=sqrt(x*x+y*y);
	return s;
}
// 两点之间的距离(一点是当前点,另一点为参数p)
double CPoint::Distance(CPoint p) const
{
	double s;
	s=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
	return s;
} 
// 返回对称点
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
	switch(style)
	{
	case axisx:
		y=-y;
		cout<<'('<<x<<','<<y<<')'<<endl;break;
	case axisy:
		x=-x;
		cout<<'('<<x<<','<<y<<')'<<endl;break;
	case point:
		x=-x;
		y=-y;
		cout<<'('<<x<<','<<y<<')'<<endl;break;
	}
	return 0;
}
//以(x,y) 形式输出坐标点
void CPoint::output()
{
	cout<<'('<<x<<','<<y<<')';
}
int main()
{
	CPoint c1(3,4),c2(5,6),c3;
	cout<<"这两点";
	c1. output();
	c2.output();
	cout<<"之间的距离为:"<<c1. Distance(c2);
	cout<<endl;
	c1. output();
	cout<<"到原点的距离为:"<<c1.Distance0();
	cout<<endl;
	c2. output();
	cout<<"到原点的距离为:"<<c2.Distance0();
	cout<<endl;
	c3.input();
	cout<<"这两点";
	c1. output();
	c3.output();
	cout<<"之间的距离为:"<<c3. Distance( c1);
	cout<<endl;
	c3. output();
	cout<<"到原点的距离为:"<<c3.Distance0();
	cout<<endl;
	c3.output();
	cout<<"关于x轴的对称点:";
	c3.SymmetricAxis(axisx);
	cout<<endl;
	c1.output();
	cout<<"关于y轴的对称点:";
	c1.SymmetricAxis(axisy);
	cout<<endl;
	c2.output();
	cout<<"关于原点的对称点:";
	c2.SymmetricAxis(point);
	cout<<endl;
	system("pause");
	return 0;
}


感言:用const的成员函数改变私有成员的值。

cout里面不能再加输出函数output.

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) Point类代码如下: ```python import math class Point: def __init__(self, x, y): self.x = x self.y = y def distance(self, other): return math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2) def distance_to_origin(self): return math.sqrt(self.x ** 2 + self.y ** 2) def reflect(self): return Point(-self.x, -self.y) ``` 其中,`__init__`方法初始化点的坐标,`distance`方法计算两点之间距离,`distance_to_origin`方法计算点到原点距离,`reflect`方法计算点关于坐标轴对称点。 (2) 建立点(3,5)和点(-2,4),计算它们之间距离、到原点距离、关于坐标轴对称点的代码如下: ```python point1 = Point(3, 5) point2 = Point(-2, 4) print("The distance between point1 and point2 is:", point1.distance(point2)) print("The distance from point1 to origin is:", point1.distance_to_origin()) print("The distance from point2 to origin is:", point2.distance_to_origin()) reflected_point1 = point1.reflect() reflected_point2 = point2.reflect() print("The reflection of point1 is:", reflected_point1.x, reflected_point1.y) print("The reflection of point2 is:", reflected_point2.x, reflected_point2.y) ``` 输出结果为: ``` The distance between point1 and point2 is: 5.0990195135927845 The distance from point1 to origin is: 5.830951894845301 The distance from point2 to origin is: 4.47213595499958 The reflection of point1 is: -3 -5 The reflection of point2 is: 2 -4 ``` 可以看到,点(3, 5)和点(-2, 4)之间距离为5.0990,点(3, 5)到原点距离为5.8309,点(-2, 4)到原点距离为4.4721。点(3, 5)关于坐标轴对称点为(-3, -5),点(-2, 4)关于坐标轴对称点为(2, -4)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值