
已知Point类和Rectangle类的声明与定义如下,请完成main函数定义:
第一行输入2个doube型值,用空格分隔,分别表示矩形左上角的x与y坐标值;
第二行输入2个double型值,用空格分隔,分别表示矩形右下角的x与y坐标值;
利用2个点制造一个矩形,输出矩形的长、宽和面积,输出格式见输出样例。
裁判测试程序样例:
#include
using namespace std;
class Point{
public:
Point(double xx=0, double yy=0);
void Set(double xx, double yy);
double GetX() { return x; }
double GetY() { return y; }
void Move(double xx,double yy);
void Output();
private:
double x;
double y;
};
class Rectangle{
public:
Rectangle(Point &p1, Point &p2);
void Set(Point &p1, Point &p2);
double GetLength();
double GetWidth();
double GetArea();
private:
Point ltp; //矩形左上角坐标
Point rbp; //矩形右下角点坐标,x与y都大于左上角ltp的x和y
};
/* 请在这里填写main函数定义 */
Point::Point(double xx, double yy)
{
Set(xx,yy);
}
void Point::Set(double xx, double yy)
{
x = xx;
y = yy;
}
void Point::Move(double xx,double yy)
{
x = x + xx;
y = y + yy;
}
void Point::Output()
{
cout << x << "," << y << endl;
}
Rectangle::Rectangle(Point &p1, Point &p2):ltp(p1),rbp(p2)
{
}
void Rectangle::Set(Point &p1, Point &p2)
{
ltp.Set(p1.GetX(), p1.GetY());
rbp.Set(p2.GetX(), p2.GetY());
}
double Rectangle::GetLength()
{
return rbp.GetX() - ltp.GetX();
}
double Rectangle::GetWidth()
{
return rbp.GetY() - ltp.GetY();
}
double Rectangle::GetArea()
{
return GetLength() * GetWidth();
}
输入样例:
第一行输入左上角坐标的x与y值,用空格分隔
第二行输入右下角坐标的x与y值,用空格分隔
1 2
3 4
输出样例:
length= 2
width= 2
area= 4
c++解析:
int main()
{
int a, b, c, d;
cin >> a >> b;
cin >> c >> d;
cout << "length= " << abs(c - a) << endl;
cout << "width= " << abs(d - b) << endl;
cout << "area= " << abs((c - a) * (d - b)) << endl;
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)