fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. bool IsRange(std::size_t value, std::size_t min, std::size_t max)
  5. {
  6. return value >= min && value <= max;
  7. }
  8.  
  9. bool isLeapYear(std::size_t year)
  10. {
  11. return year % 4 == 0 && year % 100 || year % 400 == 0;
  12. }
  13.  
  14. bool CheckDate(std::size_t day, std::size_t month, std::size_t year)
  15. {
  16. const int months[] = {1,3,5,7,8,10,12,4,6,9,11};
  17. const int MONTH_WITH_31 = 7;
  18.  
  19. if(month < 1 || month > 12)
  20. return false;
  21. if(month == 2)
  22. return IsRange(day, 1, 28 + isLeapYear(year));
  23. return IsRange(day, 1, 30 + (std::find(months, months + MONTH_WITH_31, month) != std::end(months)));
  24. }
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28. std::cout << std::boolalpha << CheckDate(28, 2, 2015) << std::endl
  29. << CheckDate(29, 2, 2015) << std::endl;
  30. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
true
false