【东华大学oj】12 长方形集合(面向对象)

12 长方形集合

作者: Turbo时间限制: 1S章节: 类与对象

问题描述 :

 定义一个Point类,包括两个私有属性:int x, int y,它们分别表示一个点的x和y座标。

再定义构造函数:

Point(int x, int y),即传两个参数,构造一个点对象。

定义一个Rectangle类,包括四个私有属性:

Int num, Point topLeft, int width, int height,

它们分别表示长方形的编号(长方形的编号不重复)、左上角顶点的座标,以及横向的宽度和纵向的高度。

注意:在计算机系统里,座标系如下定义:屏幕的左上角的座标是(0,0),x轴是横轴,屏幕的最右端x值最大,y轴是纵轴,屏幕的最下方y值最大。图如下:

1460516066979027970.jpg

再定义构造函数:

Rectangle(int num, int x, int y, int width, int height)

以及实例方法:

int getArea()    //获取该图形的面积

bool isIn(Point p)   //判断传入的点是否在该图形之内(不包括边界),如果在内部返回true,否则返回false

定义一个RectangleCollection类,包括两个私有属性:

Rectangle rects[100];   //一个包含长方形的数组,最多100个元素

int count;            //以上数组中长方形的实际个数

还包括实例方法:

void addRectangle(Rectangle r)//添加一个长方形到数组中,并count++

void deleteRectangle(int num) //根据num从数组中删除一个长方形(该长方形的编号等于num)

int inRects(Point p)//根据传入的p,判断p位于rects数组中的哪些长方形之内,返回这些长方形面积之和。

请根据自己的需要定义其它构造函数或者其它方法和属性。

使用main函数测试以上RectangleCollection类的addRectangle方法、deleteRectangle方法以及inRects方法。main函数可参考如下代码:

int main()
{
    int num, topLeftX, topLeftY, width, height;
    int px, py;
    int op;
    RectangleCollection rc;
    Rectangle r;
    Point p;

    while (cin >> op)
    {
        switch (op)
        {
            case 1:
                cin >> num >> topLeftX >> topLeftY >> width >> height;
                r =Rectangle(num, topLeftX, topLeftY, width, height);
                rc.addRectangle(r);
                break;
            case 2:
                cin >> num;
                rc.deleteRectangle(num);
                break;
            case 3:
                cin >> px >> py;
                p=Point(px, py);
                cout << rc.inRects(p) << endl;
                break;
        }
    }
    return 0;
}

输入说明 :

可输入多组测试数据,每组测试数据包含两行:
第一行输入一个操作的种类,
1:增加一个长方形
2:删除一个长方形
3:求包含某一指定点的所有长方形的面积和
第二行输入所需要的参数:
对于第1个操作,第二行输入长方形r的信息,包括编号num,左上角顶点x座标、左上角顶点y座标、宽度、高度。
对于第2个操作,第二行输入一个编号num
对于第3个操作,第二行输入一个点p的信息,包括其x座标和y座标。
所有输入都为非负整数,之间以一个空格分隔。无多余空格或空行,两组测试数据之间也无空行。
在输入时,将保证不会向长方形集合中添加一个编号(num)已经存在的长方形。

输出说明 :

仅第3个操作有输出,因此,测试数据中必然包含第3个操作。
第3个操作仅输出一个整数,占一行。
如果有多个输出,每个输出占一行,行首与行尾无空格,中间无空行。

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

class Point
{
    int x,y;
public:
    Point(int x,int y):x(x),y(y) {}
    int getX() const {return x;}
    int getY() const {return y;}
    Point() {}

};

class  Rectangle
{
    int num;
    Point topLeft;
    int width,height;
public:
    Rectangle(int num,int x,int y,int width,int height):num(num),topLeft(x,y),width(width),height(height)
    { }
    Rectangle() {}
    int getArea() const
    {
        return width*height;
    }

    bool isIn(Point p)const
    {
        if(p.getX()>topLeft.getX()&&p.getX()<topLeft.getX()+width&&p.getY()>topLeft.getY()&&p.getY()<topLeft.getY()+height)
            return true;
            else return false;
    }
    int getNum()const {return num;}


};

class RectangleCollection
{
    vector<Rectangle> rects;
    int count;
public:
    void addRectangle(const Rectangle&r)
    {
        rects.push_back(r);
    }

    void deleteRectangle(int num)
    {
        for(size_t i=0;i<rects.size();++i)
        {
            if(rects[i].getNum()==num)
            {
                rects.erase(rects.begin()+i);
                break;
            }
        }
    }
    int inRects(Point p)
    {
      int areaSum=0;
      for(const auto& rect:rects)
      {
          if(rect.isIn(p))
          {
              areaSum+=rect.getArea();
          }
      }
      return areaSum;
    }
};
int main()
{
    int num, topLeftX, topLeftY, width, height;
    int px, py;
    int op;
    RectangleCollection rc;
    Rectangle r;
    Point p;

    while (cin >> op)
    {
        switch (op)
        {
        case 1:
            cin >> num >> topLeftX >> topLeftY >> width >> height;
            r =Rectangle(num, topLeftX, topLeftY, width, height);
            rc.addRectangle(r);
            break;
        case 2:
            cin >> num;
            rc.deleteRectangle(num);
            break;
        case 3:
            cin >> px >> py;
            p=Point(px, py);
            cout << rc.inRects(p) << endl;
            break;
        }
    }
    return 0;
}

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值