Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <limits> //numeric_limits
  2. #include <iostream>
  3. //https://codereview.stackexchange.com/questions/67636/is-it-friday-yet
  4. //dayOfWeek.cpp
  5.  
  6. enum Months { January=1, February, March, April, May, June, July, August, September, October, November, December};
  7.  
  8. const std::string weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  9.  
  10. /**
  11.  * Makes sure data isn't malicious, and signals user to re-enter proper data if invalid
  12.  * getSanitizedNum
  13.  */
  14. int getInt()
  15. {
  16.     int input = 0;
  17.     while(!(std::cin>>input))
  18.     {
  19.         // clear the error flag that was set so that future I/O operations will work correctly
  20.         std::cin.clear();
  21.         // skips to the next newline
  22.         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  23.         std::cout << "\n\tInvalid input.\n\tPlease enter a positive number: ";
  24.     }
  25.     return input;
  26. }
  27.  
  28. /**
  29.  * Safetly grabs and returns a lowercase version of the character (if the lowercase exists)
  30.  * getSanitizedChar
  31.  */
  32. char32_t getChar()
  33. {
  34.     // absorb newline character (if existant) from previous input
  35.     if('\n' == std::cin.peek()) std::cin.ignore();
  36.     return std::tolower(std::cin.get());
  37. }
  38.  
  39. int getCenturyValue(int year)
  40. {
  41.     return (2 * (3 - div(year / 100, 4).rem));
  42. }
  43.  
  44. int getYearValue(int year)
  45. {
  46.     int mod = year % 100;
  47.     return (mod + div(mod, 4).quot);
  48. }
  49.  
  50. int getMonthValue(int month, int year)
  51. {
  52.     const static int NON_LEAP_VALS[] = { 0xDEAD, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 },
  53.                          LEAP_VALS[] = { 0xDEAD, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
  54.  
  55.   return (__isleap(year) ? LEAP_VALS[month] : NON_LEAP_VALS[month]);
  56. }
  57. /*
  58. int getMonthValue(int month, int year)
  59. {
  60.     switch (month)
  61.     {
  62.         case January:
  63.             if (__isleap(year)) return 6;
  64.         case October:
  65.             return 0;
  66.         case May:
  67.             return 1;
  68.         case August:
  69.             return 2;
  70.         case February:
  71.             if (__isleap(year)) return 2;
  72.         case March:
  73.         case November:
  74.             return 3;
  75.         case June:
  76.             return 4;
  77.         case September:
  78.         case December:
  79.             return 5;
  80.         case April:
  81.         case July:
  82.             return 6;
  83.         default:
  84.             return -1;
  85.     }
  86. }
  87. */
  88. int dayOfWeek(int day, int month, int year)
  89. {
  90.     return div(day + getMonthValue(month, year) + getYearValue(year) + getCenturyValue(year), 7).rem;
  91. }
  92.  
  93. void getInput(int &day, int &month, int &year)
  94. {
  95.  int meses[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  96.  do{
  97.     do{
  98.        std::cout << "\n\tEnter the day (1-31): ";
  99.        day = getInt();
  100.       }while(day < 1 || day > 31);
  101.      
  102.     do{  
  103.        std::cout << "\n\tEnter the month (1-12): ";
  104.        month = getInt();
  105.       }while(month < 1 || month > 12);
  106.      
  107.     do{  
  108.        std::cout << "\n\tEnter the year: ";
  109.        year = getInt();
  110.       }while(year < 1100);
  111.      
  112.       if(__isleap(year))meses[1] = 29;
  113.        
  114.       if(day > meses[month-1])std::cout<<"\n\tO mes "<<month<<" do ano de "<<year<<" nao tem "<<day<<" dias!!!\n\n";
  115.      
  116.   }while(day > meses[month-1]);
  117. }
  118.  
  119. int main()
  120. {
  121.  int day = 0;
  122.  int month = 0;
  123.  int year = 0;
  124.  
  125.  do{
  126.     getInput(day, month, year);
  127.     std::cout << "\n\tThe day of the week is " << weekDays[dayOfWeek(day, month, year)] << std::endl;
  128.    
  129.     std::cout << "\n\tRun the program again (y/N): ";  // signify n as default with capital letter
  130.    
  131.    }while('y' == getChar());
  132.    
  133.   return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement