用C++语言 创建一个矩形类,矩形的边平行于坐标轴。矩形的属性用其左下角坐标、长度和宽度描述。 (1) 从键盘输入两个矩形参数(注意输入提示信息),创建两个矩形对象, 判断两个矩形是否相交?求出相交部分的面积。 (2) 从键盘再输入或随机产生3个矩形对象,对输入的所有5个矩形按面积由小到大排序, 然后依次显示各个矩形的左下角坐标、长、宽、面积。
时间: 2024-03-02 17:52:32 浏览: 26
好的,我可以帮您解答您的问题。以下是用C++语言创建一个矩形类的代码:
```c++
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle(double x = 0, double y = 0, double w = 0, double h = 0) {
this->x = x;
this->y = y;
this->w = w;
this->h = h;
}
double getLeft() const {
return x;
}
double getRight() const {
return x + w;
}
double getTop() const {
return y + h;
}
double getBottom() const {
return y;
}
double getWidth() const {
return w;
}
double getHeight() const {
return h;
}
double getArea() const {
return w * h;
}
bool intersect(const Rectangle& other) const {
if (getLeft() >= other.getRight() || getRight() <= other.getLeft() ||
getTop() <= other.getBottom() || getBottom() >= other.getTop())
return false;
else
return true;
}
Rectangle getIntersection(const Rectangle& other) const {
double left = max(getLeft(), other.getLeft());
double right = min(getRight(), other.getRight());
double top = min(getTop(), other.getTop());
double bottom = max(getBottom(), other.getBottom());
return Rectangle(left, bottom, right - left, top - bottom);
}
friend bool operator<(const Rectangle& r1, const Rectangle& r2) {
return r1.getArea() < r2.getArea();
}
friend ostream& operator<<(ostream& out, const Rectangle& r) {
out << "(" << r.getLeft() << "," << r.getBottom() << "," << r.getWidth() << "," << r.getHeight() << "," << r.getArea() << ")";
return out;
}
private:
double x, y, w, h;
};
```
接下来,我来解答您的问题:
(1) 从键盘输入两个矩形参数(注意输入提示信息),创建两个矩形对象,判断两个矩形是否相交?求出相交部分的面积。
```c++
int main() {
double x1, y1, w1, h1;
double x2, y2, w2, h2;
cout << "请输入第一个矩形的左下角坐标、长度和宽度(用空格隔开):" << endl;
cin >> x1 >> y1 >> w1 >> h1;
cout << "请输入第二个矩形的左下角坐标、长度和宽度(用空格隔开):" << endl;
cin >> x2 >> y2 >> w2 >> h2;
Rectangle r1(x1, y1, w1, h1);
Rectangle r2(x2, y2, w2, h2);
if (r1.intersect(r2)) {
Rectangle r3 = r1.getIntersection(r2);
cout << "两个矩形相交,相交部分的面积为:" << r3.getArea() << endl;
}
else {
cout << "两个矩形不相交" << endl;
}
return 0;
}
```
(2) 从键盘再输入或随机产生3个矩形对象,对输入的所有5个矩形按面积由小到大排序,然后依次显示各个矩形的左下角坐标、长、宽、面积。
```c++
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
int main() {
srand(time(0)); // 随机数种子
vector<Rectangle> rects;
for (int i = 0; i < 3; i++) {
double x, y, w, h;
cout << "请输入第" << i + 1 << "个矩形的左下角坐标、长度和宽度(用空格隔开):" << endl;
cin >> x >> y >> w >> h;
rects.push_back(Rectangle(x, y, w, h));
}
for (int i = 0; i < 2; i++) {
double x = rand() % 10;
double y = rand() % 10;
double w = rand() % 10 + 1;
double h = rand() % 10 + 1;
rects.push_back(Rectangle(x, y, w, h));
}
sort(rects.begin(), rects.end());
for (int i = 0; i < rects.size(); i++) {
cout << "左下角坐标:" << rects[i].getLeft() << "," << rects[i].getBottom() << ",长度:" << rects[i].getWidth() << ",宽度:" << rects[i].getHeight() << ",面积:" << rects[i].getArea() << endl;
}
return 0;
}
```
希望能够帮到您!
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)