2021-05-15

002.Rectangle类的设计及使用

题目描述
​设计一个矩形类Rectangle。其属性为矩形的左下角与右上角两个点的坐标。

main函数已经完成,请根据main函数完成该类的设计:

int main(){
int top,bottom,left,right;
cin>>top>>right>>bottom>>left;
Rectangle r(top,right,bottom,left);
r.showLeftTop(); //输出左上顶点的坐标
r.showRightTop(); //输出右上顶点的坐标
r.showLeftBottom(); //输出左下顶点的坐标
r.showRightBottom(); //输出右下顶点的坐标
cout<<r.getHeight()<<" “<<r.getWidth()<<endl; //输出高和宽
cout<<r.getArea()<<” "<<r.getPerimeter()<<endl; //输出面积和周长
return 0;
}

注意:输入应该保证右上顶点的横坐标大于左下顶点的横坐标且右上顶点的纵坐标大于左下顶点的纵坐标,如果不满足,则将所有坐标设置为0
输入描述
依次输入右上顶点的纵坐标、横坐标和左下顶点的纵坐标、横坐标
输出描述
输出左上顶点的坐标
输出右上顶点的坐标
输出左下顶点的坐标
输出右下顶点的坐标
输出高和宽
输出面积和周长
提示
你需要提交main函数之外的代码部分
样例输入
5 5 3 3
样例输出
3,5
5,5
3,3
5,3
2 2
4 8
允许最长运行时间
1000ms
允许使用最大内存
128KB

#include<iostream>
using namespace std;
class Rectangle {
private:
	int x1, y1, x2, y2;
public:
	Rectangle(int x1=0,int y1=0,int x2=0,int y2=0) {
		this->x1 = x1;
		this->y1 = y1;
		this->x2 = x2;
		this->y2 = y2;
	}
	void showLeftTop() {
		int x3 = x2, y3 = y1;
		if (x1 <= x2 || y1 <= y2)
			cout << "0,0" << endl;
		else
		cout << x3 << "," << y3 << endl;
	}
	void showRightTop() {
		if (x1 <= x2 || y1 <= y2)
			cout << "0,0" << endl;
		else
		cout << x1 << "," << y1 << endl;
	}
	void showLeftBottom() {
		if (x1 <= x2 || y1 <= y2)
			cout << "0,0" << endl;
		else
		cout << x2<< "," << y2<<endl;
	}
	void showRightBottom() {
		int x4 = x1, y4 = y2;
		if (x1 <= x2 || y1 <= y2)
			cout << "0,0" << endl;
		else
		
		cout << x4 << "," << y4 << endl;
	}
	int getHeight() {
		int m = x1 - x2;
		if (x1 <= x2 || y1 <= y2)
			return 0;
		else
		return m;
	}
	int getWidth() {
		int n = y1 - y2;
		if (x1 <= x2 || y1 <= y2)
			return 0;
		else
		return n;
	}
	int getArea() {
		int a = (x1 - x2) * (y1 - y2);
		if (x1 <= x2 || y1 <= y2)
			return 0;
		else
		return a ;
	}
	int getPerimeter() {
		int b = 2 * (x1 - x2) + 2 * (y1 - y2);
		if (x1 <= x2 || y1 <= y2)
			return 0;
		else
		return b;
	}
};

int main(){
	int top,bottom,left,right;
	cin>>top>>right>>bottom>>left;
	Rectangle r(top,right,bottom,left);
	r.showLeftTop();   //输出左上顶点的坐标 
	r.showRightTop();  //输出右上顶点的坐标 
	r.showLeftBottom();  //输出左下顶点的坐标 
	r.showRightBottom();  //输出右下顶点的坐标 
	cout<<r.getHeight()<<" "<<r.getWidth()<<endl;  //输出高和宽 
	cout<<r.getArea()<<" "<<r.getPerimeter()<<endl;  //输出面积和周长 
	return 0;
}

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值