本代码实现求取一年中的某一天是这一年的第几天,此外在我的另一篇博客中有如何求取一年中某一天是周几,
/* Name:dayofyear.cpp
Update:2018 10 12
Author:Howe
version:1.0
work:calculate the day of the year
*/
#include<iostream>
using namespace std;
bool CheckLeap(const int &year)
{
if(year%4==0&&year%100!=0)
return true;
else if(year%100!=0&&year%400==0)
return true;
else
return false;
}
int main()
{
int Regtable[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int Leaptable[13]={0,31,60,91,121,152,182,213,244,274,305,335,366};
cout<<"please input year month day,forexample: 2018 09 10"<<endl;
int year,month,day;
int DayofYear;
cin>>year>>month>>day;
if(CheckLeap(year)==1)
DayofYear=Regtable[month-1]+day;
else
DayofYear=Leaptable[month-1]+day;
cout<<year<<"/"<<month<<"/"<<day<<" is the "<<DayofYear<<" day of the year"<<endl;
return 0;
}