C语言日期计算器

时间:2024-01-29 07:54:37

记录下码子

 1 # define _CRT_SECURE_NO_WARNINGS
 2 # include <stdio.h>
 3 # include <stdlib.h>
 4 
 5 int days(int month)
 6 {
 7     int day; 
 8     switch (month)
 9     {
10         case 1:
11             day = 31; //1月天数
12             break;
13         case 2:
14             day = 29+31; //1+2月天数
15             break;
16         case 3:
17             day = 31+ 29 + 31; //1+2+3月天数
18             break;
19         case 4:
20             day = 30+ 31 + 29 + 31;
21             break;
22         case 5:
23             day = 31+ 30 + 31 + 29 + 31;
24             break;
25         case 6:
26             day = 30+ 31 + 30 + 31 + 29 + 31;
27             break;
28         case 7:
29             day = 31+30+ 31 + 30 + 31 + 29 + 31;
30             break;
31         case 8:
32             day = 31+ 31 + 30 + 31 + 30 + 31 + 29 + 31;
33             break;
34         case 9:
35             day = 30+31+ 31 + 30 + 31 + 30 + 31 + 29 + 31;
36             break;
37         case 10:
38             day =31+ 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31;
39             break;
40         case 11:
41             day = 30+31+ 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31;
42             break;
43         case 12:
44             day = 31+ 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; //12个月的天数
45             break;
46     }
47     return day;
48 }
49 
50 
51 
52 
53 int main()
54 {
55     int month, day,days1;
56     int month_2, day_2,days2;
57     int result;
58     printf("请输入一个日期\n");
59     scanf("%d %d", &month, &day);
60 
61     printf("请输入第二个日期\n");
62     scanf("%d %d", &month_2, &day_2);
63 
64     days1 = days(month) + day; //计算第一个日期的天数
65     days2 = days(month_2) + day_2; //计算第二个日期的天数
66     //printf("%d %d", days1, days2);
67 
68     //大的天数减去小的天数
69     if (days1 > days2)
70     {
71         result = days1 - days2;
72     }
73     else 
74     {
75         result = days2 - days1;
76     }
77     printf("\n");
78     printf("%d月%d日和%d月%d日间隔%d天\n", month, day, month_2, day_2,result);
79     system("pause");
80     return 0;
81 }

运行结果: