C++日期类

#include<iostream>
#include<stdlib.h>
using namespace std;

class Date
{
public:


	bool IsLeapYear(int year)
	{
		return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
	}

	int GetDayInMonth(int year, int month)
	{
		int months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (IsLeapYear(year))
		{
			months[2] = 29;
		}
		return months[month];
	}

	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 (this != &d){
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

	Date operator+(int days)
	{
		_day += days;

		if (_day > GetDayInMonth(_year, _month))
		{
			_month += 1;
			if (_month > 12)
			{
				_year += 1;
				_month = 1;
			}
			_day = 1;
		}
		return *this;
	}

	Date operator-(int days)
	{
		if (days < 0)//天数若为负数变为加
		{
			return *this + (0 - days);
		}
		Date temp(*this);
		temp._day -= days;
		while (temp._day <= 0)
		{
			temp._month--;
			if (temp._month <= 0)
			{
				temp._year--;
				temp._month = 12;
			}
			temp._day += temp.GetDayInMonth(temp._year,temp._month);
		}
		return temp;
	}

	int operator-(const Date& d)
	{
		Date maxdate(*this);
		Date mindate(d);
		if (maxdate < mindate)
		{
			maxdate = mindate;
			mindate = *this;
		}
		int count = 0;
		while (mindate < maxdate)
		{
			mindate = mindate + 1;
			++count;
		}
		return count;
			
	}

	Date& operator++()
	{
		_day += 1;
		if (_day > GetDayInMonth(_year, _month))
		{
			_month += 1;
			if (_month > 12)
			{
				_year += 1;
				_month = 1;
			}
			_day = 1;
		}
		return *this;
	}

	Date operator++(int)
	{
		Date temp = *this;
		_day += 1;
		return temp;
	}

	Date& operator--()
	{
		_day -= 1;
		if (_day == 0)
		{
			_month -= 1;
			if (_month == 0)
			{
				_year -= 1;
				_month = 12;
				_day = GetDayInMonth(_year - 1, 12);
			}
			else{
				_day = GetDayInMonth(_year, _month);
			}
		}
		return *this;
	}

	Date operator--(int)
	{
		Date temp = *this;
		_day -= 1;
		return temp;
	}

	bool operator>(const Date& d)const
	{
		while (_year > d._year || (_year == d._year&&_month > d._month) ||
			(_year == d._year && _month == d._month && _day > d._day))
		{
			return true;
		}
		return false;

	}

	bool operator>=(const Date& d)const
	{
		while (_year > d._year || (_year == d._year&&_month > d._month) ||
			(_year == d._year && _month == d._month && _day > d._day) || 
			(_year == d._year && _month == d._month && _day == d._day))
		{
			return true;
		}
		return false;
	}

	bool operator<(const Date& d)const
	{
		while (_year <d._year || (_year == d._year&&_month < d._month) ||
			(_year == d._year && _month == d._month && _day < d._day))
		{
			return true;
		}
		return false;
	}
	bool operator<=(const Date& d)const
	{
		while (_year <d._year || (_year == d._year&&_month < d._month) ||
			(_year == d._year && _month == d._month && _day < d._day) ||
			(_year == d._year && _month == d._month && _day < d._day))
		{
			return true;
		}
		return false;
	}
	bool operator==(const Date& d)const
	{
		if (_year == d._year && _month == d._month && _day == d._day)
			return true;
		return false;

	}
	bool operator!=(const Date& d)const
	{
		if (_year != d._year || _month != d._month || _day != d._day)
			return true;
		return false;
	}

	friend ostream& operator<<(ostream& _cout, const Date& d);

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}

void FunTest()
{
	Date d1(1900, 2, 28);
	cout << ++d1 << endl;
	cout << --d1 << endl;
	cout << d1 << endl;

	cout << d1-- << endl;
	cout << d1++ << endl;
	cout << d1 << endl;

	Date d2(2017, 5, 1);
	cout << d2 + 100 << endl;
	Date d3(1997, 11, 1);
	cout << d3 - 100 << endl;

	cout << boolalpha << (Date(2016, 10, 1) < d1) << endl;
	cout << d1 - d2 << endl;
}


int main()
{
	FunTest();
	system("pause\n");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,由于没有对象的概念,因此无法像面向对象语言一样使用内嵌子对象。不过,我们可以通过结构体来模拟对象的概念,从而实现类似内嵌子对象的效果。 以日期类为例,我们可以定义一个结构体来表示日期,然后在日期类中嵌入这个结构体作为其成员变量,从而实现日期类的功能。 以下是一个简单的 C 日期类的实现: ``` typedef struct { int year; int month; int day; } Date; typedef struct { Date date; int (*getYear)(const Date *); int (*getMonth)(const Date *); int (*getDay)(const Date *); } DateClass; int getYear(const Date *date) { return date->year; } int getMonth(const Date *date) { return date->month; } int getDay(const Date *date) { return date->day; } DateClass Date_new(int year, int month, int day) { DateClass dateClass = { { year, month, day }, getYear, getMonth, getDay }; return dateClass; } ``` 在上面的代码中,我们首先定义了一个日期结构体 `Date`,表示一个具体的日期。然后,我们定义了一个日期类 `DateClass`,其中包含一个 `Date` 类型的成员变量 `date`,以及三个函数指针,用于获取日期的年、月、日信息。 接着,我们实现了三个函数 `getYear`、`getMonth`、`getDay`,分别用于获取日期的年、月、日信息。最后,我们定义了一个 `Date_new` 函数,用于创建一个新的日期对象,并返回一个 `DateClass` 类型的结构体,该结构体包含了日期对象的所有信息和操作。 使用这个日期类时,我们可以先通过 `Date_new` 函数创建一个日期对象,然后使用该对象的方法获取日期的信息,如下所示: ``` DateClass date = Date_new(2021, 10, 1); printf("Year: %d\n", date.getYear(&date.date)); printf("Month: %d\n", date.getMonth(&date.date)); printf("Day: %d\n", date.getDay(&date.date)); ``` 输出结果为: ``` Year: 2021 Month: 10 Day: 1 ``` 可以看到,我们通过 `Date_new` 函数创建了一个日期对象 `date`,然后使用该对象的方法获取了日期的年、月、日信息,并将其输出到控制台上。这就是一个简单的 C 日期类的实现,其中使用了结构体来模拟对象,并实现了类似内嵌子对象的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值