问题描述 :
定义一个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值最大。图如下:
再定义构造函数:
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个操作仅输出一个整数,占一行。
如果有多个输出,每个输出占一行,行首与行尾无空格,中间无空行。
输入范例 :
1
1 4 4 4 4
1
3 3 3 3 3
1
2 2 3 5 4
3
5 5
2
3
3
5 5
1
3 3 3 4 4
3
5 5
输出范例 :
45
36
52
解题代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(void);
Point(int a, int b);
void inside(int a, int b);
int get_x(void);
int get_y(void);
};
Point::Point(int a, int b)
{
x = a;
y = b;
}
Point::Point(void)
{
;
}
int Point::get_x(void)
{
return x;
}
int Point::get_y(void)
{
return y;
}
void Point::inside(int a, int b)
{
x = a;
y = b;
return;
}
class Rectangle {
private:
int num;
Point topLeft;
int width;
int height;
public:
Rectangle(int num, int x, int y, int width, int height);
Rectangle(void);
int getnum(void);
int getArea(); //获取该图形的面积
bool isIn(Point p); //判断传入的点是否在该图形之内(不包括边界),如果在内部返回true,否则返回false
};
int Rectangle::getnum(void)
{
return num;
}
Rectangle::Rectangle(void)
{
return;
}
int Rectangle::getArea()
{
return width * height;
}
bool Rectangle::isIn(Point p)
{
bool ok1 = 0,ok2;
if (topLeft.get_x() < p.get_x() && p.get_x() < topLeft.get_x() + width)
ok1 = 1;
else
ok1 = 0;
if (topLeft.get_y() < p.get_y() && p.get_y() < topLeft.get_y() + height)
ok2 = 1;
else
ok2 = 0;
return (ok1 && ok2);
}
Rectangle::Rectangle(int num, int x, int y, int width, int height)
{
Rectangle::width = width;
Rectangle::height = height;
Rectangle::num = num;
topLeft.inside(x, y);
return;
}
class RectangleCollection {
private:
Rectangle rects[100];
int count;
public:
RectangleCollection(void);
void addRectangle(Rectangle r);//添加一个长方形到数组中,并count++
void deleteRectangle(int num); //根据num从数组中删除一个长方形(该长方形的编号等于num)
int inRects(Point p);//根据传入的p,判断p位于rects数组中的哪些长方形之内,返回这些长方形面积之和。
};
void RectangleCollection::addRectangle(Rectangle r)
{
rects[count++] = r;
return;
}
void RectangleCollection::deleteRectangle(int num)
{
int i, j;
for (i = 0; i < count; i++)
{
if (rects[i].getnum() == num)
{
for (j = i + 1; j < count; j++)
rects[j - 1] = rects[j];
count--;
break;
}
}
return;
}
int RectangleCollection::inRects(Point p)
{
long long int all = 0;
int i;
for (i = 0; i < count; i++)
{
if (rects[i].isIn(p))
all += rects[i].getArea();
}
return all;
}
RectangleCollection::RectangleCollection(void)
{
count = 0;
return;
}
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;
}