Advertisement
Guest User

calendar assignment hell

a guest
Nov 3rd, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. /*
  2. * prompt for month
  3. */
  4. int getMonth()
  5. {
  6.    int month;
  7.    cout << "Enter a month number: ";
  8.    cin >> month;
  9.    if (month < 1 || month > 12)
  10.    {
  11.       cout << "Month must be between 1 and 12." << endl;
  12.       cin >> month;
  13.    }
  14.    return month;
  15. }
  16. /*
  17. * prompt for year
  18. */
  19. int getYear()
  20. {
  21.    int year;
  22.    cout << "Enter year: ";
  23.    cin >> year;
  24.    if (year < 1753)
  25.    {
  26.       cout << "Year must be 1753 or later."<< endl;
  27.       cin >> year;
  28.    }
  29.    return year;
  30. }
  31. /*
  32. * to find it it's a leap year
  33. */
  34. bool isLeapYear(int year)
  35. {
  36.    if (year % 4 == 0 && year % 100 == 0 || year % 400 == 0)
  37.    {
  38.       return true;
  39.    }
  40.    else
  41.    {
  42.       return false;
  43.    }
  44. }
  45. /*
  46. * to set the days in the month
  47. */
  48. int daysInMonth(int month, int year)
  49. {
  50.    int numDays = 0;
  51.    if (month == 1 || month == 3 || month == 5 || month == 7
  52.        || month == 8 || month == 10 || month == 12)
  53.    {
  54.       numDays = 31;
  55.       return numDays;
  56.    }
  57.    else if (month == 2)
  58.    {
  59.       if (isLeapYear(year))
  60.       {
  61.          numDays = 29;
  62.       }
  63.       else
  64.       {
  65.          numDays = 28;
  66.       }
  67.       return numDays;
  68.    }
  69.    else
  70.    {
  71.       numDays = 30;
  72.       return numDays;
  73.    }
  74.    return numDays;
  75. }
  76. /*
  77. * to calculate the year (obviously)
  78. */
  79. int calcYears(int years)
  80. {
  81.    int year;
  82.    years = year - 1753;
  83.  
  84.    return years;
  85. }
  86. /*
  87. * to calculate the offset
  88. */
  89. int computeOffset(int month, int year)
  90. {
  91.    int numDays;
  92.    int years;
  93.    int months = calcYears(years) * 12 + month - 1;
  94.  
  95.    for (int i = 0; i > months; i++)
  96.    {
  97.       numDays += daysInMonth(i % 12, i / 12);
  98.    }
  99.  
  100.    int offset = numDays % 7;
  101.    return offset;
  102. }
  103. /*
  104. * to display the month and year
  105. */
  106. int header(int month, int year)
  107. {
  108.    if (month == 1)
  109.    {
  110.       cout << "January, " << year;
  111. else if (month == 2)
  112.    {
  113.       cout << "Febuary, " << year;
  114.    }
  115.    else if (month == 3)
  116.    {
  117.       cout << "March, " << year;
  118.    }
  119.    else if (month == 4)
  120.    {
  121.       cout << "April, " << year;
  122.    }
  123.    else if (month == 5)
  124.    {
  125.       cout << "May, " << year;
  126.    }
  127.    else if (month == 6)
  128.    {
  129.       cout << "June, " << year;
  130.    }
  131.    else if (month == 7)
  132.    {
  133.       cout << "July, " << year;
  134.    }
  135.    else if (month == 8)
  136.    {
  137.       cout << "August, " << year;
  138.    }
  139.    else if (month == 9)
  140.    {
  141.       cout << "September, " << year;
  142.    }
  143.    else if (month == 10)
  144.    {
  145.       cout << "October, " << year;
  146.    }
  147.    else if (month == 11)
  148.    {
  149.       cout << "November, " << year;
  150.    }
  151.    else if (month == 12)
  152.    {
  153.       cout << "December, " << year;
  154.    }
  155.    cout << endl;
  156.  
  157.    return 0;
  158. }
  159. /*
  160. * to display the calendar
  161. */
  162. void displayCalendar(int numDays, int offset)
  163. {
  164.    cout << endl;
  165.    int month, year;
  166.    header(month, year);
  167.    cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";
  168.  
  169.    if (offset != 6)
  170.    {   
  171.     for (int i = 0; i <= offset; i++)
  172.       {
  173.          cout << "        ";
  174.       }
  175.    }
  176.    for (int day = 1; day < numDays; day++)
  177.    {
  178.       if (!(offset == 6 && day == 1))
  179.       {
  180.          if ((offset + day) % 7 == 0)
  181.          {
  182.             cout << endl;
  183.          }
  184.       }
  185.       cout << setw(4) << day;
  186.    }
  187.    cout << endl;
  188. }
  189. /*                                                                                      
  190.  * executing the program                                                                
  191.  */
  192. int main()
  193. {
  194.    int numDays = 0;
  195.    int offset = 0;
  196.    int month = getMonth();
  197.    int year = getYear();
  198.    offset = computeOffset(month, year);
  199.    numDays = daysInMonth(month, year);
  200.  
  201.    displayCalendar(offset, numDays);
  202.    return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement