用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

根据长方形左上角和右下角的坐标的面积

2017-08-03 作者: 芙蓉妹妹举报

[c++]代码库

#include <iostream>
using namespace std;
class Point
{
public:
    int x,y;
    Point (int a,int b)
    {
        x=a;
        y=b;
        cout<<"A point ("<<x<<", "<<y<<") is created!"<<endl;
    }
    Point (const Point &p)
    {
        x=p.x;
        y=p.y;
        cout<<"A point ("<<x<<", "<<y<<") is copied!"<<endl;
    }
    ~Point ()
    {
        cout<<"A point ("<<x<<", "<<y<<") is erased!"<<endl;
    }
    int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }
};
class Rectangle
{
private:
    Point leftTop,rightBottom;
public:
    Rectangle(int a,int b,int c,int d):leftTop(a,b),rightBottom(c,d)
    {
        cout<<"A rectangle ("<<a<<", "<<b<<") to ("<<c<<", "<<d<<") is created!"<<endl;
    }
    ~Rectangle()
    {
        cout<<"A rectangle ("<<leftTop.getX()<<", "<<leftTop.getY()<<") to ("<<rightBottom.getX()<<", "<<rightBottom.getY()<<") is erased!"<<endl;
    }
    Point &getLeftTop()
    {
        return leftTop;
    }
    Point getRightBottome()
    {
        return rightBottom;
    }
    int getArea()
    {
        int a,b,c,d;
        a=leftTop.x;
        b=leftTop.y;
        c=rightBottom.x;
        d=rightBottom.y;
        return (c-a)*(b-d);
    }
};
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;
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...