码迷,mamicode.com
首页 > 其他好文 > 详细

日期计算器

时间:2016-08-14 10:23:19      阅读:1470      评论:0      收藏:0      [点我收藏+]

标签:

实现日期加减天数,得到一个日期;日期减日期,得到一个天数。

<span style="font-size:18px;">#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;

//添加打印指定月的信息,一个月的每一天和对应


enum
{
	zero,
	one,
	tow,
	three
};
class Date
{
public:
	Date(int year = 2016, int month = 7, int day = 20, int week = 3) :_year(year)
		, _month(month)
		, _day(day)
		, _week(week)
	{
	}
	Date operator+ (int day);
	Date operator- (int day);
	int operator- (const Date& d);
	void display()const;
	friend int bissextile(const Date&, const Date&);
	friend istream& operator>>(istream& is, Date& d);
	int count() const;
//private:
	int _year;
	int _month;
	int _day;
	int _week;
	static int month[12];
};

int Date::month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int Date::count() const          //计算从当前年份开始到当前日期已经过了多少天
{
	int i = 0;
	int sum = 0;
	for (i = 0; i <_month - 1; i++)
	{
		sum += month[i];
	}
	return sum + _day;
}


int bissextile(const Date& d1, const Date& d2)       //判断两个日期之间有多少个闰年
{
	int i = 0;
	int j = 0;
	if (d1._month>2)
	{
		i = d1._year;
	}
	else
	{
		i = d1._year - 1;
	}
	if (d2._month <= 2)
	{
		j = d2._year;
	}
	else
	{
		j = d2._year + 1;
	}
	int sum = 0;
	for (; i >= j; i--)
	{
		if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))
		{
			sum++;
		}
	}
	return sum;        
}


Date Date::operator+ (int day)          //日期加天数
{
	Date tmp = *this;
	_week = (_week + day - 1) % 7 + 1;        //求出这一天对应的星期

	_year += (day / 365);
	day = day % 365;
	if (day - (month[_month - 1] - _day)>0)
	{
		day -= (month[_month - 1] - _day);
		_day = 0;                       
	}
	else
	{
		_day += day;
		return *this;
	}
	for (int i = _month; day >month[i % 12]; i++)
	{
		day -= month[i % 12];
		_month++;
	}

	_year += (_month / 12);
	_month = _month % 12 + 1;
	_day = day;
	return *this;
}

Date Date::operator- (int day)        //日期减天数
{
	int  d = day % 7;
	if (_week >d)
	{
		_week = _week - d;
	}
	else
	{
		_week = 7 - (d - _week);
	}

	Date tmp = *this;
	_year -= (day / 365);
	day = day % 365;
	if (day - _day >= 0)
	{
		day -= _day;
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day = 0;
	}
	else
	{
		_day -= day;
		return *this;
	}
	for (int i = _month - 1; day - month[i] >= 0; i--)
	{
		day -= month[i];
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		if (i == 0)
		{
			i = 12;
		}
	}
	_day = month[_month - 1] - day;
	return *this;
}


int Date::operator- (const Date& d)                       //日期-日期
{
	int sum = 0;
	sum = bissextile(*this, d);                            //判断两个日期之间有多少个闰年
	return (_year - d._year) * 365 + (*this).count() - d.count() + sum;
}


void Date::display()const
{
	int i = 0;
	int j = 0;
	int d = _day - 1;
	int week = _week;
	d = d % 7;
	if (week >d)
	{
		week = week - d;
	}
	else
	{
		week = 7 - (d - week);
	}

	cout << "  " << _year << "年" << _month << "月" << _day << "日" << "星期" << _week << endl;
	cout << "  日 " << " 一 " << " 二 " << " 三 " << " 四 " << " 五 " << " 六 " << endl;
	for (i = 0; i < (week % 7); i++)
	{
		cout << setw(4) << " ";
	}
	j = month[_month - 1];
	if ((((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0)) && _month == 2)
	{
		j++;
	}
	for (i = 0; i <j; i++)
	{
		week++;
		cout << setw(4) << setiosflags(ios::right) << (i + 1);
		if (week % 7 == 0)
		{
			cout << endl;
		}
	}
	cout << endl;
}

istream& operator>>(istream& is, Date& d)
{
	is >> d._year >> d._month >> d._day >> d._week;
	return is;
}

void test1()
{
	Date d1;
	Date d2;
	int day = 0;
	cout << "input>:日期" << endl;
	cin >> d1;
	cout << "input>:天数" << endl;
	cin >> day;
	d2 = d1 + day;
	int sum = bissextile(d1, d2);
	d2 = d2 - sum;
	d2.display();
}


void test2()
{
	Date d1;
	Date d2;
	Date d3;
	int day = 0;
	cout << "input>:日期" << endl;
	cin >> d1;
	cout << "input>:天数" << endl;
	cin >> day;
	d3 = d1;
	d2 = d1 - day;
	int sum = bissextile(d3, d2);
	d2 = d2+sum;
	d2.display();
}

void test3()
{
	Date d1;
	Date d2;
	cout << "input>:日期" << endl;
	cin >> d1;
	cout << "input>:日期" << endl;
	cin >> d2;
	int day=0;
	day = d1 - d2;
	cout << "相差"<<day<<"天" << endl;
}

void menu()
{
	int n;
	while (1)
	{
		cout << " 0.exit"<<" 1.日期加天数" << " 2.日期减天数" << " 3.日期减日期" << endl << endl;;
		cout <<"请选择:";
		cin >> n;
		switch (n)
		{
		case zero:
			exit(1);
			break;
		case one:
			test1();
			break;
		case tow:
			test2();
			break;
		case three:
			test3();
			break;
		default:
			cout << "输入不合法" << endl;
		}
	}
}

int main()
{
	menu();
	system("pause");
	return 0;
}</span><span style="font-size:32px;">
</span>


日期计算器

标签:

原文地址:http://blog.csdn.net/lf_2016/article/details/52203030

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!