Guest User

Untitled

a guest
Jan 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const string DAYS = "UMTWRFS";
  8.  
  9. class Month
  10. {
  11. private:
  12.     int numDays;
  13.     int startDOW;
  14. public:
  15.     Month(int days, char day1){this->numDays = days; this->startDOW = string("UMTWRFS").find(day1);};
  16.     friend ostream & operator<<(ostream & out, Month m){
  17.         cout << "Su Mo Tu We Th Fr Sa" << endl;
  18.         int prints = 7;
  19.         for (int i = 0;i < m.startDOW;i++, prints++) cout << "   ";
  20.         for (int day = 1;day <= m.numDays; day++, prints++){
  21.             if (!(prints % 7)) cout << endl;
  22.             cout << setw(2) << day;
  23.             cout << ' ';
  24.         }
  25.     }
  26. };
  27.  
  28. int main()
  29. {
  30.     int year, month;
  31.     char startDay;
  32.     do {
  33.         cout << "Please enter the year: ";
  34.         cin >> year;
  35.     } while ((year < 1) || (year > 9999));
  36.     do {
  37.         cout << "Please enter the month (1-12): ";
  38.         cin >> month;
  39.     } while ((month < 1) || (month > 12));
  40.     do {
  41.         cout << "Please enter the day of week that the year begins with (U,M,T,W,R,F,S): ";
  42.         cin >> startDay;
  43.     } while (DAYS.find(startDay) == string::npos);
  44.     int lengths[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  45.     int totalDays = 0;
  46.     for (int i = 0; i < month; i++) totalDays += lengths[i];
  47.     if ((month > 2) && (!(year % 4) || (!(year % 100) && !(year % 400)))) totalDays++; // leap year, and yes, there probably are too many parens
  48.     Month cal(lengths[month], ((DAYS.find(startDay)+totalDays) % 7)+1); // not 100% sure abt the math for the second arg
  49.     cout << "\n\n\n" << cal << endl; // original string to make it look nice.
  50.     return 0;
  51. }
Add Comment
Please, Sign In to add comment