首页 > 代码库 > 判断日期是否是合法的。

判断日期是否是合法的。

  1 #include <iostream>
  2 using std::cin;
  3 using std::cout;
  4 using std::endl;
  5 int main()
  6 {
  7     int year;
  8     int month;
  9     int day;
 10     char op = -;
 11     cin >> year >> op >> month >> op >> day;
 12     if (year % 100 == 0 && year % 400 == 0)
 13         {
 14             if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
 15             {
 16                 if (day >= 1 && day <= 31)
 17                 {
 18                     cout << "YES";
 19                 }
 20                 else
 21                 {
 22                     cout << "NO";
 23                 }
 24             }
 25             else if (month == 4 || month == 6 || month == 9 || month == 11)
 26             {
 27                 if (day >= 1 && day <= 30)
 28                 {
 29                     cout << "YES";
 30                 }
 31                 else
 32                 {
 33                     cout << "NO";
 34                 }
 35             }
 36             else if(month==2)
 37             {
 38                 if (day >= 1 && day <= 29)
 39                 {
 40                     cout << "YES";
 41                 }
 42                 else
 43                 {
 44                     cout << "NO";
 45                 }
 46             }
 47             else
 48             {
 49                 cout << "NO";
 50             }
 51         }
 52 
 53     else if (year % 100 != 0 && year % 4 == 0)
 54     {
 55         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
 56         {
 57             if (day >= 1 && day <= 31)
 58             {
 59                 cout << "YES";
 60             }
 61             else
 62             {
 63                 cout << "NO";
 64             }
 65         }
 66         else if (month == 4 || month == 6 || month == 9 || month == 11)
 67         {
 68             if (day >= 1 && day <= 30)
 69             {
 70                 cout << "YES";
 71             }
 72             else
 73             {
 74                 cout << "NO";
 75             }
 76         }
 77         else if(month == 2)
 78         {
 79             if (day >= 1 && day <= 29)
 80             {
 81                 cout << "YES";
 82             }
 83             else
 84             {
 85                 cout << "NO";
 86             }
 87         }
 88 
 89         else
 90         {
 91             cout << "NO";
 92         }
 93     }
 94 
 95     else 
 96     {
 97         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
 98         {
 99             if (day >= 1 && day <= 31)
100             {
101                 cout << "YES";
102             }
103             else
104             {
105                 cout << "NO";
106             }
107         }
108 
109         else if (month == 4 || month == 6 || month == 9 || month == 11)
110         {
111             if (day >= 1 && day <= 30)
112             {
113                 cout << "YES";
114             }
115             else
116             {
117                 cout << "NO";
118             }
119         }
120 
121         else if(month == 2)
122         {
123             if (day >= 1 && day <= 28)
124             {
125                 cout << "YES";
126             }
127             else
128             {
129                 cout << "NO";
130             }
131         }
132         else
133         {
134             cout << "NO";
135         }
136     }
137 
138     return 0;
139 }

 

判断日期是否是合法的。