Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // Forward declarations for functions we'll use later (these could have easily just been put in
  8. // this order at the top of this source file):
  9. bool is_leap_year(const int year);
  10. string month_name(const int month);
  11. int days_in_month(const bool is_leap_year, const int month);
  12. void print_whitespace(const size_t amount);
  13. void print_calendar(const bool is_leap_year, const int calendar[12][31]);
  14. void generate_calendar(const bool is_leap_year, const int start_day, int calendar[12][31]);
  15.  
  16. int main(int /*argc*/, char* /*argv*/[])
  17. {
  18. int year = 0;
  19. int day = 0;
  20. bool day_valid = false;
  21.  
  22. // Get user input for year and validate it.
  23. while(!year)
  24. {
  25. cout << "Enter the year: ";
  26. cin >> year;
  27. if(year < 0 || year > 9999)
  28. {
  29. cout << "That's not a valid year!" << endl;
  30. year = 0;
  31. }
  32. }
  33.  
  34. // Get user input for day and validate it.
  35. while(!day_valid)
  36. {
  37. cout << "Enter the day of the week of January 1st, using 0 through 6" << endl
  38. << "(eg. 0 = Sun, 1 = Mon, 2 = Tue, 3 = Wed, 4 = Thu, 5 = Fri, 6 = Sat): ";
  39. cin >> day;
  40. if(day < 0 || day > 6)
  41. cout << "That's not a valid day! Must be between 0 and 6." << endl;
  42. else
  43. day_valid = true;
  44. }
  45.  
  46. // The assignment seems to require this.
  47. const bool leap_year = is_leap_year(year);
  48. if(leap_year)
  49. cout << year << " is a leap year." << endl
  50. << "There are 29 days in February." << endl;
  51. else
  52. cout << year << " is not a leap year." << endl
  53. << "There are 28 days in February." << endl;
  54.  
  55. // Generate our calendar.
  56. int calendar[12][31];
  57. generate_calendar(leap_year, day, calendar);
  58. print_calendar(leap_year, calendar);
  59.  
  60. return EXIT_SUCCESS;
  61. }
  62.  
  63. bool is_leap_year(const int year)
  64. {
  65. // A year is a leap year if it is divisible by 4 unless it is both divisible by 100 and not divisible by 400.
  66. if(year%4==0)
  67. {
  68. if(year%100==0 && year%400!=0)
  69. return false;
  70. return true;
  71. }
  72. return false;
  73. }
  74.  
  75. string month_name(const int month)
  76. {
  77. switch(month)
  78. {
  79. case 0:
  80. return "January";
  81. case 1:
  82. return "February";
  83. case 2:
  84. return "March";
  85. case 3:
  86. return "April";
  87. case 4:
  88. return "May";
  89. case 5:
  90. return "June";
  91. case 6:
  92. return "July";
  93. case 7:
  94. return "August";
  95. case 8:
  96. return "September";
  97. case 9:
  98. return "October";
  99. case 10:
  100. return "November";
  101. case 11:
  102. return "December";
  103. }
  104. return "WTF"; // I usually use a NODEFAULT() macro. This code will never be executed.
  105. }
  106.  
  107. int days_in_month(const bool is_leap_year, const int month)
  108. {
  109. int days_count = 0;
  110. switch(month)
  111. {
  112. // 30 days has september, april, june, and november. all the rest have 31, except for
  113. // february, 28! Or friggin 29 on a leap year.
  114. case 8: case 3: case 5: case 10:
  115. days_count = 30;
  116. break;
  117. case 1:
  118. if(is_leap_year)
  119. days_count = 29;
  120. else
  121. days_count = 28;
  122. break;
  123. default:
  124. days_count = 31;
  125. }
  126. return days_count;
  127. }
  128.  
  129. void print_whitespace(const size_t amount)
  130. {
  131. // Printing spaces for formatting becomes a common thing we do, so we want to put it in a
  132. // function that handles it.
  133. for(size_t i=0;i<amount;++i)
  134. cout << " ";
  135. }
  136.  
  137. void print_calendar(const bool is_leap_year, const int calendar[12][31])
  138. {
  139.  
  140. for(int month = 0; month < 12; ++month)
  141. {
  142. // Print the calendar header. Note that the whole calendar, including the '+' signs, is
  143. // TWENTY FOUR CHARACTERS LONG.
  144. cout << "+----------------------+" << endl
  145. << "|";
  146.  
  147. // We want to make the month name nicely centered in the calendar month heading. I'd usually
  148. // use setw here like we do below for the days, but that would require iostreams to have
  149. // a "center" (similar to how it has a left and right), but it doesn't. So we
  150. // need to do it ourselves.
  151. const string mn = month_name(month);
  152. const size_t left_ws = (22-mn.length())/2 + mn.length()%2;
  153. const size_t right_ws = (22-mn.length())/2;
  154. print_whitespace(left_ws);
  155. cout << mn;
  156. print_whitespace(right_ws);
  157. cout << "|" << endl;
  158.  
  159. cout << "+----------------------+" << endl
  160. << "| S M T W T F S |" << endl
  161. << "+----------------------+" << endl
  162. << "|";
  163.  
  164. // The first line of the calendar is a bit special - it has some empty days. Print each empty
  165. // day (they're three spaces each).
  166. int day = calendar[(size_t)month][(size_t)0];
  167. print_whitespace(day*3);
  168.  
  169. // Iterate through the rest of the days.
  170. const int days_count = days_in_month(is_leap_year, month);
  171. for(int date = 0; date < days_count; ++date)
  172. {
  173. cout << right << setw(3) << date+1;
  174. if(7 == ++day)
  175. {
  176. day = 0;
  177. cout << " |" << endl
  178. << "|";
  179. }
  180. }
  181.  
  182. // Similarly to the first line (but backwards), print the trailing whitespace.
  183. print_whitespace((7-day)*3);
  184.  
  185. cout << " |" << endl
  186. << "+----------------------+" << endl
  187. << endl;
  188. }
  189. }
  190.  
  191. void generate_calendar(const bool is_leap_year, const int start_day, int calendar[12][31])
  192. {
  193. int day = start_day;
  194. for(int month=0; month < 12; ++month)
  195. {
  196. const int days_count = days_in_month(is_leap_year, month);
  197.  
  198. for(int date=0; date < days_count; ++date)
  199. {
  200. if(7 == day)
  201. day = 0;
  202.  
  203. calendar[(size_t)month][(size_t)date] = day++;
  204.  
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement