-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateValidation.c
executable file
·157 lines (151 loc) · 2.58 KB
/
dateValidation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <math.h>
int getLength(int n)
{
int i = 0;
while (n != 0)
{
i++;
n /= 10;
}
return i;
}
int getOnes(int n)
{
return n - (n / 10) * 10;
}
int isLargeMonth(int mth)
{
int i = 0;
switch (mth)
{
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
i = 1;
break;
case 4:
case 6:
case 9:
case 11:
// day += 30;
break;
}
return i;
}
int isLeap(int yr)
{
int leap = 0;
if (yr % 100 == 0)
{
if (yr % 400 == 0)
leap = 1;
}
else
{
if (yr % 4 == 0)
leap = 1;
}
return leap;
}
int main()
{
int buff, length, yr = 0, mth = 0, day = 0, invalid = 0;
scanf("%d", &buff);
length = getLength(buff);
for (int i = 1; i <= 2; i++)
{
day += getOnes(buff) * pow(10, i - 1);
buff /= 10;
}
for (int i = 1; i <= 2; i++)
{
mth += getOnes(buff) * pow(10, i - 1);
buff /= 10;
}
for (int i = 1; i <= length - 4; i++)
{
yr += getOnes(buff) * pow(10, i - 1);
buff /= 10;
}
// determine whether mth is over 2
switch (mth)
{
case 1:
if (day > 31)
{
invalid = 1;
}
break;
case 2:
// determine whether yr is leap yr
if (isLeap(yr))
{
if (day > 29)
{
invalid = 1;
}
}
else
{
if (day > 28)
{
invalid = 1;
}
}
day += 31;
break;
case 3 ... 12:
// simulate
if (isLargeMonth)
{
if (day > 31)
{
invalid = 1;
}
}
else
{
if (day > 30)
{
invalid = 1;
}
}
while (mth > 2)
{
if (isLargeMonth(mth))
{
day += 31;
}
else
{
day += 30;
}
mth--;
}
if (isLeap(yr))
{
day += 29;
}
else
{
day += 28;
}
break;
default:
invalid = 1;
break;
}
if (invalid == 1)
{
printf("Invalid");
}
else
{
printf("%d", day);
}
return 0;
}