C++结构体分文件编写

主文件“class.cpp”

实现判断点和圆的位置关系,在园内,在圆上,在圆外

#include<iostream>
#include<string>
#include "point.h"
#include "circle.h"

using namespace std;

//判断点和圆之间的关系
void isInCircle(Circle &c, point &p)
{
	int distance;
	//计算点和圆之间的距离
	distance = pow((p.getx() - c.getcentor().getx()), 2) + pow((p.gety() - c.getcentor().gety()), 2);
	if (distance == pow(c.getR(), 2))
	{
		cout << "点在圆上" << endl;
	}
	else if(distance > pow(c.getR(), 2))
	{
		cout << "点在圆外" << endl;
	}
	else if (distance < pow(c.getR(), 2))
	{
		cout << "点在圆内" << endl;
	}
}
int main() 
{
	point P, centor1;
	Circle C;
	P.setx(0);
	P.sety(0);
	cout << "点X: " << P.getx();
	cout << "点Y: " << P.gety();
	centor1.setx(9);
	centor1.sety(0);
	C.setr(10);
	
	C.setcentor(centor1);
	cout << "半径R: " << C.getR();
	cout << "点x: " << C.getcentor().getx();
	cout << "点Y: " << C.getcentor().gety();
	isInCircle(C, P);

	
	system("pause");
	return 0;
}

point.h

#pragma once
#include<iostream>
using namespace std;

class point
{
public:
	void setx(int x);

	void sety(int y);

	int getx();

	int gety();

private:
	int m_x;
	int m_y;
};

point.cpp

#include"point.h"


void point::setx(int x)
{
	m_x = x;
}
void point::sety(int y)
{
	m_y = y;
}
int point::getx()
{
	return m_x;
}
int point::gety()
{
	return m_y;
}

circle.cpp

#include "circle.h"


void Circle::setr(int r)
{
	m_R = r;
}
//设置圆心
void Circle::setcentor(point centor)
{
	m_centor = centor;
}
point Circle::getcentor()
{
	return m_centor;
}
int Circle::getR()
{
	return m_R;
}

circle.h

#pragma once
#include<iostream>
#include "point.h"
using namespace std;

class Circle
{
public:

	void setr(int r);

	//设置圆心
	void setcentor(point centor);

	point getcentor();

	int getR();

private:
	int m_R;
	point m_centor;
};

[1] https://www.bilibili.com/video/BV1et411b73Z?p=105

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值