Advertisement
Guest User

Untitled

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