第六周实验报告 任务三(改进)

本文介绍了一个使用C++实现的图形类CPoint,该类可以处理二维平面上的点,并提供了计算两点间距离、到原点的距离及求取不同对称点的功能。通过实例演示了如何输入坐标点并调用这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源代码:

#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;
}

double CPoint::Distance(CPoint p) const
{
	return(sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)));
}

double CPoint::Distance0() const
{
	return(sqrt((x - 0) * (x - 0) + (y - 0) * (y - 0)));
}

void CPoint::input()
{
	char z;
	
	while(1)
	{
		cout << "请以 x , y 形式输入坐标点:";

		cin >> x >> z >> y;
		
		if(z == ',')
		{
			break;
		}
		
		else
		{
			cout<< "输入格式有误!!!" << endl;
		}
	}
}

void CPoint::output()
{
	cout << "(" << x << "," << y << ")" << endl;
}

CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
	CPoint p;
	
	switch(style)
	{
	case axisx: p.y = -y; p.x = x;break;
		
	case axisy: p.x = -x; p.y = y;break;
		
	case point: p.x = -x; p.y = -y; break;
		
	default: break;
	}
	return p;//返回一个类类型的变量
}

void main()
{
	CPoint a,  b(1,2), p;
	
	a.input();
	
	cout << "输入点到原点的距离为:" << a.Distance0() << endl;
	
	cout << "输入点到b点(1,2)的距离为:" << a.Distance(b) <<endl;
	
	p = a.SymmetricAxis(axisx);
	
    cout << "输入点关于x轴的对称点为:";
	
	p.output();
	
	p = a.SymmetricAxis(axisy);
	
	cout << "输入点关于y轴的对称点为:";
	
	p.output();
	
	p = a.SymmetricAxis(point);
	
	cout << "输入点关于原点的对称点为:";
	
	p.output();
	
	system("pause");
}


截图:

 

这个是周一晚上到贺老那里听了讲解后改得···觉得简单了好多···也容易理解了···之前自己做的那个有些地方自己都有点摸不着头脑~以后要坚持去!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值