码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 日期类

时间:2015-11-17 19:25:19      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:c++;日期类;运算符重载

#pragma once //头文件

class Date{

private:
	int _year;
	int _month;
	int _day;

public:
	Date(int year=1990,int month=1,int day=1);
	Date(const Date& tmp);
    ~Date();
	int operator-(const Date& tmp);//日期相减
	Date operator+(int day);//日期与天数相加
	Date& operator++();//日期与天数相加
	Date& operator+=(int day);//日期与天数相加

	Date operator-(int day);//日期与天数相减
	bool operator>(const Date& tmp);//日期比较
	bool operator==(const Date& tmp);//日期是否相等


	void PrintDate();//打印日期
	bool IsleapYear(const Date& tmp);//是否闰年
	bool IsOverDay(const Date& tmp);//是否超过1个月的天数
	bool IsOverMonth(const Date& tmp);//是否超过1年的月数
};

#include<iostream>//函数文件
#include "date.h"

using namespace std;

Date::Date(int year,int month,int day)
{
    _year=year;
	_month=month;
	_day=day;
	if(year<1990||month<1||IsOverMonth(*this)||day<1||IsOverDay(*this))
	{	
		cout<<"无效的日期"<<endl;
		exit(-1);	
	}
}

Date::Date(const Date& tmp)
{
	_year=tmp._year;
	_month=tmp._month;
	_day=tmp._day;
}

Date::~Date()
{

}
int Date::operator-(const Date& tmp)//基本实现日期相减 (复杂逻辑)
{
	Date dateTmp;
	int count=0 ;
	int tmpCount=0;
	if(tmp._year-_year==0)
	{
		dateTmp._month=(_month<tmp._month)? _month:tmp._month;
		dateTmp._day=0;
		int maxDay=_month>tmp._month?_day:tmp._day;
		while(dateTmp._month!=(_month>tmp._month? _month:tmp._month)||
			dateTmp._day!=maxDay)
		{
			
			if(dateTmp._month!=(_month>tmp._month? _month:tmp._month))
			{
				if(dateTmp._month==1||dateTmp._month==3||dateTmp._month==5
					||dateTmp._month==7
				||dateTmp._month==8||dateTmp._month==10||dateTmp._month==12)
				{	
					count+=31;
					++dateTmp._month;
				}
				else if(dateTmp._month==2)
				{
					if(IsleapYear(tmp))
					{
						count+=29;
						++dateTmp._month;
					}
					else
					{
						count+=28;
						++dateTmp._month;
					}
				}
				else 
				{	
					count+=30;
					++dateTmp._month;
				}
			}
			else
			{
				++dateTmp._day;
				++count ;
			}
		}
		return count-(_month>tmp._month?_day:tmp._day);
	}
	else
	{
		dateTmp._year=(_year<dateTmp._year)? _year:tmp._year;
		dateTmp._month=1;
		dateTmp._day=0;
		int maxDay=(_year>tmp._year?_day:tmp._day);
		int tmpCount=0;
		while((dateTmp._year!=(_year>tmp._year? _year:tmp._year))||
			(dateTmp._month!=(_year>tmp._year? _month:tmp._month))||
			(dateTmp._day!=maxDay))
		{
			if(dateTmp._year!=(_year>tmp._year? _year:tmp._year))
			{
				if(IsleapYear(dateTmp))
				{
					count+=366;
					++dateTmp._year;
				}
				else
				{
					count+=365;
					++dateTmp._year;
				}
			}

			else if(dateTmp._month!=(_year>tmp._year? _month:tmp._month))
			{
				if(dateTmp._month==1||dateTmp._month==3||dateTmp._month==5
					||dateTmp._month==7
					||dateTmp._month==8||dateTmp._month==10||dateTmp._month==12)
				{	
					count+=31;
					++dateTmp._month;
				}
				else if(dateTmp._month==2)
				{
					if(IsleapYear(tmp))
					{
						count+=29;
						++dateTmp._month;
					}
					else
					{
						count+=28;
						++dateTmp._month;
					}
				}
				else 
				{	
					count+=30;
					++dateTmp._month;
				}
			}
			else
			{
				++count ;
				++dateTmp._day;		
			}
		}
		dateTmp._month=1;
		dateTmp._day=0;
		maxDay=_year<tmp._year?_day:tmp._day;
		while(dateTmp._month!=(_year<tmp._year? _month:tmp._month)||
			dateTmp._day!=maxDay)
		{
			
			if(dateTmp._month!=(_year<tmp._year? _month:tmp._month))
			{
				if(dateTmp._month==1||dateTmp._month==3||dateTmp._month==5
					||dateTmp._month==7
					||dateTmp._month==8||dateTmp._month==10||dateTmp._month==12)
				{	
					tmpCount+=31;
					++dateTmp._month;
				}
				else if(dateTmp._month==2)
				{
					if(IsleapYear(dateTmp))
					{
						tmpCount+=29;
						++dateTmp._month;
					}
					else
					{
						tmpCount+=28;
						++dateTmp._month;
					}
				}
				else 
				{	
					tmpCount+=30;
					++dateTmp._month;
				}
			}
			else
			{
				++dateTmp._day;
				++tmpCount ;
			}
		}
		return count-tmpCount;
	}
}
int Date::operator-(const Date& tmp)//日期相减  (与上个函数体 重复)
{
//利用其它 类的成员函数  简单实现日期相减
	int count=0;
	if(*this>tmp)
	{
		Date dateTmp(tmp);
		while(1)
		{
			if (dateTmp == *this)
				return count;
			if ((++dateTmp) == *this)
			{
				count++;
				return count;
			}
			count++;
		}
	}
	else
	{
		Date dateTmp(*this);
		while(1)
		{
			if (dateTmp == *this)
				return count;
			if ((++dateTmp) == *this)
			{
				count++;
				return count;
			}
			count++;
		}

	}
  
}

Date Date::operator+(int day)//日期与天数相加
{
	Date tmp(*this);
	int count=0;
	while(count!=day)
	{
		++tmp._day;
		if(IsOverDay(tmp))
		{
			++tmp._month;
			if(IsOverMonth(tmp))
			{
				++tmp._year;	
				tmp._month=1;
			}
			tmp._day=1;
		}
		count++;
	}
	return tmp;
}
Date Date::operator-(int day)//日期与天数相减
{
	Date tmp(*this);
	int count=0;
	while(count!=day)
	{
		--tmp._day;
		if(tmp._day==0)
		{
			--tmp._month;
			if(tmp._month==0)
			{
				--tmp._year;	
				tmp._month=12;
			}
			if(tmp._month==1||tmp._month==3||tmp._month==5||tmp._month==7
				||tmp._month==8||tmp._month==10||tmp._month==12)
			{	

				tmp._day=31;
			}
			else if(tmp._month==2)
			{
				if(IsleapYear(tmp))
					tmp._day=29;
				else
					tmp._day=28;
			}
			else 
				tmp._day=30;

		}
		count++;
	}
	return tmp;

}


bool Date::IsleapYear(const Date& tmp)//是否闰年
{
	return ((tmp._year%4==0&&tmp._year%100!=0)||(tmp._year%400==0));
}

bool Date::IsOverDay(const Date& tmp)//是否超过1个月的天数
{
	if(tmp._month==1||tmp._month==3||tmp._month==5||tmp._month==7
		||tmp._month==8||tmp._month==10||tmp._month==12)
	{	
		
			return tmp._day>31;
	}
	else if(tmp._month==2)
	{
		 if(IsleapYear(tmp))
			 return tmp._day>29;
		 else
			 return tmp._day>28;
	}
	else 
		return tmp._day>30;

}

bool Date::IsOverMonth(const Date& tmp)//是否超过12个月的
{
	return tmp._month>12;
}
void Date::PrintDate()
{
	cout<<_year<<"年"<<_month<<"月"<<_day<<"日"<<endl;
}

bool Date::operator>(const Date& tmp)//日期比较
{
	return (_year>=tmp._year)&&(_month)>=(tmp._month)&&(_day>=tmp._day);
}

bool Date::operator==(const Date& tmp)
{
	return ((_year==tmp._year)&&(_month==tmp._month)&&(_day==tmp._day));

}
Date& Date::operator++()//日期与天数相加  前置++
{
	int count = 0;
	while (count != 1)
	{
		++(this->_day);
		if (IsOverDay(*this))
		{
			++(this->_month);
			if (IsOverMonth(*this))
			{
				++(this->_year);
				this->_month = 1;
			}
			this->_day = 1;
		}
		count++;
	}
	return *this;
}

Date& Date::operator+=(int day)//日期与天数相加
{

	int count = 0;
	while (count != day)
	{
		++(this->_day);
		if (IsOverDay(*this))
		{
			++(this->_month);
			if (IsOverMonth(*this))
			{
				++(this->_year);
				this->_month = 1;
			}
			this->_day = 1;
		}
		count++;
	}
	return *this;
}


//主函数测试文件

#include<iostream>
#include "date.h"
using namespace std;

void test()
{
	Date d1(2010,1,1);
	Date d2(2010,1,1);
	
	//(d2-35).PrintDate();
	//(d2 +1).PrintDate();
	//(++d2).PrintDate();
	//(d1 += 100).PrintDate();
	//cout<<(d2>d1)<<endl;
	cout<<d1-d2<<endl;
}

int main()
{
	test();

	return 0;
}


c++ 日期类

标签:c++;日期类;运算符重载

原文地址:http://shaungqiran.blog.51cto.com/10532904/1713436

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