【东华大学oj】18 长方形?(面向对象)

18 长方形?

作者: Turbo时间限制: 1S章节: 类与对象

问题描述 :

实验目的:学习友元的使用。

实验内容:

定义一个Point类,包括两个私有成员:int x, int y,它们分别表示一个点的x和y座标。

再定义构造函数:

Point(int x, int y),即传两个参数,构造一个点对象。

定义一个Rectangle类,包括4个私有成员:Point topLeft, topRight,bottomLeft, bottomRight,它们分别表示长方形4个顶点的座标。

注意:在计算机系统里,座标系如下定义:屏幕的左上角的座标是(0,0),x轴是横轴,屏幕的最右端x值最大,y轴是纵轴,屏幕的最下方y值最大。图如下:

1461165900902074774.jpg

再定义构造函数:

Rectangle(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight)

以及实例方法:

bool isRectangle()  //判断4个顶点构成的图形是不是长方形,是则返回true,否则返回false。这个方法只在本类中调用,所以可声明为private的。

int getArea()    //如果是长方形则返回长方形的面积,否则返回0

bool isIn(Point p)   //如果是长方形则判断传入的点是否在该图形之内(不包括边界),如果在内部返回true,不在内部则返回false。如果不是长方形,则一律返回false。

说明:由于在Rectangle类中有大量的语句需要使用到Point类的私有成员x和y,因此,使用友元可直接访问x和y,从而可减少编程中的麻烦。

使用main函数测试以上getArea方法和isIn方法。main函数可参考如下代码:

int main()
{
   int topLeftX, topLeftY, topRightX, topRightY, bottomLeftX, bottomLeftY, bottomRightX, bottomRightY;
   int px, py;

   cin >> topLeftX >> topLeftY >> topRightX >> topRightY >> bottomLeftX >> bottomLeftY >> bottomRightX >> bottomRightY;
   cin >> px >> py;

   Point p(px, py);
   Rectangle r(Point(topLeftX, topLeftY), Point(topRightX, topRightY), Point(bottomLeftX, bottomLeftY), Point(bottomRightX, bottomRightY));
   cout << r.getArea() << endl;
   if (r.isIn(p))
        cout << "In" << endl;
   else
        cout << "Not in" << endl;

   return 0;
}

输入说明 :

第一行输入长方形r的信息,包括四个顶点x座标及y座标,顶点的输入顺序为左上、右上、左下、右下。

第二行输入一个点p的信息,包括其x座标和y座标。

所有输入都为非负整数,之间以一个空格分隔。无多余空格或空行。

输出说明 :

输出两行,第一行输出长方形面积,第二行输出点p是否位于长方形r之内,如果在内部,则输出“In”,否则输出“Not in”。

#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

class Point
{
private :
    int x,y;
public :
    Point(int x,int y):x(x),y(y) {}
    int getx()
    {
        return x;
    }
    int gety()
    {
        return y;
    }
    friend class Rectangle;
};

class Rectangle
{
private:
    Point topLeft,topRight,bottomLeft, bottomRight;
    bool isRectangle()
    {
        if(topLeft.getx()==bottomLeft.getx()&&
           topRight.getx()==bottomRight.getx()&&
                topLeft.gety()==topRight.gety()&&
           bottomLeft.gety()==bottomRight.gety()&&
                topLeft.getx()<topRight.getx()&&
                topLeft.gety()<bottomLeft.gety())
            return 1;
        else return 0;
    }
public:
    Rectangle(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight) :
        topLeft(topLeft), topRight(topRight),bottomLeft(bottomLeft), bottomRight(bottomRight) {}

    int getArea( )
    {
        int area=(topRight.getx()-topLeft.getx())*(bottomLeft.gety()-topLeft.gety());
        if(isRectangle()) return area;
        else return 0;
    }

    bool isIn(Point p)
    {
        if(isRectangle())
        {
            if(p.getx()<topRight.getx()&&p.getx()>topLeft.getx()&&p.gety()>topLeft.gety()&&p.gety()<bottomLeft.gety()) return true;
            else return false;

        }
        else return false;
    }


};

int main()
{
   int topLeftX, topLeftY, topRightX, topRightY, bottomLeftX, bottomLeftY, bottomRightX, bottomRightY;
   int px, py;

   cin >> topLeftX >> topLeftY >> topRightX >> topRightY >> bottomLeftX >> bottomLeftY >> bottomRightX >> bottomRightY;
   cin >> px >> py;

   Point p(px, py);
   Rectangle r(Point(topLeftX, topLeftY), Point(topRightX, topRightY), Point(bottomLeftX, bottomLeftY), Point(bottomRightX, bottomRightY));
   cout << r.getArea() << endl;
   if (r.isIn(p))
        cout << "In" << endl;
   else
        cout << "Not in" << endl;

   return 0;
}

  • 18
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值