设计思路很简单,就是日期一天天增加,在增加的过程中,严格遵守1 3 5 7 8 10 12 月大,2月闰年29天 平年28天的规则。
#include <iostream>
using namespace std;
class date
{
public:
bool judgeCF()
{
if (m_mouth == 4 || m_mouth == 6 || m_mouth == 9 || m_mouth == 11)
{
if (m_date>30)
{
return true;
}
}
if (m_mouth == 1 || m_mouth == 3 || m_mouth == 5 || m_mouth == 7
|| m_mouth == 8 || m_mouth == 10)
{
if (m_date>31)
{
return true;
}
}
if (m_mouth==2)
{
if (m_date>29)
{
return true;
}
}
return false;
}
date(int year, int mouth, int date, int n)
{
/* 通过天数一天天加的方式,来增长,1 3 5 7 8 10 12 大月31天
2 4 6 9 11 小月 30天,先不计算闰年和润2月*/
m_year = year;
m_mouth = mouth;
m_date = date;
bool judge;
if (judge=judgeCF())
{
cout << "输入错误";
return;
}
int tag = n;
//int increase=0;
while (tag--)//最后一次在程序块里面的tag=0,所以是先用判断再-
{
if (m_mouth==1 || m_mouth==3 || m_mouth == 5 || m_mouth == 7
|| m_mouth == 8 || m_mouth == 10 )
{
m_date++;
if (m_date == 32)
{
m_mouth++;
if (tag == 0)
{
m_date = 1;
break;
}
else
{
m_date = 0;
}
}
}
if ( m_mouth == 4 || m_mouth == 6 || m_mouth == 9 || m_mouth == 11)
{
m_date++;
if (m_date==31)
{
m_mouth++;
if (tag==0)
{
m_date = 1;
break;
}
else
{
m_date = 1;
}
}
}
if (m_mouth==2)
{
m_date++;
if (m_year % 400 == 0 || (m_year % 4 == 0 && m_year % 100 != 0))
{
if (m_date==30)
{
m_mouth++;
if (tag == 0)
{
m_date = 1;
break;
}
else
{
m_date = 1;
}
}
}
else
{
if (m_date == 29)
{
m_mouth++;
if (tag == 0)
{
m_date = 1;
break;
}
else
{
m_date = 1;
}
}
}
}
if (m_mouth==12)
{
m_date++;
if (m_date == 32)
{
m_year++;
m_mouth = 1;
if (tag == 0)
{
m_date = 1;
break;
}
else
{
m_date = 0;
}
}
}
}
}
void print()
{
cout << m_year << "年" << m_mouth << "月" << m_date << "日"<<endl;
}
private:
int m_year;
int m_mouth;
int m_date;
};
int main()
{
date da(2019,10,30,120);//23正常,小月正常,大月正常 ,二月正常 2019,9,3,58
da.print();
system("pause");
return 0;
}