c语言程序设计:任意给出一个年月日,求出是星期几.不能用数组,只能用if语句

2024-11-20 10:29:44
推荐回答(2个)
回答(1):

int y, m, d;
int w;
int day;
#define runnian(y) (!(y % 4 != 0) || (y % 400 != 0 && y % 100 == 0))
while(1)
{
printf("please input date in year month day\n");
scanf("%d%d%d", &y,&m,&d);
while(getchar()!='\n');
printf("get date %d-%d-%d\n", y, m, d);
if(y < 0
|| (m < 1 || m > 12)
|| d < 1 || d > 31
|| ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30 )
|| (m == 2 && d > (runnian(y) ? 29 : 28)))
{
printf("invalid date input\n");
continue;
}
day = 0;
day += y * 365;
day += (y - 1)/4;
day -= (y - 1)/100;
day += (y - 1)/400;
if(m > 1) day+= 31;
if(m > 2)
{
day += 28;
if(y % 4 == 0) day ++;
if(y % 100 == 0) day --;
if(y % 400 == 0) day ++;
}
if(m > 3) day += 31;
if(m > 4) day += 30;
if(m > 5) day += 31;
if(m > 6) day += 30;
if(m > 7) day += 31;
if(m > 8) day += 31;
if(m > 9) day += 30;
if(m > 10) day += 31;
if(m > 11) day += 30;
day += d;
day += 5;
w = day % 7;
printf("this day is %d\n", w + 1);
}
return 0;

公元后的都可以 公元前的就不行了

回答(2):

同学你北理工的吧?