c语言输入一个日期输出他是一年的第几天星期几!!跪求
1个回答
2013-10-23
展开全部
源程序如下:(有点难,搞了一个小时,加点分吧)
#include <stdio.h>
int YMD_2_JD(int y, int m, int d){
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int JD,i;
JD=d;
for (i=0;i<m;i++) JD+=MonthDay[i];
if ( ((y%4==0)&&(y%100!=0) || (y%400==0) ) && (m>2)) JD++;
return JD;
}
void main ()
{
int y,m,d,c,s,w,x;
printf("请输入8位年月日,以空格隔开");
scanf("%d%d%d",&y,&m,&d);
if (m==1) c=d;
else if (m==2) c=31+d;
else if (m==3) c=31+28+d;
else if (m==4) c=31+28+31+d;
else if (m==5) c=31+28+31+30+d;
else if (m==6) c=31+28+31+30+31+d;
else if (m==7) c=31+28+31+30+31+30+d;
else if (m==8) c=31+28+31+30+31+30+31+d;
else if (m==9) c=31+28+31+30+31+30+31+31+d;
else if (m==10) c=31+28+31+30+31+30+31+31+30+d;
else if (m==11) c=31+28+31+30+31+30+31+31+30+31+d;
else if (m==12) c=31+28+31+30+31+30+31+31+30+31+30+d;
if (m>2)
{if (y%100==0) {if (y%400==0) c=c+1;}
else if (y%4==0) c=c+1;}
s=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400+c;
x=YMD_2_JD(y,m,d);
printf("第%d天\n",x);
w=s%7;
switch (w)
{
case 0: printf("星期日\n");break;
case 1: printf("星期一\n");break;
case 2: printf("星期二\n");break;
case 3: printf("星期三\n");break;
case 4: printf("星期四\n");break;
case 5: printf("星期五\n");break;
case 6: printf("星期六\n");break;
printf("\n");
}
}
VC++测试如下:
#include <stdio.h>
int YMD_2_JD(int y, int m, int d){
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int JD,i;
JD=d;
for (i=0;i<m;i++) JD+=MonthDay[i];
if ( ((y%4==0)&&(y%100!=0) || (y%400==0) ) && (m>2)) JD++;
return JD;
}
void main ()
{
int y,m,d,c,s,w,x;
printf("请输入8位年月日,以空格隔开");
scanf("%d%d%d",&y,&m,&d);
if (m==1) c=d;
else if (m==2) c=31+d;
else if (m==3) c=31+28+d;
else if (m==4) c=31+28+31+d;
else if (m==5) c=31+28+31+30+d;
else if (m==6) c=31+28+31+30+31+d;
else if (m==7) c=31+28+31+30+31+30+d;
else if (m==8) c=31+28+31+30+31+30+31+d;
else if (m==9) c=31+28+31+30+31+30+31+31+d;
else if (m==10) c=31+28+31+30+31+30+31+31+30+d;
else if (m==11) c=31+28+31+30+31+30+31+31+30+31+d;
else if (m==12) c=31+28+31+30+31+30+31+31+30+31+30+d;
if (m>2)
{if (y%100==0) {if (y%400==0) c=c+1;}
else if (y%4==0) c=c+1;}
s=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400+c;
x=YMD_2_JD(y,m,d);
printf("第%d天\n",x);
w=s%7;
switch (w)
{
case 0: printf("星期日\n");break;
case 1: printf("星期一\n");break;
case 2: printf("星期二\n");break;
case 3: printf("星期三\n");break;
case 4: printf("星期四\n");break;
case 5: printf("星期五\n");break;
case 6: printf("星期六\n");break;
printf("\n");
}
}
VC++测试如下:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询