计算任意一个日期是星期几

付费节点推荐


免费节点


节点使用教程


//公式 w = (int)(c/4) - 2*c + y + (int)(y/4)+ (int)(13*(m+1)/5) + d - 1;

//可以获取当前日期,并计算今天是星期几.

#include <stdio.h>

#include <stdlib.h>

#include  <math.h>

#include <time.h>

#include <string.h>

int whatday(int,int,int);

int  Negativenum(int );

int today();

 

int main()

{

int year,mon,day,i;

printf("1)计算当日日期\n2)计算任意日期是星期几\n======================================\n请选择:");

scanf("%d",&i);

switch(i)

{

case 1:today();  break;

case 2: printf("输入日期:如:1994 7 19\n"); scanf("%d%d%d",&year,&mon,&day);

whatday(year,mon,day); break;

default : printf("输入有误!\n");

}

system("PAUSE");

return 0;

}

 

int whatday(int year,int mon,int day)

{

int y,d,m,c,w;

m=mon;

d=day;

c=year/100;   //取年份的前两位数

y=year%100;   //取年份的后两位数

if(m>=1&&m<=2)#xa0;#xa0; //1,2月份时,取前一年

year-=1;

w = (int)(c/4) - 2*c + y + (int)(y/4)+ (int)(13*(m+1)/5) + d - 1;

w=w%7;

Negativenum(w);

switch(w)

{

case 0:printf("that day is sunday!\n"); break;

case 1:printf("that day is Monday!\n");break;

case 2:printf("that day is wendesday!\n");break;

case 3:printf("that day is thursday!\n"); break;

case 4:printf("that day is friday!\n"); break;

case 5:printf("that day is tuseday!\n"); break;

case 6:printf("that day is sadurday!\n");break;

default:printf("cout error!\n");

 

}

}

    int Negativenum(int w)  //w可能是负数,转换成正数

{

if(w < 0)

w += 7;

return w;

}

 

int today()

{

int year;

int mon;

int day;

time_t t=time(NULL);

struct tm* today =localtime(&t);

year=today->tm_year +1900;

mon=today->tm_mon+1;

day=today->tm_mday;

printf("today is:%d年%d月%d日\n",year,mon,day);

whatday(year,mon,day);

}

 

 

 

 

 

未经允许不得转载:Bcoder资源网 » 计算任意一个日期是星期几

相关推荐

赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册