实验9:Problem D: 从点到面

 

HomeWeb BoardProblemSetStandingStatusStatistics
 
Problem D: 从点到面

Problem D: 从点到面

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 529   Solved: 332
[ Submit][ Status][ Web Board]

Description

一个矩形可以由左上角和右下角的顶点而唯一确定。现在请定义两个类:Point和Rectangle。

其中Point类有x和y两个属性(均为int类型),表示二维空间内一个点的横纵坐标,并具有相应的构造函数、析构函数和拷贝构造函数。此外,还有getX()和getY()方法用以得到一个点的坐标值。

Rectangle类有leftTop和rightBottom两个属性(均为Point类的对象),表示一个矩形的左上角和右下角的两个点,并具有相应的构造函数、析构函数。此外,还有getLeftTop()、getRightBottom()方法用于获取相应的左上角点、右下角点,getArea()方法用以获取面积。

 

Input

输入有多行。

第一行是一个正整数M,表示后面有M个测试用例。

每个测试用例占一行,包括4个正整数,分别为左上角的横坐标、纵坐标,右下角的横坐标、纵坐标。

注意:

1.请根据输出样例判断两个类中相应方法的书写方法。

2. 假定屏幕的左下角为坐标原点。

 

Output

输出见样例。

 

Sample Input

1 10 10 20 0

Sample Output

A point (10, 10) is created! A point (20, 0) is created! A rectangle (10, 10) to (20, 0) is created! Area: 100 Left top is (10, 10) A point (20, 0) is copied! A point (20, 0) is copied! Right bottom is (20, 0) A point (20, 0) is erased! A point (20, 0) is erased! A rectangle (10, 10) to (20, 0) is erased! A point (20, 0) is erased! A point (10, 10) is erased!

HINT

 

Append Code

[ Submit][ Status][ Web Board]
#include<iostream>
using namespace std;
class Point{
    friend class Rectangle;
public:
    int x,y;
    Point(int a,int b):x(a),y(b){cout<<"A point ("<<x<<", "<<y<<") is created!"<<endl;}
    ~Point(){cout<<"A point ("<<x<<", "<<y<<") is erased!"<<endl;}
    Point(const Point &p){x=p.x;y=p.y;cout<<"A point ("<<x<<", "<<y<<") is copied!"<<endl;}
    int getX(){return x;}
    int getY(){return y;}

};
class Rectangle{
    friend class Point;
public:
    Point leftTop,rightBottom;
    Rectangle(Point p,Point q):leftTop(p),rightBottom(q){cout<<"A rectangle ("<<leftTop.x<<", "<<leftTop.y<<") to ("<<rightBottom.x<<", "<<rightBottom.y<<") is created!"<<endl;}
    Rectangle(int a,int b,int c,int d):leftTop(a,b),rightBottom(c,d){cout<<"A rectangle ("<<leftTop.x<<", "<<leftTop.y<<") to ("<<rightBottom.x<<", "<<rightBottom.y<<") is created!"<<endl;}
    ~Rectangle(){cout<<"A rectangle ("<<leftTop.x<<", "<<leftTop.y<<") to ("<<rightBottom.x<<", "<<rightBottom.y<<") is erased!"<<endl;}
    Point &getLeftTop(){return leftTop;}
    Point getRightBottome(){return rightBottom;}
    int getArea(){return (rightBottom.x-leftTop.x)*(leftTop.y-rightBottom.y);}
};
int main()
{
    int cases;
    int x1, y1, x2, y2;

    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>x1>>y1>>x2>>y2;
        Rectangle rect(x1,y1,x2,y2);
        cout<<"Area: "<<rect.getArea()<<endl;
        cout<<"Left top is ("<<rect.getLeftTop().getX()<<", "<<rect.getLeftTop().getY()<<")"<<endl;
        cout<<"Right bottom is ("<<rect.getRightBottome().getX()<<", "<<rect.getRightBottome().getY()<<")"<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/auto1945837845/p/5447296.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值