C++权限案例点到圆的距离

本案例主要在于用到类中里面使用了一个类。
及带有类的模块化的编程
eg:

#include<iostream>

using namespace std;

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

        }
        int gety()
        {
         return m_y;
        }


private:
	int m_x;
	int m_y;
};

class circle
{
public:
	void setR(int r)
	{
	   m_r=r;	
	}
	int getR()
	{
	return m_r;
	}
	void setCentral(point central)
	{
		m_central = central;
	}
	point getCentral()
	{
		
		return m_central;
	}
	int ifCircleandPoint(point &point1,circle &Acircle)
	{
		int flag=0;
		flag=((point1.getx()-Acircle.getCentral().getx())*(point1.getx()-Acircle.getCentral().getx())+(point1.gety()-Acircle.getCentral().gety())*(point1.gety()-Acircle.getCentral().gety())==(Acircle.getR()*Acircle.getR()));
	        if(flag == 1)
		{
			return 0;		
				}
		flag=((point1.getx()-Acircle.getCentral().getx())*(point1.getx()-Acircle.getCentral().getx())+(point1.gety()-Acircle.getCentral().gety())*(point1.gety()-Acircle.getCentral().gety())>(Acircle.getR()*Acircle.getR()));
		 if(flag==1)
		{
			return 1;
				}
		else
		{
			return 2;
				}
	}
private:
	int m_r;
	point m_central;

};


int main()
{
	circle a1;
	point b1;
	point center;
	int flag;
	center.setx(10);
	center.sety(0);
	b1.setx(10);
	b1.sety(100);
	a1.setR(10);
	a1.setCentral(center);
	flag=a1.ifCircleandPoint(b1,a1);
	if(flag == 0)
	{
		cout<<"on the circle"<<endl;
	}
	else if(flag == 1)
	{
		cout<<"out the circle"<<endl;	
	}
	else if(flag == 2)
	{	
		cout<<"infer the circle"<<endl;		
	}


	return 0;
}

eg:
5个文件
在这里插入图片描述
circle.cpp

#include"circle.h"
	void circle::setR(int r)
	{
	   m_r=r;	
	}
	int circle::getR()
	{
	return m_r;
	}
	void circle::setCentral(point central)
	{
		m_central = central;
	}
	point circle::getCentral()
	{
		
		return m_central;
	}
	int circle::ifCircleandPoint(point &point1,circle &Acircle)
	{
		int flag=0;
	flag=((point1.getx()-Acircle.getCentral().getx())*(point1.getx()-Acircle.getCentral().getx())+(point1.gety()-Acircle.getCentral().gety())*(point1.gety()-Acircle.getCentral().gety())==(Acircle.getR()*Acircle.getR()));
	        if(flag == 1)
		{
			return 0;		
				}
	flag=((point1.getx()-Acircle.getCentral().getx())*(point1.getx()-Acircle.getCentral().getx())+(point1.gety()-Acircle.getCentral().gety())*(point1.gety()-Acircle.getCentral().gety())>(Acircle.getR()*Acircle.getR()));
		 if(flag==1)
		{
			return 1;
				}
		else
		{
			return 2;
				}
	}

circle.h

#pragma once
#include<iostream>
#include"point.h"
using namespace std;
class circle
{
public:
	void setR(int r);
	int getR();
	void setCentral(point central);
	point getCentral();
	int ifCircleandPoint(point &point1,circle &Acircle);
private:
	int m_r;
	point m_central;

};

point.cpp

#include"point.h"


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

        }
        int point::gety()
        {
         return m_y;
        }
point.h
#pragma once//restrict repeating headerfile
#include<iostream>

using namespace std;

class point
{
public:	
	void setx(int x);
	int getx();
	void sety(int y);
        int gety();


private:
	int m_x;
	int m_y;
};

mainpro.cpp

#include"point.h"
#include"circle.h"

int main()
{
	circle a1;
	point b1;
	point center;
	int flag;
	center.setx(10);
	center.sety(0);
	b1.setx(10);
	b1.sety(100);
	a1.setR(10);
	a1.setCentral(center);
	flag=a1.ifCircleandPoint(b1,a1);
	if(flag == 0)
	{
		cout<<"on the circle"<<endl;
	}
	else if(flag == 1)
	{
		cout<<"out the circle"<<endl;	
	}
	else if(flag == 2)
	{	
		cout<<"infer the circle"<<endl;		
	}




	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值