please finish the todo part. use efficient algorithm. don't make the time complexity too large so that the program will crash.
/* * File: calendar.cpp * ------------------ * This file implements the calendar.h interface */
#include #include "P3calendar.h" #include "error.h" #include "strlib.h" using namespace std;
/* * Implementation notes: monthToString * ----------------------------------- * The monthToString function must return some value if the month does not * match any of the enumeration constants. Here, as in the Direction * type, the function returns ???. */ namespace P3 { string monthToString(Month month) { switch (month) { case JANUARY: return "JANUARY"; case FEBRUARY: return "FEBRUARY"; case MARCH: return "MARCH"; case APRIL: return "APRIL"; case MAY: return "MAY"; case JUNE: return "JUNE"; case JULY: return "JULY"; case AUGUST: return "AUGUST"; case SEPTEMBER: return "SEPTEMBER"; case OCTOBER: return "OCTOBER"; case NOVEMBER: return "NOVEMBER"; case DECEMBER: return "DECEMBER"; default: return "???"; } }
Month operator++(Month & month, int) { Month old = month; month = Month(month + 1); return old; }
/* * Implementation notes: Constructors * ---------------------------------- * There are three constructors for the Date class. The default * constructor creates a Date with a zero internal value that must * be assigned a new value before it is used. The others initialize * the date from the arguments by calling the private initDate method. */
Date::Date() { /* Empty */ }
Date::Date(int day, Month month, int year) { initDate(day, month, year); }
Date::Date(Month month, int day, int year) { initDate(day, month, year); }
/* * Implementation notes: getDay, getMonth * -------------------------------------- * In this implementation of the Date class, the day and the month are * not stored explicitly but must instead be computed from the dayInYear * field. */
int Date::getDay() { Month month = JANUARY; int day = dayInYear; while (day > daysInMonth(month, year)) { day -= daysInMonth(month, year); month++; } return day; }
/* * Method: getMonth * Usage: Month month = date.getMonth(); * ------------------------------------- * Returns the month. */
Month Date::getMonth() { Month month = JANUARY; int day = dayInYear; while (day > daysInMonth(month, year)) { day -= daysInMonth(month, year); month++; } return month; }
int Date::getYear() { return year; }
/* * Implementation notes: toString * ------------------------------ * The toString method uses the getters to perform the translation into * day/month/year values. */
string Date::toString() { string day = to_string(getDay()); string month = to_string(getMonth()); string year = to_string(getYear()); return (day + "/" + month + "/" + year); }
string Date::capitalize(string str) { if (str == "") return ""; return toUpperCase(str.substr(0, 1)) + toLowerCase(str.substr(1)); }
void Date::initDate(int day, Month month, int yyyy) { if (day daysInMonth(month, yyyy)) { error("Specified date does not exist in the calendar"); } dayInYear = day; for (Month m = JANUARY; m
ostream & operator
Date operator+(Date date, int delta) { //TO DO; }
Date operator-(Date date, int delta) { return date + -delta; }
int operator-(Date d1, Date d2) { //TO DO; }
Date & operator+=(Date & date, int delta) { return date = date + delta; }
Date & operator-=(Date & date, int delta) { return date = date - delta; }
Date operator++(Date & date) { return date += 1; }
Date operator++(Date & date, int) { //TO DO; }
Date operator--(Date & date) { return date -= 1; }
Date operator--(Date & date, int) { Date old = date; date -= 1; return old; }
bool operator==(Date d1, Date d2) { return d1 - d2 == 0; }
bool operator!=(Date d1, Date d2) { //TO DO; }
bool operator
bool operator
bool operator>(Date d1, Date d2) { return d1 - d2 > 0; }
bool operator>=(Date d1, Date d2) { return d1 - d2 >= 0; }
/* * Implementation notes: daysInMonth * --------------------------------- * This function is a reasonably literal translation of the old rhyme: * * Thirty days has September * April, June, and November * All the rest have 31 * Excepting February alone * Which has 28 in fine * And each leap year 29 */
int daysInMonth(Month month, int year) { if ((month == SEPTEMBER) || (month == APRIL) || (month == JUNE) || (month == NOVEMBER)) { return 30; } else if(month == FEBRUARY) { if(isLeapYear(year)) return 29; else return 28; } else { return 31; } }
/* * Implementation notes: isLeapYear * -------------------------------- * This function simply encodes the rule for determining leap years: * a leap year is any year divisible by 4, except for years ending in 00, * in which case the year must be divisible by 400. */
bool isLeapYear(int year) { return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); }
}
/* * File: TestCalendar.cpp * ---------------------- * This file tests the calendar.h interface. */
#include #include "P3calendar.h" using namespace std; using namespace P3; int P3calendar() { Date moonLanding(20, JULY, 1969); Date kennedyAssassination(NOVEMBER, 22, 1963); Date newYearsEve(DECEMBER, 31, 2011); Date inaugurationDay(21, JANUARY, 2013); Date electionDay(6, NOVEMBER, 2012); cout kennedyAssassination = " kennedyAssassination) int main(){ cout 1 /* Gul 6 7 8 #include #include "P3calendar.h" #include "error.h" #include "strlib.h" using namespace std; + * Implementation notes: monthToString * 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 BO 31 32 33 34 35 36 37 38 39 10 41 12 * The monthToString function must return some value if the month does not * match any of the enumeration constants. Here, as in the Direction * type, the function returns ???. +/ namespace P3 { string monthToString (Month month) { switch (month) { case JANUARY: return "JANUARY"; case FEBRUARY: return "FEBRUARY"; case MARCH: return "MARCH"; case APRIL: return "APRIL"; case MAY: return "MAY"; case JUNE: return "JUNE"; case JULY: return "JULY"; case AUGUST: return "AUGUST"; case SEPTEMBER: return "SEPTEMBER"; case OCTOBER: return "OCTOBER"; case NOVEMBER: return "NOVEMBER"; case DECEMBER: return "DECEMBER"; default: return "???"; } } Month operator++ (Month & month, int) { Month old = month; month = Month (month + 1); return old; } 43 14 4.4 /* * Implementation notes: Constructors 45 46 47 48 49 50 51 52 53 54 55 56 * There are three constructors for the Date class. The default * constructor creates a Date with a zero internal value that must * be assigned a new value before it is used. The others initialize * the date from the arguments by calling the private initDate method. */ Date::Date() { /* Empty */ } Date::Date(int day, Month month, int year) { initDate (day, month, year); } Date::Date (Month month, int day, int year) { initDate(day, month, year); } /* * Implementation notes: getDay, getMonth 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 * In this implementation of the Date class, the day and the month are * not stored explicitly but must instead be computed from the dayInYear * field. int Date::getDay() { Month month = JANUARY; int day = dayInYear; while (day > daysInMonth (month, year)) { day -- daysInMonth (month, year); month++; } return day; } /* * Method: getMonth * Usage: Month month = date.getMonth(); 83 84 85 86 87 88 89 90 91 92 93 * Returns the month. */ 94 Month Date::getMonth() { Month month = JANUARY; int day = dayInYear; while (day > daysInMonth (month, year)) { day -= daysInMonth (month, year); month++; } return month; } > int Date::getYear() { return year; } /* * Implementation notes: toString 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 * The toString method uses the getters to perform the translation into * day/month/year values. */ string Date:: toString() { string day to_string(getDay(); string month = to_string(getMonth(); string year = to_string(getYear(); return (day + "/" + month + "/" + year); } string Date::capitalize(string str) { if (str == "") return ""; return toUpperCase(str.substr(0, 1)) + to LowerCase(str.substr(1)); } 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 void Date::initDate(int day, Month month, int yyyy) { if (day daysInMonth (month, yyyy)) { error("Specified date does not exist in the calendar"); } dayInYear = day; for (Month m = JANUARY; m daysInMonth(mon, yyyy)) { while(days > daysInMonth (mon, yyyy)) { days daysInMonth (mon, yyyy); mon++; } if(mon>12) { yyyy++; mon=JANUARY; } } return Date (days,mon, yyyy); } Date operator-(Date date, int delta) { return date + -delta; } 164 165 Aunu 1661 int operator-(Date di, Date d2) { //TO DO; } 167 168 169 Date & operator+=(Date & date, int delta) { return date = date + delta; } 170 171 172 173 174 175 176 Date & operator-=(Date & date, int delta) { return date = date - delta; } Date operator++ (Date & date) { return date += 1; } Date operator++(Date & date, int) { Date old = date; date += 1; return old; } 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 Date operator--(Date & date) { return date -- 1; } Date operator--(Date & date, int) { Date old = date; date -= 1; return old; } 1981 bool operator==(Date di, Date d2) { return di-d2==0; } 199 200 201 202 203 bool operator != (Date di, Date d2) { return (di.getDay()!=d2.getDayO1|d.getMonth()!=d2.getMonth ||d1.getYear()!=d2.getYear(); } bool operator Date di, Date d2) { return dl - d2 > 0; } 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 bool operator>=(Date di, Date d2) { return di - d2 >= 0; } /* * Implementation notes: daysInMonth * This function is a reasonably literal translation of the old rhyme: + + Thirty days has September April, June, and November All the rest have 31 Excepting February alone which has 28 in fine And each leap year 29 * */ APRIL) || (month JUNE) || (month == NOVEMBER) 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 int daysInMonth (Month month, int year) { if ((month == SEPTEMBER) || (month == { return 30; } else if(month == FEBRUARY) { if(isLeapYear (year)) return 29; else return 28; } else { return 31; } } /* * Implementation notes: isLeapYear + * This function simply encodes the rule for determining leap years: * a leap year is any year divisible by 4, except for years ending in 00, * in which case the year must be divisible by 400. */ bool isLeapYear(int year) { return ((year % 4 == 0) && (year % 100 != 0)) IT (year % 400 == 0); } 261 262 263 264 265 266 } O The expression d1 - d2, which returns how many days separate dl and d2 Using the direction.h interface as an example, design and implement a calendar.h interface that exports the Month type from Chapter 1, along with the functions daysInMonth and isLeap Year, which also appear in that chapter. Your interface should also export a month ToString function that returns the constant name for a value of type Month. Test your implementation by writing a main program that asks the user to enter a year and then writes out the number of days in each month of that year, as in the following sample run: TestCalendar Enter a year: 2012 JANUARY has 31 days. FEBRUARY has 29 days. MARCH has 31 days. APRIL has 30 days. MAY has 31 days. JUNE has 30 days. JULY has 31 days. AUGUST has 31 days. SEPTEMBER has 30 days. OCTOBER has 31 days. NOVEMBER has 30 days. DECEMBER has 31 days. Extend the calendar.h interface still further by adding overloaded versions of the following operators: The insertion operator , and >= The expression date + n, which returns the date n days after date The expression date - n, which returns the date n days before date The expression d1 - d2, which returns how many days separate dl and d2 The shorthand assignment operators += and - with an integer on the right The ++ and - operators in both their prefix and suffix form The Rational class presented in the text defines the operators +, -, *, / but needs several other operators for completeness, including the following: The relational operators ==, !=, , and >= The shorthand assignment operators +=, -=, *=, and /= The ++ and - operators in both their prefix and suffix form. Add these operators to the interface and implementation. Requirments & Hints: Please fill in the TODO part of operator+, -, ++ and != in P3calendar.cpp