Conval. h
# Ifndef conval_h
# Define conval_h
// Some constants used
Const int mo = 1;
Const int Tu = 2;
Const int we = 3;
Const int th = 4;
Const int Fr = 5;
Const int SA = 6;
Const int su = 0;
# Endif
Calendar. h
# Ifndef calendar_h
# Define calendar_h
# Include <iostream>
Using namespace STD;
Class clendar;
// It is mainly used to pass parameters between the calendar class and the user.
Class date
{
PRIVATE:
Int m_year;
Int m_month;
Int m_day;
Public:
Date (INT year = 0, int minth = 0, int day = 0 );
Friend class calendar;
};
// 1949------2949
Class calendar
{
PRIVATE:
Const static int s_year; // Save the start year 2007 for calculation.
Const static int s_weekday; // number of weeks on the first day of Calculation
// Check whether the date given by the user is valid
Bool testdate (date d) const;
// Determine whether a year is a leap year
Bool isleapeyear (INT year) const;
// The calculation date of the year less than s_year
Int lowyear (date d) const;
// The year is later than the s_year calculation date
Int upyear (date d) const;
// Print the date that the user wants to query
Void showcalendar (date D, int day) const;
// Print the calendar for one year. showcalendar calls this function.
Void showyear (date D, int day) const;
// Print the month of a year. showcalendar calls this function.
Void showmonth (date D, int day) const;
// Print the specific day of the user query
Void showday (date D, int day) const;
// Returns the number of days of the month to be passed.
Int monthdays (INT year, int month) const;
// Print a date of a month
Int printmonth (INT dnum, int day) const;
Public:
Calendar (){}
~ Calendar (){}
// Print the date that the user wants to print
Void printdate (date d) const;
};
# Endif
Calendarfun. cpp
# Include "date. H"
# Include "conval. H"
# Include <iostream>
Using namespace STD;
Const int calendar: s_weekday = Mo;
Const int calendar: s_year = 2007;
Date: Date (INT year, int month, int day ):
M_year (year), m_month (month), m_day (day)
{}
Bool calendar: testdate (date d) const
{
If (D. m_year <1949 | D. m_year> 2949)
Return false;
If (D. m_month <0 | D. m_month> 12)
Return false;
If (! D. m_month & D. m_day)
Return false;
If (! (D. m_month % 2 ))
{
If (D. m_month! = 2)
{
If (D. m_day> 30)
Return false;
}
Else
{
If (isleapeyear (D. m_year ))
{
If (D. m_day> 29)
Return false;
}
Else
If (D. m_day> 28)
Return false;
}
}
Return true;
}
Bool calendar: isleapeyear (INT year) const
{
Bool flag (false );
If (! (Year % 4 ))
{
If (Year %100)
Flag = true;
}
Else
If (! (Year % 400 ))
Flag = true;
Return flag;
}
Int calendar: lowyear (date d) const
{
Int year (D. m_year );
Int days (0 );
For (; year! = S_year; ++ year)
{
If (isleapeyear (year ))
Days ++ = 366;
Else
Days ++ = 365;
}
If (D. m_month)
{
For (INT month = 1; month! = D. m_month; ++ month)
{
If (month % 2)
Days-= 31;
Else
{
If (2 = D. m_month)
{
If (isleapeyear (D. m_year ))
Days-= 29;
Else
Days-= 28;
}
}
}
}
Days-= D. m_day;
Return (7-days % 7 + s_weekday) % 7 );
}
Int calendar: upyear (date d) const
{
Int year (s_year );
Int days (0 );
For (; year! = D. m_year; + + Year)
{
If (isleapeyear (year ))
Days ++ = 366;
Else
Days ++ = 365;
}
If (D. m_month)
{
For (INT month = 1; month! = D. m_month; ++ month)
{
If (month % 2)
Days + = 31;
Else
{
If (2 = month)
{
If (isleapeyear (D. m_year ))
Days + = 29;
Else
Days + = 28;
}
Else
Days + = 30;
}
}
}
If (D. m_day)
Days + = D. m_day;
Else
Days + = 1;
Return (days % 7 );
}
Void calendar: printdate (date d) const
{
Int wday;
If (testdate (d ))
{
If (D. m_year <s_year)
Wday = lowyear (d );
Else
Wday = upyear (d );
Showcalendar (D, wday );
}
}
Void calendar: showcalendar (date D, int day) const
{
If (D. m_year &&! D. m_month &&! D. m_day)
Showyear (D, Day );
Else
{
If (D. m_year & D. m_month &&! D. m_day)
Showmonth (D, Day );
Else
Showday (D, Day );
}
}
Void calendar: showyear (date D, int day) const
{
Int dnum;
For (INT month = 1; month! = 13; ++ month)
{
Int I (0 );
Cout <D. m_year <"year" <month <"month" <Endl;
Cout <"so" <"Mo" <"tu" <"we"
<"Th" <"FR" <"sa" <Endl;
For (; I! = Day; ++ I)
Cout <"";
Dnum = monthdays (D. m_year, month );
Day = printmonth (dnum, Day );
Cout <Endl;
Cout <Endl;
}
}
Void calendar: showmonth (date D, int day) const
{
Int dnum;
Dnum = monthdays (D. m_year, D. m_month );
Cout <D. m_year <"year" <D. m_month <"month" <Endl;
Cout <"so" <"Mo" <"tu" <"we"
<"Th" <"FR" <"sa" <Endl;
For (INT I = 0; I! = Day; ++ I)
Cout <"";
Day = printmonth (dnum, Day );
Cout <Endl;
}
Void calendar: showday (date D, int day) const
{
Cout <day <Endl;
Cout <D. m_year <"year" <D. m_month <"month"
<D. m_day <"day :";
Switch (day)
{
Case MO: cout <"Monday" <Endl;
Break;
Case Tu: cout <"Tuesday" <Endl;
Break;
Case we: cout <"Wednesday" <Endl;
Break;
Case TH: cout <"Thursday" <Endl;
Break;
Case fr: cout <"Friday" <Endl;
Break;
Case SA: cout <"Saturday" <Endl;
Break;
Case su: cout <"Sunday" <Endl;
Break;
}
}
Inline int calendar: monthdays (INT year, int month) const
{
If (month % 2)
Return 31;
Else
{
If (2 = month)
{
If (isleapeyear (year ))
Return 29;
Else
Return 28;
}
Else
Return 30;
}
}
Int calendar: printmonth (INT dnum, int day) const
{
For (Int J = 1; J! = Dnum + 1; ++ J)
{
If (j <10)
Cout <j <"";
Else
Cout <j <"";
If (! (Day + J) % 7 ))
Cout <Endl;
}
Day = (day + dnum % 7) % 7;
Return day;
}