芝士很芒 2021-12-07 00:05 采纳率: 66.7%
浏览 30
已结题

刚入门,这种难度自己写起来有点费力

img

  • 写回答

1条回答 默认 最新

  • 南城Flipped 2021-12-08 09:02
    关注
    
    #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;
    }
    

    没细测

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月16日
  • 已采纳回答 12月8日
  • 创建了问题 12月7日

悬赏问题

  • ¥15 c#转安卓 java html
  • ¥15 os.listdir文件路径找不到
  • ¥15 使用gojs3.0,如何在nodeDataArray设置好text的位置,再go.TextBlock alignment中进行相应的改变
  • ¥15 psfusion图像融合指标很低
  • ¥15 银河麒麟linux系统如何修改/etc/hosts权限为777
  • ¥50 医院HIS系统代码、逻辑学习
  • ¥30 docker离线安装mysql报错,如何解决?
  • ¥15 构建工单的总账影响在哪里查询或修改
  • ¥15 三个简单项目写完之后有重赏之后联系我
  • ¥15 python报内存不能read错误