{"id":2290,"date":"2015-09-12T13:48:17","date_gmt":"2015-09-12T13:48:17","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2290"},"modified":"2015-09-12T13:48:17","modified_gmt":"2015-09-12T13:48:17","slug":"generate-yearly-calendar-in-c","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/12\/generate-yearly-calendar-in-c\/","title":{"rendered":"Generate Yearly Calendar  in C++"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushCpp.js\"><\/script>\n<p>A pure C++ class to print the yearly calendar\u00a0 of a given year, on the console. Compiled in g++ under Linux. Should compile fine in VC++ with no or minimal changes as no OS specific code has been used.<\/p>\n<p>&nbsp;<br \/>\n<pre class=\"brush: cpp\">\/**\r\n * Class to generate a yearly calendar\r\n * Amit Sengupta, 2015 Truelogic.org\r\n * Using g++, Linux Mint 18\r\n *\/\r\n\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;iomanip&gt;\r\n\r\nusing namespace std;\r\n\r\nclass Cal {\r\n\r\n\tpublic:\r\n\t\tCal(void);\r\n\t\tvoid getYearly(int year);\r\n\r\n\tprotected:\r\n\t\tint mYear;\r\n\t\tstring mMonths[13];\r\n\t\tstring mWeekDays [8];\r\n\t\tint mDays [13];\r\n\t\tint firstDayOfMonth(int month, int year);\r\n\r\n\t\tstatic const int YEAR_ROW_WIDTH  = 35; \/\/ width of a row in yearly display\r\n\r\n};\r\n\r\n\/**\r\n * Constructor - initialize data \r\n *\r\n *\/\r\nCal::Cal(void) {\r\n\t\tmMonths[0] = &quot;&quot;;\t\/\/ month starts from 1\r\n\t\tmMonths[1] = &quot;January&quot;;\r\n\t\tmMonths[2] = &quot;February&quot;;\r\n\t\tmMonths[3] = &quot;March&quot;;\r\n\t\tmMonths[4] = &quot;April&quot;;\r\n\t\tmMonths[5] = &quot;May&quot;;\r\n\t\tmMonths[6] = &quot;June&quot;;\r\n\t\tmMonths[7] = &quot;July&quot;;\r\n\t\tmMonths[8] = &quot;August&quot;;\r\n\t\tmMonths[9] = &quot;September&quot;;\r\n\t\tmMonths[10] = &quot;October&quot;;\r\n\t\tmMonths[11] = &quot;November&quot;;\r\n\t\tmMonths[12] = &quot;December&quot;;\r\n\r\n\t\tmDays[0] =0;\r\n\t\tmDays[1] = 31;\r\n\t\tmDays[2] = 28;\r\n\t\tmDays[3] = 31;\r\n\t\tmDays[4] = 30;\r\n\t\tmDays[5] = 31;\r\n\t\tmDays[6] = 30;\r\n\t\tmDays[7] = 31;\r\n\t\tmDays[8] = 31;\r\n\t\tmDays[9] = 30;\r\n\t\tmDays[10] = 31;\r\n\t\tmDays[11] = 30;\r\n\t\tmDays[12] = 31;\r\n\r\n\t\tmWeekDays[0] = &quot;&quot;;\r\n\t\tmWeekDays[1] = &quot;Sun&quot;;\r\n\t\tmWeekDays[2] = &quot;Mon&quot;;\r\n\t\tmWeekDays[3] = &quot;Tue&quot;;\r\n\t\tmWeekDays[4] = &quot;Wed&quot;;\r\n\t\tmWeekDays[5] = &quot;Thu&quot;;\r\n\t\tmWeekDays[6] = &quot;Fri&quot;;\r\n\t\tmWeekDays[7] = &quot;Sat&quot;;\r\n\r\n\r\n}\r\n\r\n\/**\r\n * Using a standard c++ algorithm to find day of week for a date\r\n * Parameters:\r\n *  month - month number\r\n *  year - year\r\n * Returns:\r\n *\tdow = day of week, sun =0, mon=1 etc.\r\n *\/\r\nint Cal::firstDayOfMonth(int month, int year) {\r\n\tint dow = 0;\r\n\tint day = 1;\r\n\r\n    int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };\r\n    year -= month &lt; 3;\r\n    dow = ( year + year\/4 - year\/100 + year\/400 + t[month-1] + day) % 7;\r\n\r\n\treturn dow;\r\n}\r\n\r\n\/**\r\n * Print yearly calendar\r\n * Parameters:\r\n *  year - year to print for\r\n *\/\r\nvoid Cal::getYearly(int year) {\r\n\r\n\tmYear = year;\r\n\tif (mYear &lt; 1900 || mYear &gt; 2100) {\r\n\t\tstd::cout &lt;&lt; &quot;Year must be between 1900 and 2100&quot; &lt;&lt; endl;\r\n\t\treturn;\r\n\t}\r\n\r\n\t\/\/ check for leap year\r\n\tif ((mYear % 4 == 0 &amp;&amp; mYear % 100 != 0) || (mYear % 400 == 0))\r\n\t\tmDays[2] = 29;\r\n\r\n\tint leftPadding = 0;\r\n\tfor (int i =1; i &lt;=12; i ++) {\r\n\t\t\r\n\t\t\/\/ center month label\r\n\t\tleftPadding = (YEAR_ROW_WIDTH - mMonths[i].size()) \/ 2;\r\n\t\tfor (int x =0; x &lt; leftPadding; x++) {\r\n\t\t\tcout &lt;&lt; &quot;  &quot;;\r\n\t\t}\r\n\t\tcout &lt;&lt; mMonths[i] &lt;&lt; endl;\r\n\t\t\r\n\t\t\/\/print weekday labels\r\n\t\tfor (int j = 1; j &lt;= 7; j++) {\r\n\t\t\tcout &lt;&lt; setw(5) &lt;&lt; mWeekDays[j] &lt;&lt; setw(2) &lt;&lt; &quot;   &quot;;\r\n\t\t}\r\n\t\tcout &lt;&lt; endl;\r\n\r\n\t\t\/\/ print days\r\n\t\tint daysPrinted = 0;\r\n\t\tfor (int j = 1; j &lt;= mDays[i]; j++) {\r\n\t\t\t\/\/ move display head to the correct day of week for 1st\r\n\t\t\tif (j == 1) {\r\n\t\t\t\tint dow = firstDayOfMonth(i, mYear);\r\n\t\t\t\tfor (int spaces=0; spaces &lt; dow; spaces++) {\r\n\t\t\t\t\tcout &lt;&lt; setw(5) &lt;&lt; &quot; &quot; &lt;&lt; setw(2) &lt;&lt; &quot;   &quot;;\r\n\t\t\t\t\tdaysPrinted ++;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tcout &lt;&lt; setw(5) &lt;&lt; j &lt;&lt; setw(2) &lt;&lt; &quot;   &quot;;\r\n\t\t\tdaysPrinted ++;\r\n\t\t\tif (daysPrinted % 7 == 0) {\r\n\t\t\t\tcout &lt;&lt; endl;\r\n\t\t\t\tdaysPrinted = 0;\r\n\t\t\t}\r\n\t\t}\r\n\t\tcout &lt;&lt; endl &lt;&lt; setw(35) &lt;&lt; &quot;----------------------------------------------------------------&quot; &lt;&lt; endl;\r\n\t}\r\n}\r\n\r\nint main() {\r\n\t\r\n\tCal cal;\r\n\tcal.getYearly(2015);\r\n\r\n}\r\n\r\n<\/pre><br \/>\nSample output is given below:<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-12-191217.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2292\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-12-191217-620x544.png\" alt=\"Screenshot from 2015-09-12 19:12:17\" width=\"620\" height=\"544\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-12-191217-620x544.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-12-191217-300x263.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-12-191217.png 637w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>A pure C++ class to print the yearly calendar\u00a0 of a given year, on the console. Compiled in g++ under Linux. Should compile fine in <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/12\/generate-yearly-calendar-in-c\/\" title=\"Generate Yearly Calendar  in C++\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2294,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,298],"tags":[],"class_list":["post-2290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-linux"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2290"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2290\/revisions"}],"predecessor-version":[{"id":2293,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2290\/revisions\/2293"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2294"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}