完善Date类

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	
		: _year(year)
			, _month(month)
			, _day(day)
		{}
	
	
	Date(const Date& d)

		: _year(d._year)
		, _month(d._month)
		, _day(d._day)
	{}
	Date& operator=(const Date& d)
	{
		if (&d != this)//不可以给自己赋值
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	bool LeapYear(int year)
	{//闰年 被400整除 或 被4不被100
		return ( year % 400 == 0 )||(year % 4 == 0 && year % 100);
	}
	int GetMonthDay(int year, int month)
	{//判断这个月有多少天  2月是28天(闰月29); 4.6.9.11月30天; 1.3.5.7.8.10.12是31天。
		if (month == 2 && LeapYear(year))
			return 29;
		else if (month == 2)
			return 28;
		else{
			if ((month< 8 && month % 2) || (month>8 && month % 2 == 0))
				return 31;
			return 30;
		}
			
	}
	Date operator+(int days)
	{//进月   加上days天后是这个月的第几天?
		_day += days;  
		while (_day > GetMonthDay(_year,_month))//如果大于这个月的总天数
		{
			_day -= GetMonthDay(_year, _month);//减去上个月的天数
			_month += 1;//进月
			if (_month > 12)//进年
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}
	Date operator-(int days)
	{
		_day -= days;
		while (_day < 1)
		{
			--_month;//一定在该月之前
			if (_month < 1)//退年
				_year--;
			_month = 12 ;//假设变化天数days最多为28,即月份和年份起伏最大为1,
		}
		_day += GetMonthDay(_year, _month);
		return *this;
	}
	int operator-(const Date& d);//几天前
	{
		if (*this == d)
			return 0;
		Date max = *this;
		Date min = d;
		if (max < d){
			max = d;
			min = *this;		
		}
		int count = 0;
		while (min < max)
		{
			//min++;
			max--;
			count++;
		}
		return 0;		
	}

	Date& operator++()//前置
	{
		operator+(1);
		return *this;
	}		
	Date operator++(int)//后置
	{//0
		Date tmp = *this;
		operator +(1);
		return tmp;
	}

	Date operator--(int)
	{
		Date tmp = *this;
		operator -(1);
		return tmp;
	}
	bool operator>(const Date& d)const
	{
		if (_year > d._year)
			return true;
		if (_year < d._year)
			return false;
		//year=
		if (_month > d._month)
			return true;
		if (_month < d._month)
			return false;
		if (_day_ > d._day)
			return true;
		else
			return false;
	}

	bool operator>=(const Date& d)const
	{
		if (*this > d || *this == d)
			return ture;
	}

	bool operator<(const Date& d)const
	{
		if (*this >= d)
			return false;
		return true;
	}
	bool operator<=(const Date& d)const
	{
		if (*this > d)
			return false;
		return true;
	}
	bool operator==(const Date& d)const
	{
		return   !(*this > d && *this < d);
	}

	bool operator!=(const Date& d)const
	{
		return  !(*this == d);
	}
private:
	int _year;
	int _month;
	int _day;
};

1.编写MyDate.java 2.该有如下构造方法 2.1 无参数构造方法public MyDate(),以当前的系统时间构造MyDate对象 2.2 public MyDate(int year, int month, int day), 以指定的年月日构造MyDate对象 3.该有如下属性 3.1 private int year ;//年 3.2 private int month; //月 3.3 private int day; //日 4.该有如下方法 4.1 public String after(int day); //返回当前对象代表的日期之后day天的日期,比如当前对象是2008-8-8,调用after(5)以后,应该返回2008-8-13,格式可自定义 4.2 public String before(int day); //返回当前对象代表的日期之前day天的日期,比如当前对象是2008-8-8,调用before(5)以后,应该返回2008-8-3,格式可自定义 4.3 public void setYear(int year); //设置年为指定值year 4.4 public void setMonth(int month); //设置月为指定值month 4.5 public void setDay(int day); //设置日为指定值day 4.6 public int getYear(); //返回当前对象的年 4.7 public int getMonth(); //返回当前对象的月 4.8 public int getDay(); //返回当前对象的日 4.9 public void set (int year, int month, int day); //设置年、月、日为指定的值year、month、day 4.10 public String toString();//以字符串形式返回当前对象的年月日,例如2008年08月08日,格式可自定义 4.11 public boolean equals(MyDate mydate);//当前对象与另一个对象比较,如果两个对象的年月日均相等,则返回true,反之返回false 5.编写TestMyDate.java,在main方法中对MyDate的各个方法进行测试 6.按照编程规范为MyDate.java编写规范的代码 7.按照java doc API的要求,对代码编写规范的注释,然后利用javadoc.exe命令生成MyDate.java的API doc 8.撰写上机报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值