Untitled

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include "Date.h" // Driver Program to check your implementation int main() { Date date1(3, 8, 2018); Date date2 = date1.DateAfter(13); Date date3 = date1.DateBefore(25); date1.setDay(25); date2.setMonth(12); date3.setYear(2015); cout << date1.numberOfDays() << endl; cout << date2.dayOfWeek() << endl; date3.printMonthCalendar(); return 0; }
#ifndef Date_h #define Date_h #include <iostream> #include <string> using namespace std; class Date { public: Date(int day, int month, int year); ~Date(); /*Get function for accessing private member*/ int getDay(); int getMonth(); int getYear(); /*Set function for updating private member*/ void setDay(int day); void setMonth(int month); void setYear(int year); /*A Function that returns the date after a given date For e.g- today is 8/3/2018 (Day/Month/Year) DateAfter(3) should return a date of 11/3/2018 */ Date DateAfter(int days); /*A Function that returns the date after a given date For e.g- today is 8/3/2018 (Day/Month/Year) DateBefore(3) should return a date of 5/3/2018 */ Date DateBefore(int days); /*A Function that returns the day of a date For e.g- if the date is 8/3/2018, then the day() function should return "Thursday" */ string dayOfWeek(); /* A Function to return the number of days in Current Month. Month Number Name Number of Days 0 January 31 1 February 28 (non-leap) / 29 (leap) 2 March 31 3 April 30 4 May 31 5 June 30 6 July 31 7 August 31 8 September 30 9 October 31 10 November 30 11 December 31*/ int numberOfDays(); /* Function to print a calendar of the current month. Just like what you see on a calendar. An example is like --------------March-------------- Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 */ void printMonthCalendar(); private: /*Current Date*/ int day; int month; int year; }; void round_up(Date &x); void round_down(Date &x); int Date::getDay(){ return day; } int Date::getMonth(){ return month; } int Date::getYear(){ return year; } void Date::setYear(int year){ Date::year=year; } void Date::setMonth(int month){ Date::month=month; } void Date::setDay(int day){ Date::day=day; } Date Date::DateAfter(int days){ day+=days; Date::Date n(Date::day,Date::month,Date::year); round_up(n); return n; } Date Date::DateBefore(int days){ day-=days; Date::Date n(day,month,year); round_down(n); return n; } string Date::dayOfWeek(){ if(month==1 || month==2){ month+=12; year--; } int Week=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7; switch(Week){ case 0: return "Monday"; case 1: return "Tuesday"; case 2: return "Wednesday"; case 3: return "Thursday"; case 4: return "Friday"; case 5: return "Saturday"; case 6: return "Sunday"; } return "error"; } void round_up(Date &x){ int day=x.getDay(); int month=x.getMonth(); int year=x.getYear(); int day_limit[12+1]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if( year%400==0 || (year%4==0 && !year%100==0 )) day_limit[2]=29; while( day>day_limit[month] ){ day-=day_limit[month]; month++; while(month>12){ month-=12; year++; if( year%400==0 || (year%4==0 && !year%100==0 )) day_limit[2]=29; else day_limit[2]=28; } } } void round_down(Date &x){ int day=x.getDay(); int month=x.getMonth(); int year=x.getYear(); int day_limit[12+1]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if( year%400==0 || (year%4==0 && !year%100==0 )) day_limit[2]=29; while(day<1){ day+=day_limit[month-1]; month--; if(month<1){ month+=12; year--; if( year%400==0 || (year%4==0 && !year%100==0 )) day_limit[2]=29; else day_limit[2]=28; } } } string name_of_month(int month){ switch(month){ case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10:return "October"; case 11:return "November"; case 12:return "December"; } return "error"; } int Date::numberOfDays(){ int day_limit[12+1]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if( year%400==0 || (year%4==0 && !year%100==0 )) day_limit[2]=29; return day_limit[getMonth()]; } void Date::printMonthCalendar(){ int month=getMonth(); cout<<"--------------"<<name_of_month(month)<<"--------------"<<endl; cout<<"Sun Mon Tue Wed Thu Fri Sat"<<endl; } #endif /* Date_h */
Editor Settings
Theme
Key bindings
Full width
Lines