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



#include<iostream>
using namespace std;

struct Date{
 int year,month,day;
};

enum Week{
MON=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN,
};

int isLeapYear(int y)
{
 if((y%4==0&&y%100!=0)||y%400==0)return 1;
 else return 0;
}

int DaysGone(Date d,int *restdays)
{/*
 *返回值是某个日期在一年中是第多少天
 *restdays是这年还剩多少天
 */
 int leap=0;
 int days;
 if(isLeapYear(d.year))leap=1;
 switch(d.month){
  case 1:days=d.day;break;
  case 2:days=d.day+31;break;
  case 3:if(leap)days=d.day+29+31;
         else days=d.day+28+31;
         break;
  case 4:if(leap)days=d.day+31+29+31;
         else days=d.day+31+28+31;
         break;
  case 5:if(leap)days=d.day+30+31+29+31;
         else days=d.day+30+31+28+31;
         break;
  case 6:if(leap)days=d.day+31+30+31+29+31;
         else days=d.day+31+30+31+28+31;
         break;
  case 7:if(leap)days=d.day+30+31+30+31+29+31;
         else days=d.day+30+31+30+31+28+31;
         break;
  case 8:if(leap)days=d.day+31+30+31+30+31+29+31;
         else days=d.day+31+30+31+30+31+28+31;
         break;
  case 9:if(leap)days=d.day+31+31+30+31+30+31+29+31;
         else days=d.day+31+31+30+31+30+31+28+31;
         break;
  case 10:if(leap)days=d.day+30+31+31+30+31+30+31+29+31;
         else days=d.day+30+31+31+30+31+30+31+28+31;
         break;
  case 11:if(leap)days=d.day+31+30+31+31+30+31+30+31+29+31;
         else days=d.day+31+30+31+31+30+31+30+31+28+31;
         break;
  case 12:if(leap)days=d.day+30+31+30+31+31+30+31+30+31+29+31;
         else days=d.day+30+31+30+31+31+30+31+30+31+28+31;
         break;
  }
 if(leap)*restdays=366-days;
 else *restdays=365-days;
 return days;
}

int ComputeDays(Date early,Date later)
{
 int leapcount=0;
 int y,earlyrestdays=0,laterrestdays=0,latergonedays=0;
 int result;
 for(y=early.year+1;y<later.year;y++)
  if(isLeapYear(y))leapcount++;//不包含要求计算的两年
 DaysGone(early,&earlyrestdays);
 latergonedays=DaysGone(later,&laterrestdays);
 result=latergonedays+earlyrestdays;
 result+=(later.year-early.year-1)*365+leapcount;
 return result; 
}

int WhoEarly(Date d1,Date d2)
{//d1早则返回1,d2则0,=则-1
 int res=0;
 int y,m,d;
 y=d1.year-d2.year;
 m=d1.month-d2.month;
 d=d1.day-d2.day;
 if(y<0)res=1;
 else if(y==0){
      if(m<0)res=1;
       else if(m==0){
       if(d<0)res=1;
        else if(d==0)res=-1;
         else res=0;
      }//else if
      else res=0;
  }//else if
 else res=0;
}

int Mod7(Date d1,Date d2,int weekday)
{//此处d1是周几为已知数weekday
 int tmp,res;
 tmp=WhoEarly(d1,d2);
 if(tmp==-1)res=0;
 else if(tmp==1)res=ComputeDays(d1,d2)%7;
 else res=7-ComputeDays(d2,d1)%7;
 switch(weekday){
  case MON:res=(res+1)%7;break;
  case TUE:res=(res+2)%7;break;
  case WED:res=(res+3)%7;break;
  case THU:res=(res+4)%7;break;
  case FRI:res=(res+5)%7;break;
  case SAT:res=(res+6)%7;break;
  case SUN:res=(res+7)%7;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 0;

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。