#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
class CDate{
int m_nday;
int m_nMonth;
int m_nYear;
public:
CDate();
CDate(int day,int month,int year);
void Display();
void AddDay();
void Setdate(int day,int month,int year);
~CDate();
private:
bool IsLeapYear();
};
CDate::CDate(){}
CDate::CDate(int year,int month,int day){
m_nday = day;
m_nMonth = month;
m_nYear = year;
}
void CDate::Display(){
char day[3];
char month[3];
char year[5];
//itoa(m_nday,day,10);
//itoa(m_nMonth,month,10);
//itoa(m_nYear,year,10);
snprintf(day, sizeof(day), "%d", m_nday);
snprintf(month, sizeof(month), "%d", m_nMonth);
snprintf(year, sizeof(year), "%d", m_nYear);
cout << day << "/" << month << "/" << year << endl;
}
void CDate::AddDay(){
m_nday++;
if(IsLeapYear()){
if((m_nMonth == 2) && (m_nday == 30)){
m_nMonth++;
m_nday=1;
return;
}
}else{
if((m_nMonth == 2) && (m_nday == 29)){
m_nMonth++;
m_nday=1;
return;
}
}
if(m_nday > 31){
if(m_nMonth == 12){
m_nYear++;
m_nMonth = 1;
m_nday = 1;
}else{
m_nMonth++;
m_nday = 1;
}
}else if(m_nday == 31){
if(m_nMonth==4 || m_nMonth==6 || m_nMonth==9 || m_nMonth==12){
m_nMonth++;
m_nday = 1;
}
}
}
void CDate::Setdate(int year,int month,int day){
m_nday = day;
m_nMonth = month;
m_nYear = year;
}
CDate::~CDate() {}
bool CDate::IsLeapYear()
{
//自己
return ((m_nYear%4==0 && m_nYear%100 != 0) || m_nYear % 400==0);
}
int main(){
CDate d;
//d.Setdate(2012,2,28);
d.Setdate(2012,2,29);
cout<<"当前日期:";
d.Display();
d.AddDay();
cout<<"当前日期加1:";
d.Display();
return 0;
} 李春葆 p89