用C语言计算给定年份的给定日期总数(即星期日或星期一或等)

时间:2013-07-01 18:29:31

标签: c calendar

由于标题清除了我想用c语言计算给定年份在1600-2500之间的给定日期总数。我已经编写了一些代码,但仍然计算闰年。请帮帮我。

#include<stdio.h>
#include<string.h>

int calculateNoOfDays(int year,int choice);
int isLeap(int year);

int main()
{
    int choice,year;    
    int count;
    char dayName[12];

     do{
         printf("Please Enter a year between 1601 and 2500\n");
         scanf("%d",&year);
     }
     while(!(year>=1601 && year<=2500));


 printf("Please select an option between 1 and 7: \nMonday: 1\nTuesday: 2\nWednesday: 3\nThursday: 4\nFriday: 5\nSaturday: 6\nSunday: 7\n");
        scanf("%d",&choice);


    switch((choice))
    {

            case 1:strcpy(dayName,"Monday");
    break;
        case 2:strcpy(dayName,"Tuesday");
    break;
    case 3:strcpy(dayName,"Wednesday");
    break;
    case 4:strcpy(dayName,"Thursday");
    break;
    case 5:strcpy(dayName,"Friday");
    break;
    case 6:strcpy(dayName,"Saturday");
    break;
    case 7:strcpy(dayName,"Sunday");
    break;

}

count=calculateNoOfDays(year,choice);
printf("For the given year, the number of %ss in that year is: %d \n",dayName,count);

return (1);
}

1 个答案:

答案 0 :(得分:0)

你可以阅读here,很容易确定哪些年是飞跃。这是伪代码:

if year is divisible by 400 then
   is_leap_year
else if year is divisible by 100 then
   not_leap_year
else if year is divisible by 4 then
   is_leap_year
else
   not_leap_year

通过这种方式,您获得了一年中的天数:365或366。

下一步将确定1月1日的工作日下降。这应该通过摆弄标准C date/time library functions来实现。你可能需要做一些迭代才能到达那里,但如果你聪明的话可能没有。

剩下的是一个简单的计算来计算你的天数。