2020-11-23

设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。

#include <iostream>
using namespace std;
class Rectangle
{
public:
	Rectangle(int top,int left,int bottom,int right);
	~Rectangle(){}
	int GetTop()const{return itsTop;}
	int GetLeft()const{return itsLeft;}//常量函数
	int GetBottom()const{return itsBottom;}
	int GetRight()const {return itsRight;}
	void SetTop(int top){itsTop=top;}
	void SetLeft(int left){itsLeft=left;}
	void SetRight(int right){itsRight=right;}
	int GetArea()const;
private:
	int itsTop;
	int itsLeft;
	int itsBottom;
	int itsRight;
};
Rectangle::Rectangle(int top,int left,int bottom,int right)
{
	itsTop=top;
	itsLeft=left;
	itsBottom=bottom;
	itsRight=right;
}
int Rectangle::GetArea()const
{
	int Width=itsRight-itsLeft;
	int Height=itsTop-itsBottom;
	return (Width*Height);
}
int main()
{
	Rectangle MyRectangle(100,20,50,80);
	int Area=MyRectangle.GetArea();
	cout<<"Area:"<< Area<<"\n";
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Java中的SimpleDateFormat类将String类型的时间转换为Date对象,然后再对Date对象进行处理。 首先,我们先将时间段转换为【2020-11-20 00:00:00,2021-10-09 23:59:59】,即将结束时间改为当天的最后一秒: ```java String startTime = "2020-11-20 09:09:09"; String endTime = "2021-10-09 10:10:10"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = sdf.parse(startTime); Date endDate = sdf.parse(endTime); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); endCalendar.set(Calendar.HOUR_OF_DAY, 23); endCalendar.set(Calendar.MINUTE, 59); endCalendar.set(Calendar.SECOND, 59); endDate = endCalendar.getTime(); String newEndTime = sdf.format(endDate); System.out.println(newEndTime); ``` 输出结果为:2021-10-09 23:59:59 接下来,我们可以使用一个循环,每次增加一天,输出该天的开始时间和结束时间: ```java Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); while (calendar.getTime().before(endDate)) { Date startOfDay = calendar.getTime(); calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date endOfDay = calendar.getTime(); System.out.println(sdf.format(startOfDay) + " - " + sdf.format(endOfDay)); } // 输出最后一天的开始时间和结束时间 Date startOfDay = calendar.getTime(); System.out.println(sdf.format(startOfDay) + " - " + newEndTime); ``` 输出结果为: ``` 2020-11-20 09:09:09 - 2020-11-21 00:00:00 2020-11-21 00:00:00 - 2020-11-22 00:00:00 2020-11-22 00:00:00 - 2020-11-23 00:00:00 ... 2021-10-07 00:00:00 - 2021-10-08 00:00:00 2021-10-08 00:00:00 - 2021-10-09 00:00:00 2021-10-09 00:00:00 - 2021-10-09 23:59:59 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

STRUGGLE_xlf

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

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

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

打赏作者

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

抵扣说明:

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

余额充值