概述
输入年月日,求出是一年中的第多少天,区分闰年和非闰年。
[tiger506@localhost C_C++]$ cat -n doy.c
1 #include<stdio.h>
2
3 int day_of_year(int month, int day, int year)
4 {
5 int sum = 0;
6 switch(month){
7 case 1: sum = 0 + day;
8 break;
9 case 2: sum = 31 + day;
10 break;
11 case 3: sum = 31 + 28 + day;
12 break;
13 case 4: sum = 31 + 28 + 31 + day;
14 break;
15 case 5: sum = 31 + 28 + 31 + 30 + day;
16 break;
17 case 6: sum = 31 + 28 + 31 + 30 + 31 + day;
18 break;
19 case 7: sum = 31 + 28 + 31 + 30 + 31 + 30 + day;
20 break;
21 case 8: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
22 break;
23 case 9: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
24 break;
25 case 10: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
26 break;
27 case 11: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
28 break;
29 case 12: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
30 break;
31 default: break;
32 }
33
34 if(year % 4 == 0 && year % 100 != 0)
35 sum = sum + 1;
36 if(year % 400 == 0)
37 sum = sum + 1;
38 return sum;
39 }
40
41
42 int main(void)
43 {
44 int month, day, year;
45 printf("Enter month/day/year :");
46 scanf("%d/%d/%d", &month, &day, &year);
47 printf("The sum of day is : %dn", day_of_year(month, day, year));
48
49 return 0;
50 }
结果演示:
最后
以上就是光亮水蜜桃为你收集整理的day_of_year另解的全部内容,希望文章能够帮你解决day_of_year另解所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复