-->
当前位置:首页 > Eng > 正文内容

函数题:平面矩形 - C/C++ 类与抽象

Luz3年前 (2022-12-04)Eng785
在一个平面内,由左上角(top left)顶点坐标结合右下角(bottom right)顶点坐标即可确定一个平面矩形。请设计Rect和Point类,使其可以被下述代码所利用,并产生期望的输出。


### 裁判测试程序样例:
c++
//Project - Rect
#include <iostream>
#include <cmath>
using namespace std;

//定义Point类
//定义Rect类

int main() {
auto rt = Rect(Point(1,6),Point(7,8));
printf("Vertices of rectangle rt:\n");
printf("(%d,%d)-----------------------(%d,%d)\n",
rt.tl.x,rt.tl.y,rt.topRight().x,rt.topRight().y);

printf("(%d,%d)-----------------------(%d,%d)\n",
rt.bottomLeft().x,rt.bottomLeft().y,rt.br.x,rt.br.y);

printf("Size information of rectangle rt:\n");
printf("width - %d height - %d\n",rt.width(),rt.height());
printf("area - %d diagonal legnth - %.2f",rt.area(),rt.diagonalLength());

return 0;
}


### 输入样例:
in


### 输出样例:
out
Vertices of rectangle rt:
(1,6)-----------------------(7,6)
(1,8)-----------------------(7,8)
Size information of rectangle rt:
width - 6 height - 2
area - 12 diagonal legnth - 6.32


请注意:函数题只需要提交相关代码片段,不要提交完整程序。

* Point topRight()生成并返回矩形右上角顶点坐标;
* Point bottomLeft()生成并返回矩形左下角顶点的坐标;
* 属性Point tl表示左上角顶点坐标, br表示右下角顶点坐标;
* int width(), int height()分别计算并返回矩形的宽,高;
* double diagonalLength()计算并返回矩形的对角线长度,使用勾股定理进行计算。


### 感觉不会?  那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)











answer:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。