日期计算器

来源:互联网 发布:南京行知小学宿舍 编辑:程序博客网 时间:2024/05/21 09:03

写一个简单的日期类实现日期计算器功能:

1 给一个日期计算这个日期加上一个天数后是哪一天

2 给一个日期计算这个日期减去一个天数后是哪一天

3 给定两个日期,求这两个日期中间相隔多少天


Date.h

#ifndef __DATE_H__#define __DATE_H__#define _CRT_SECURE_NO_DEPRECATE 1#include<iostream>#include<stdlib.h>using namespace std;class Date{public:Date(int year=1900, int month=1, int day=1){this->_year = year;this->_month = month;this->_day = day;}    Date(const Date& d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}~Date(){;}Date& operator=(const Date& d){if(this != &d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}return *this;}void Print_Date();int GetMonthDay(int year, int month);bool CheckLegality();Date& operator+=(int day);    //求某个日期day天后的日期是哪一天Date& operator-=(int day);  //求某个日期day天前的日期是哪一天bool operator>(const Date& d); bool operator==(const Date& d);Date& operator++();           //前置++int operator-(const Date& d); //求两个日期相差多少天private:int _year;int _month;int _day;};#endif //__DATE_H__

Date.cpp

#include"Date.h"void Date:: Print_Date(){cout<<this->_year<<"-"<<this->_month<<"-"<<this->_day<<endl;}int Date::GetMonthDay(int year, int month){int MonthDay[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};if(((year%4 == 0 && year%100 != 0) || (year%400 == 0)) && month == 2){return MonthDay[month]+1;   //解决闰年2月问题}else{return MonthDay[month];}}bool Date:: CheckLegality(){if((this->_month>0 && this->_month<=12)&&this->_day>0 && this->_day<=GetMonthDay(this->_year,this->_month))return true;elsereturn false;}//日期加上一个天数//加一个正数//加一个负数转至减一个数//加0       直接返回Date& Date:: operator+=(int day){//1给日期加上这个天数//2每次减去当前月的天数,月数进1,再检查月是否满,给年进一,直到天数合法if(day<0){day = -day;operator-=(day);}else{this->_day += day;while (CheckLegality() == 0){this->_day -= GetMonthDay(this->_year,this->_month);this->_month +=1;if(this->_month > 12){this->_year += 1;this->_month -= 12;}}}return *this;}//减去一个正数//减去一个负数  转致加一个数//减去0  直接返回Date& Date:: operator-=(int day){//1先加上这个负数//2每次加上前一个月的天数,然后月数减一,再检查月数是否合法,考虑给年减一,直到日期合法if(day<0)             //如果day为负数,减去一个负数等于加上一个正数,调用operator+{day = -day;operator+=(day);}else{this->_day -=day;while( CheckLegality() == 0){if(this->_month == 1)    //解决1月-1 得到0月   0月返回0天问题{this->_day += 31;this->_year -=1;this->_month = 12;}else{this->_day += GetMonthDay(this->_year,(this->_month)-1);this->_month -=1;}}}return *this;}//>重载bool Date:: operator>(const Date& d){if(this->_year > d._year){return true;}else if(this->_year < d._year){return false;}else{if (this->_month > d._month){return true;}else if(this->_month < d._month){return false;}else{if(this->_day > d._day){return true;}else if (this->_day < d._day){return false;}elsereturn false;}}}bool Date:: operator==(const Date& d){if(this->_year == d._year &&this->_month == d._month &&this->_day == d._day) return true;elsereturn false;}Date& Date:: operator++(){this->_day +=1;if(CheckLegality() == 0){   this->_day -= GetMonthDay(this->_year,this->_month);   this->_month += 1;   if(CheckLegality() == 0)   {this->_month = 1;this->_year += 1;   }}return *this;}//先判断哪个日期大哪个日期小//用小的日期不断++ 逼近较大的日期直到相等int Date:: operator-(const Date& d){Date big;Date small;int count = 0;if(operator>(d)){big = *this;small = d; }else{big = d;small = *this;}while( !(small.operator==(big))){small.operator++();count++;}return count;}

test.cpp

#include"Date.h"int main(){Date d1(2008,1,1);Date d2 = d1;d1.operator+=(-1000);d1.Print_Date();d2.operator-=(-1000);d2.Print_Date();int ret = d2.operator-(d1);  cout<<ret<<endl;system("pause");return 0;}


 这首久久时间网上计算结果:





只写了主要功能,不太必要的功能没写。