给天数算到哪年哪月
给那个日期对象求差值
注意构造函数的有效参数判断
#pragma once
#include<iostream>
using namespace std;
class Date
{
private:
int _year;
int _month;
int _day;
public:
//默认成员函数
Date(int year = 1990, int month = 1, int day = 1);//缺省构造函数
Date(const Date& c);//拷贝构造函数
Date& operator=(const Date& c);//赋值操作符重载
~Date();//析构函数
void display();
Date AddDay(int day);
int SubDate(const Date& d);
bool operator<(const Date& d);
};
#include"Date.h"
Date::Date(int year, int month, int day)
{
if (year<1900)
{
cout << "year error" << endl;
return;
}
if (month <1 && month > 12)
{
cout << "month error" << endl;
return;
}
switch (month)
{
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))//瑞年2月有29天
{
if (day<1 || day>29)
{
printf("day error\n");
return;
}
}
else
{
if (day<1 || day>28)//非瑞年2月有28天
{
printf("day error\n");
return;
}
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day<1 || day>31)//1,3,5,7,8,10,12月为31天
{
cout << "day error" << endl;
}
break;
default:
if (day<1 || day>30)//其他月为30天
{
cout << "day error" << endl;
}
break;
}
//已确认日期有效
_year = year;
_month = month;
_day = day;
}
Date::Date(const Date& c)
{
_year = c._year;
_month = c._month;
_day = c._day;
}
Date& Date::operator=(const Date& c)//&表引用
{
if (this != &c)//&表取地址
{
_year = c._year;
_month = c._month;
_day = c._day;
}
return *this;
}
Date:: ~Date()
{
cout << "析构函数" << endl;
}
void Date::display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
int months[12] = { 31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//平年存储月份天数
Date Date:: AddDay(int day)
{
//瑞年366天,平年365天
int curYear = _year;
int curMonth = _month;
int curDay = _day;
int leftDay;
if (day == 0)
return *this;
else if (day > 0)
{
while (1)
{
if (curMonth == 2)
{
if ((curYear % 400 == 0) || (curYear % 4 == 0 && curYear % 100 != 0))
leftDay = 29;
else
leftDay = 28;
}
else
leftDay = months[curMonth - 1];
if (day <= leftDay)
{
curDay += day;
break;
}
else
{
day -= leftDay - curDay;
if (curMonth == 12 && day > 0)
{
++curYear;
curMonth = 1;
}
else
curMonth += 1;
curDay = 0;
}
}
}
else
{
day = -day;
while (1)
{
if (day <= curDay)
{
curDay -= day;
break;
}
else
{
day -=curDay;
if (curMonth == 1 && day > 0)
{
--curYear;
curMonth = 12;
}
else
curMonth -= 1;
}
if (curMonth == 2)
{
if ((curYear % 400 == 0) || (curYear % 4 == 0 && curYear % 100 != 0))
curDay = 29;
else
curDay = 28;
}
else
curDay = months[curMonth - 1];
}
}
Date tmp(curYear, curMonth, curDay);
return tmp;
}
int Date::SubDate(const Date& d)
{
int retDay = 0;
int flag = 1;
Date d1(*this);
Date d2(d);
if (!(*this < d))
{
d1 = d;
d2 = *this;
flag = -1;
}
int lefYear = d2._year;
int lefMonth = d2._month;
int leftDay = d2._day;
while (1)
{
if (d1._year == lefYear && (d1._month == lefMonth))
{
retDay += leftDay -d1. _day;
break;
}
else
{
retDay += leftDay;
if (lefMonth == 1)
{
--lefYear;
lefMonth = 12;
}
else
--lefMonth;
}
if (lefMonth == 2)
{
if ((lefYear % 400 == 0) || (lefYear % 4 == 0 && lefYear % 100 != 0))
leftDay = 29;
else
leftDay = 28;
}
else
leftDay = months[lefMonth - 1];
}
return retDay*flag;
}
bool Date::operator<(const Date& d)
{
if (_year<d._year)
{
if (_month < d._month)
{
if (_day < d._day)
{
return true;
}
else
return false;
}
else
return false;
}
else
{
return false;
}
}
#include<iostream>
#include"Date.h"
using namespace std;
void Test1()
{
Date d1(2012,4,10);
d1.display();
Date d2(d1.AddDay(-30));
d2.display();
//int ret = d2.SubDate(d1);
//cout << ret <<endl;
//Date d3(2015, 2, 3);
//cout << (d1 < d3) << endl;
}
int main()
{
Test1();
system("pause");
return 0;
}本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1712925
原文:http://10541556.blog.51cto.com/10531556/1712925