正如标题所示,我想用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);
}
最佳答案
正如您可以阅读的那样 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 来实现。 。您可能需要进行一些迭代才能达到目标,但如果您足够聪明,也许不需要。
剩下的就是简单的计算即可得到您的天数。
关于用C语言计算给定年份中给定日期的总数(即星期日或星期一等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17411236/