0

正如标题所清除的那样,我想用 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);
}
4

1 回答 1

0

正如您可以在此处阅读的那样,很容易确定哪些年份是闰年。这是伪代码:

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 日期/时间库函数来实现。你可能需要做一些迭代才能到达那里,但如果你很聪明,也许不需要。

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

于 2013-07-01T18:45:19.227 回答