用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

判断点是否在园内,可以自行决定判断次数

2017-08-03 作者:芙蓉妹妹举报

[c++]代码库

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
    static int numOfPoints;
    int x,y;
    Point(int a,int b)
    {
        x=a;
        y=b;
        numOfPoints++;
        cout<<"The Point ("<<x<<", "<<y<<") is created! Now, we have "<<numOfPoints<<" points."<<endl;
    }
    ~Point()
    {
        numOfPoints--;
        cout<<"A Point ("<<x<<", "<<y<<") is erased! Now, we have "<<numOfPoints<<" points."<<endl;
    }
    Point (const Point &p)
    {
        x=p.x;
        y=p.y;
        numOfPoints++;
        cout<<"A Point ("<<x<<", "<<y<<") is copied! Now, we have "<<numOfPoints<<" points."<<endl;
    }
    static int  getNumOfPoints()
    {
        return numOfPoints;
    }
    int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }
};
int Point::numOfPoints=0;
class Circle
{
private:
    Point center;
    int radius;
public:
    static int numOfCircles;
    Circle(int a,int b,int c):center(a,b)
    {
        radius=c;
        numOfCircles++;
        cout<<"A circle at ("<<center.x<<", "<<center.y<<") and radius "<<radius<<" is created! Now, we have "<<numOfCircles<<" circles."<<endl;
    }
    ~Circle()
    {
        numOfCircles--;
        cout<<"A circle at ("<<center.x<<", "<<center.y<<") and radius "<<radius<<" is erased! Now, we have "<<numOfCircles<<" circles."<<endl;
    }
    Circle(Point b,int a):center(b)
    {
        radius=a;
        numOfCircles++;
        cout<<"A circle at ("<<center.x<<", "<<center.y<<") and radius "<<radius<<" is created! Now, we have "<<numOfCircles<<" circles."<<endl;
    }
    static int getNumOfCircles()
    {
        return numOfCircles;
    }
    Point& getCenter()
    {
        return center;
    }
    bool pointInCircle(Point &p)
    {
        double d;
        d=sqrt((p.x-center.x)*(p.x-center.x)+(p.y-center.y)*(p.y-center.y));
        if(d<radius)
            return true;
        else
            return false;
    }
};
int Circle::numOfCircles=0;
int main()
{
    int cases,num;
    int x, y, r, px, py;
    Point aPoint(0,0), *bPoint;
    Circle aCircle(1,1,1);
    cin>>cases;
    cout<<"We have "<<Point::getNumOfPoints()<<" points and "<<Circle::getNumOfCircles()<<" circles now."<<endl;
    for (int i = 0; i < cases; i++)
    {
        cin>>x>>y>>r;
        bPoint = new Point(x,y);
        Circle circle(*bPoint, r);
        cin>>num;
        for (int j = 0; j < num; j++)
        {
            cin>>px>>py;
            if (circle.pointInCircle(*(new Point(px, py))))
            {
                cout<<"("<<px<<", "<<py<<") is in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
            else
            {
                cout<<"("<<px<<", "<<py<<") is not in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
        }
        delete bPoint;
    }
    cout<<"We have "<<Point::getNumOfPoints()<<" points, and "<<Circle::getNumOfCircles()<<" circles."<<endl;
    return 0;
}


分享到:
更多

网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。