源码如下
(有问题还请指出)
/*计算两个日期之间的天数并指出各自的星期数*/
/*为方便起见日期的设置范围为1900年1月1日到2100年12月31日*/
#include<iostream>
using namespace std;
enum Week { Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};
static const int NR_months[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//非闰年的月份对应天数
static const int R_months[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };//闰年的月份对应天数
struct Date
{
int year;
int month;
int day;
Week wek;
};
void InitPrint();//打印基本信息
bool DateCheck(Date d);//判断参数日期是否符合规范
bool RYearCheck(int year);//判断是否为闰年
Date DateArrange(Date d1, Date d2);//返回的d1,d2中靠后的日期
int DaysToLastYear(Date d);//计算当前日期到上一年12月31日的间隔天数
int DaysToNextYear(Date d);//实际上算出的是当前日期到本年12月31日的间隔天数
int DaysBetweenYears(int year1,int year2);//计算两个年份同一天之间的间隔天数
Week WeekCheck(Date d);//判断某个日期对应的星期数并返回
int main(void)
{
InitPrint();
for (;;)
{
Date D1{ 0,0,0,Sunday };
Date D2{ 0,0,0,Sunday };
while (1)
{
cout << "请输入第一个日期:";
cin >> D1.year >> D1.month >> D1.day;
if (DateCheck(D1) == false)
{
cout << "输入的日期格式有误或超出范围,请重新输入!" << endl;
}
else
{
break;
}
}
while (1)
{
cout << "请输入第二个日期:";
cin >> D2.year >> D2.month >> D2.day;
if (DateCheck(D2) == false)
{
cout << "输入的日期格式有误或超出范围,请重新输入!" << endl;
}
else
{
break;
}
}
Date temp = DateArrange(D1, D2);
D1 = ((temp.year == D2.year) && (temp.month == D2.month) && (temp.day == D2.day)) ? D1 : D2;
D2 = temp;
D1.wek = WeekCheck(D1);
D2.wek = WeekCheck(D2);
int days = DaysToNextYear(D1) + DaysToLastYear(D2) + DaysBetweenYears(D1.year, (D2.year - 1));
cout << D1.year << "年" << D1.month << "月" << D1.day << "日" << "是星期" << int(D1.wek) + 1;;
cout << endl;
cout << D2.year << "年" << D2.month << "月" << D2.day << "日" << "是星期" << int(D2.wek) + 1;
cout << endl;
cout << "它们相隔" << days << "天!" << endl;
system("pause");
}
}
bool RYearCheck(int year)
{
return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
}
bool DateCheck(Date d)
{
bool flag = RYearCheck(d.year);
if ((d.year > 2100) || (d.year < 1900)|| (d.month > 12) || (d.month < 1)
|| (d.day > (flag ? R_months[d.month-1] : NR_months[d.month-1])) || (d.day < 1))
{
return false;
}
else
{
return true;
}
}
void InitPrint()
{
cout << "本程序计算两个日期之间的天数并指出各自的星期数" << endl;
cout << "为方便起见日期的设置范围为1900年1月1日到2100年12月31日" << endl;
cout << "日期输入格式为:Year Month Day" << endl;
}
Date DateArrange(Date d1, Date d2)
{
if (d1.year != d2.year)
{
if (d1.year > d2.year)
{
return d1;
}
else
{
return d2;
}
}
else if (d1.month != d2.month)
{
if (d1.month > d2.month)
{
return d1;
}
else
{
return d2;
}
}
else
{
if (d1.day > d2.day)
{
return d1;
}
else
{
return d2;
}
}
}
int DaysToLastYear(Date d)
{
int r = int(RYearCheck(d.year));
int days=0;
for (int i = 0; i < d.month-1; i++)
{
if (r == 0)
{
days += NR_months[i];
}
else
{
days += R_months[i];
}
}
days += d.day;
return days;
}
int DaysToNextYear(Date d)
{
int r = int(RYearCheck(d.year));
int days = 0;
for (int i = d.month-1; i < 12; i++)
{
if (r == 0)
{
days += NR_months[i];
}
else
{
days += R_months[i];
}
}
days -= d.day;
return days;
}
int DaysBetweenYears(int year1, int year2)
{
int days=0;
if (year1 > year2)
{
return -(365 + RYearCheck(year1));
}
else
{
for (int i = 1; i <= (year2 - year1); i++)
{
days += 365;
days += RYearCheck(year1 + i);
}
return days;
}
}
Week WeekCheck(Date d)//特别说明,此函数首先计算了目标日期到1900年1月1日(星期一)的间隔天数,然后算出其星期数
{
Date origin = {1900,1,1,Monday};
int days = DaysToNextYear(origin) + DaysToLastYear(d) + DaysBetweenYears(origin.year, (d.year - 1));
return Week( days % 7);
}