/*2019年1月29日*/
/*作者:石辅寸*/
/*根據年月日計算星期幾*/
/* S = {x - 1 [(x-1)/4] - [(x-1)/100] + [(x-1)/400] + C}%7 */
/* S為星期序數,x為年份,C為當年元旦至當日(包含當日)的天數 */
#include<stdio.h>
main()
{
int a,b,c,d,s;
printf("清輸入年月日:");
scanf("%d%d%d",&a,&b,&c);
/*檢驗輸入是否正確*/
if(b <= 0 || b > 12)
{
printf("月份輸入錯誤,請重新輸入!");
}
if(c <= 0)
{
printf("日期輸入錯誤,請重新輸入!");
}
if(c >= 31)
{
switch(b)
{
case 1: printf("日期輸入錯誤,請重新輸入!");
break;
case 3: printf("日期輸入錯誤,請重新輸入!");
break;
case 5: printf("日期輸入錯誤,請重新輸入!");
break;
case 7: printf("日期輸入錯誤,請重新輸入!");
break;
case 8: printf("日期輸入錯誤,請重新輸入!");
break;
case 10:printf("日期輸入錯誤,請重新輸入!");
break;
case 12:printf("日期輸入錯誤,請重新輸入!");
break;
}
}
if(c >= 30)
{
switch(b)
{
case 4: printf("日期輸入錯誤,請重新輸入!");
break;
case 6: printf("日期輸入錯誤,請重新輸入!");
break;
case 9: printf("日期輸入錯誤,請重新輸入!");
break;
case 11: printf("日期輸入錯誤,請重新輸入!");
break;
}
}
if((a%4 == 0 && a%100 != 0) || (a%400 == 0))
{
if(b == 2)
{
if(c >= 29)
{
printf("日期輸入錯誤,請重新輸入!");
}
}
}
else
{
if(b == 2)
{
if(c >= 28)
{
printf("日期輸入錯誤,請重新輸入!");
}
}
}
/*閏年的情況*/
if((a%4 == 0 && a%100 != 0) || (a%400 == 0))
{
switch(b)
{
case 1: d = c;
break;
case 2: d = c + 31;
break;
case 3: d = c + 60;
break;
case 4: d = c + 91;
break;
case 5: d = c + 121;
break;
case 6: d = c + 152;
break;
case 7: d = c + 182;
break;
case 8: d = c + 213;
break;
case 9: d = c + 244;
break;
case 10:d = c + 274;
break;
case 11:d = c + 305;
break;
case 12:d = c + 335;
break;
}
}
/*平年的情況*/
else
{
switch(b)
{
case 1: d = c;
break;
case 2: d = c + 31 - 1;
break;
case 3: d = c + 60 - 1;
break;
case 4: d = c + 91 - 1;
break;
case 5: d = c + 121- 1;
break;
case 6: d = c + 152- 1;
break;
case 7: d = c + 182- 1;
break;
case 8: d = c + 213- 1;
break;
case 9: d = c + 244- 1;
break;
case 10:d = c + 274- 1;
break;
case 11:d = c + 305- 1;
break;
case 12:d = c + 335- 1;
break;
}
}
s = (a-1 + (a-1)/4 - (a-1)/100 + (a-1)/400 + d)%7;
switch(s)
{
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;
}
}
根据年月日判断星期几的c程序
最新推荐文章于 2022-10-17 09:14:40 发布