首页 > 代码库 > 输入年、月、日计算是星期几
输入年、月、日计算是星期几
/*************************************************************************
该程序功能:
1.输入年、月、日判断这天是星期几
2.打印出其中的闰年
3.计算从1600到输入时间,总共的天数
说明:
输入的年份必须大于1600
**************************************************************************/
#include <stdio.h>
/* 计算某一天是这年中的第几天 */
int calculate_days(int year,int month,int day)
{
int cnt;
int leapYear = 0;
if((month > 12 || month < 0) || (day > 31 || day < 0))
{
printf("input month or day error,please input correct data\n");
return -1;
}
/* 判断是否为闰年 */
if((year%4==0&&year%100!=0)||year%400==0)
{
leapYear = 1;
}
switch(month)
{
case 1:
cnt = day;
break;
case 2:
cnt = 31 + day;
break;
case 3:
if(leapYear)
{
cnt = 31 + 29 + day;
}
else
{
cnt = 31 + 28 + day;
}
break;
case 4:
cnt = 31 + 28 + 31 + day;
break;
case 5:
cnt = 31 + 28 + 31 + 30 + day;
break;
case 6:
cnt = 31 + 28 + 31 + 30 + 31 + day;
break;
case 7:
cnt = 31 + 28 + 31 + 30 + 31 + 30 + day;
break;
case 8:
cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 9:
cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
break;
case 10:
cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
break;
case 11:
cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
break;
case 12:
cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
break;
default :
printf("input month error!!\n");
break;
}
printf("The day is %dth days for this year\n",cnt);
return cnt;
}
/* 计算从1600年1月1日到输入的日期总共的天数 */
int total_day(int year,int month,int day)
{
int i = 0;
int sumDays = 0;
if(year<1600)
{
printf("Does not support the query is less than 1600 years\n");
printf("please input more than 1600 years\n");
return -1;
}
for(i=1600;i<year;i++)
{
if((i%4==0&&i%100!=0)||i%400==0)
{
sumDays += 366;
printf("leapyear=%d\n",i);
}
else
{
sumDays += 365;
}
}
sumDays += calculate_days(year,month,day);
printf("sumDays=%d\n",sumDays);
return sumDays;
}
int main(int argc,char *argv[])
{
int totalDays = 0;
int remind = 0;
int result = 0;
int year = 0;
int month = 0;
int day = 0;
printf("please year-month-day data\n");
scanf("%d-%d-%d",&year,&month,&day);
totalDays = total_day(year,month,day);
if(totalDays<0)
{
printf("total_day error!\n");
return -1;
}
remind = totalDays % 7;
result = ((remind - 1) + 6)% 7;
switch(result)
{
case 0:
printf("This day is Sunday\n");
break;
case 1:
printf("This day is Monday\n");
break;
case 2:
printf("This day is Tuesday\n");
break;
case 3:
printf("This day is Wednesday\n");
break;
case 4:
printf("This day is Thursday\n");
break;
case 5:
printf("This day is Friday\n");
break;
case 6:
printf("This day is Saturday\n");
break;
default:
break;
}
return 0;
}
输入年、月、日计算是星期几