前几天在备赛时看到高斯日记这个问题,现在写了一个求某个日期是当年的第几天,程序很简单,直接上代码。
# include <iostream>
# include <string>
using namespace std;
# define OK 1;
# define ERROR 0;
int IsLeapYear(int Year){
return ((Year%4==0&&Year%100!=0)||Year%400==0);
}
int DaysofYear(int Year,int Month,int Day){
int DayA[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int DayB[12]={31,29,31,30,31,30,31,31,30,31,30,31};
if(IsLeapYear(Year)){
int Days=0;
if(Month==1){
Days=Day;
return Days;
}
else{
for(int Index=0;Index<Month-1;Index++){
Days=Days+DayB[Index];
}
return Days+Day;
}
}
else{
int Days;
if(Month==1){
Days=Day;
return Days;
}
else{
for(int Index=0;Index<Month-1;Index++){
Days=Days+DayA[Index];
}
return Days+Day;
}
}
}
int DateCheck(int Year,int Month,int Day){
if((Year>=1)&&(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12)&&(Day>=1&&Day<=31)){
return OK;
}
else if((Year>=1)&&(Month==4||Month==6||Month==9||Month==11)&&(Day>=1&&Day<=30)){
return OK;
}
else{
if((Year>=1)&&IsLeapYear(Year)&&(Day>=1&&Day<=29)){
return OK;
}
else{
if((Year>=1)&&Day>=1&&Day<=28){
return OK;
}
}
}
return ERROR;
}
int main(void){
int Year,Month,Day;
bool Flag=true;
while(Flag){
cout<<"Enetr the Date as Format:yyyy mm dd:";
cin>>Year>>Month>>Day;
if(IsLeapYear(Year)){
if(DateCheck(Year,Month,Day)){
cout<<Year<<"-"<<Month<<"-"<<Day<<"is the No "<<DaysofYear(Year,Month,Day)<<endl;
Flag=false;
}
else{
cout<<"Date format is fault"<<endl;
Flag=true;
}
}
else{
if(DateCheck(Year,Month,Day)){
cout<<Year<<"-"<<Month<<"-"<<Day<<"is the No "<<DaysofYear(Year,Month,Day)<<endl;
Flag=false;
}
else{
cout<<"Date format is fault"<<endl;
Flag=true;
}
}
}
cout<<IsLeapYear(Year)<<endl;
system("pause");
return 0;
}