#include<iostream>
#include<cmath>
using namespace std;
class Point {
float x;
float y;
public:
Point() //显示定义一个默认构造函数
{
x = 0;
y = 0;
}
Point(float x, float y) {
this->x = x;
this->y = y;
}
float getx() {
return x;
}
float gety() {
return y;
}
};
class Rectangle {
Point TopLeft;
Point RightBottom;
public:
Rectangle() {
TopLeft = Point();
RightBottom = Point();
}
Rectangle(float tlx, float tly, float rbx, float rby) {
TopLeft = Point(tlx, tly);
RightBottom = Point(rbx, rby);
}
double Area() {
return abs((TopLeft.getx() - RightBottom.getx()) * (TopLeft.gety() - RightBottom.gety()));
}
double Perimeter() {
return (abs(TopLeft.getx() - RightBottom.getx()) * 2 + abs((TopLeft.gety() - RightBottom.gety())* 2));
}
};
int main() {
float tlx, tly, rbx, rby;
Rectangle rectangle;
cout << "请输入左上点x坐标:";
cin >> tlx;
cout << "请输入左上点y坐标:";
cin >> tly;
cout << "请输入右上点x坐标:";
cin >> rbx;
cout << "请输入右上点y坐标:";
cin >> rby;
rectangle = Rectangle(tlx, tly, rbx, rby);
cout << "周长:" << rectangle.Perimeter() << "面积:" << rectangle.Area() << endl;
}
C++定义点和矩形求矩形面积周长
最新推荐文章于 2024-04-24 13:38:14 发布