Advertisement
Guest User

Untitled

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