已知某一天是周几 求给定的一天是周几的算法 C++实现


#include<iostream>
using namespace std; struct Date{
int year,month,day;
}; enum Week{
MON=,
TUE,
WED,
THU,
FRI,
SAT,
SUN,
}; int isLeapYear(int y)
{
if((y%==&&y%!=)||y%==)return ;
else return ;
} int DaysGone(Date d,int *restdays)
{/*
*返回值是某个日期在一年中是第多少天
*restdays是这年还剩多少天
*/
int leap=;
int days;
if(isLeapYear(d.year))leap=;
switch(d.month){
case :days=d.day;break;
case :days=d.day+;break;
case :if(leap)days=d.day++;
else days=d.day++;
break;
case :if(leap)days=d.day+++;
else days=d.day+++;
break;
case :if(leap)days=d.day++++;
else days=d.day++++;
break;
case :if(leap)days=d.day+++++;
else days=d.day+++++;
break;
case :if(leap)days=d.day++++++;
else days=d.day++++++;
break;
case :if(leap)days=d.day+++++++;
else days=d.day+++++++;
break;
case :if(leap)days=d.day++++++++;
else days=d.day++++++++;
break;
case :if(leap)days=d.day+++++++++;
else days=d.day+++++++++;
break;
case :if(leap)days=d.day++++++++++;
else days=d.day++++++++++;
break;
case :if(leap)days=d.day+++++++++++;
else days=d.day+++++++++++;
break;
}
if(leap)*restdays=-days;
else *restdays=-days;
return days;
} int ComputeDays(Date early,Date later)
{
int leapcount=;
int y,earlyrestdays=,laterrestdays=,latergonedays=;
int result;
for(y=early.year+;y<later.year;y++)
if(isLeapYear(y))leapcount++;//不包含要求计算的两年
DaysGone(early,&earlyrestdays);
latergonedays=DaysGone(later,&laterrestdays);
result=latergonedays+earlyrestdays;
result+=(later.year-early.year-)*+leapcount;
return result;
} int WhoEarly(Date d1,Date d2)
{//d1早则返回1,d2则0,=则-1
int res=;
int y,m,d;
y=d1.year-d2.year;
m=d1.month-d2.month;
d=d1.day-d2.day;
if(y<)res=;
else if(y==){
if(m<)res=;
else if(m==){
if(d<)res=;
else if(d==)res=-;
else res=;
}//else if
else res=;
}//else if
else res=;
} int Mod7(Date d1,Date d2,int weekday)
{//此处d1是周几为已知数weekday
int tmp,res;
tmp=WhoEarly(d1,d2);
if(tmp==-)res=;
else if(tmp==)res=ComputeDays(d1,d2)%;
else res=-ComputeDays(d2,d1)%;
switch(weekday){
case MON:res=(res+)%;break;
case TUE:res=(res+)%;break;
case WED:res=(res+)%;break;
case THU:res=(res+)%;break;
case FRI:res=(res+)%;break;
case SAT:res=(res+)%;break;
case SUN:res=(res+)%;break;
}
return res;
} /*
*程序没有进行错误检查
*/
int main()
{
Date d1,d2;
int week,res;
printf("enter a date:year month day=======");
scanf("%d%d%d",&d1.year,&d1.month,&d1.day);
printf("week ?");
scanf("%d",&week);
printf("enter another date but don't know on which weekday======");
scanf("%d%d%d",&d2.year,&d2.month,&d2.day);
res=Mod7(d1,d2,week);
printf("it's ");
switch(res)
{
case MON:printf("MON\n");break;
case TUE:printf("TUE\n");break;
case WED:printf("WED\n");break;
case THU:printf("THU\n");break;
case FRI:printf("FRI\n");break;
case SAT:printf("SAT\n");break;
case SUN:printf("SUN\n");break;
}
return ; }
上一篇:android HOME点击事件的获取


下一篇:java、tomcat环境变量的配置(针对windows)