1187:矩形关系(运算符重载)

问题 C: 矩形关系(运算符重载)
时间限制: 1 Sec 内存限制: 128 MB
提交: 231 解决: 88
[提交][状态][讨论版]
题目描述
假设坐标采用二维平面坐标。

定义点类CPoint,包含属性x,y(整型)。方法有:带参构造函数,getX,getY分别返回点的x坐标,y坐标。

定义矩形类CRectangle,包含属性:矩形的左上角坐标leftPoint,右下角坐标rightPoint。类中方法有:

1)带参构造函数,初始化矩形的左上角、右下角

2)重载>运算符,参数为CPoint点对象,假设为p,若p在矩形内,返回true,否则返回false。

3)重载>运算符,第一个矩形若包含第二个矩形(部分边界可以相等),返回true,否则返回false。(要求该函数调用2)实现)

4)重载==运算符,判断两个矩形是否一致,返回true或false。

5)重载*运算符,判断两个矩形是否有重叠部分,返回true或false。

6)重载类型转换运算符,计算矩形的面积并返回,面积是整型。

7)重载《运算符,输出矩形的两个角坐标,具体格式见样例。

输入2个矩形,计算面积,判断矩形的关系。主函数如下,不可修改。

可根据需要,添加构造函数和析构函数。

输入
测试次数

每组测试数据如下:

矩形1的左上角、右下角坐标

矩形2的左上角、右下角坐标

输出
每组测试数据输出如下,中间以空行分隔:

矩形1的坐标和面积(具体格式见样例)

矩形2的坐标和面积(具体格式见样例)

矩形1和矩形2的关系(矩形1包含矩形2、矩形2包含矩形1、矩形2和矩形1相等、矩形1和矩形2相交、矩形1和矩形2不相交)

样例输入
2
1 4 4 1
2 3 3 2
1 4 4 1
0 3 5 2
样例输出
矩形1:1 4 4 1 9
矩形2:2 3 3 2 1
矩形1包含矩形2

矩形1:1 4 4 1 9
矩形2:0 3 5 2 5
矩形1和矩形2相交
提示.

#include
using namespace std;
class CPoint {
	int x, y;
public:
	CPoint() {};
	CPoint(int _x, int _y) :x(_x), y(_y) {};

	int getX() { return x; }
	int getY() { return y; }
	bool operator == (const CPoint &obj) {
		return x == obj.x && y == obj.y;
	}

};
class CRectangle {
	CPoint leftPoint, rightPoint;
public:
	CRectangle() {};
	CRectangle(int x1, int y1, int x2, int y2) :leftPoint(x1, y1), rightPoint(x2, y2) {};
	bool operator >(CPoint& c) {
		return c.getY() >= rightPoint.getY() && c.getY() <= leftPoint.getY() && c.getX() <= rightPoint.getX() && c.getX() >= leftPoint.getX();
	}
	bool operator >(CRectangle& c) {
		return c.rightPoint.getY() >= rightPoint.getY() && c.rightPoint.getX() >= 
			leftPoint.getX() && c.rightPoint.getX() <= rightPoint.getX() && c.rightPoint.getY() <= leftPoint.getY()&& 
			c.leftPoint.getY() >= rightPoint.getY() && c.leftPoint.getX() >=
			leftPoint.getX() && c.leftPoint.getX() <= rightPoint.getX() && c.leftPoint.getY() <= leftPoint.getY()
			;
	}
	bool operator ==(CRectangle& obj) {
		return (leftPoint == obj.leftPoint) && (rightPoint == obj.rightPoint);
	}
	operator int() {
		return (leftPoint.getY() - rightPoint.getY()) * (rightPoint.getX() - leftPoint.getX());
	}
	friend ostream& operator <<(ostream& os, CRectangle& d);
	bool operator *(CRectangle& obj) {
		return obj.leftPoint.getY() >= rightPoint.getY() && obj.leftPoint.getY() <= leftPoint.getY() || obj.leftPoint.getX() <= rightPoint.getX() && obj.leftPoint.getX() >= leftPoint.getX();
	}
};
ostream& operator <<(ostream& os, CRectangle& d) {

	os << d.leftPoint.getX() << " " << d.leftPoint.getY() << " " << d.rightPoint.getX() << " " << d.rightPoint.getY();
	return os;
}
int main() {
	int t, x1, x2, y1, y2;
	cin >> t;
	while (t--) {
		cin >> x1 >> y1 >> x2 >> y2;
		CRectangle rect1(x1, y1, x2, y2);
		cin >> x1 >> y1 >> x2 >> y2;
		CRectangle rect2(x1, y1, x2, y2);
		cout << "矩形1:" << rect1 << " " << (int)rect1 << endl;
		cout << "矩形2:" << rect2 << " " << (int)rect2 << endl;

		if (rect1 == rect2)
			cout << "矩形1和矩形2相等" << endl;
		else if (rect2 > rect1)
			cout << "矩形2包含矩形1" << endl;
		else if (rect1 > rect2)
			cout << "矩形1包含矩形2" << endl;
		else if (rect1 * rect2)
			cout << "矩形1和矩形2相交" << endl;
		else
			cout << "矩形1和矩形2不相交" << endl;
		cout << endl;
	}
	return 0;
}

你可能感兴趣的:(c++,开发语言,后端)