Before writing a blog post (http://blog.csdn.net/shiwazone/article/details/45053739) is implemented with basic functions, this time using the class design method, that is, the object-oriented approach to rewrite, And added a date to convert the implementation into a string. The program here can also solve the problem of programming Zhu Ji Nanxiong exercise 3.4.
#include "calendar.h" int main () {time t;t.initialtime (); T.show (); T.strshow (); Time T1;t1.initialtime (); T1. Strshow (); Time T2;t2.initialtime (); T2. Strshow (); cout << "The days between T1 and T2 are:" << t1.countdays (T2) <<endl;cout << "The Days be Tween T2 and T1 is: "<< t2.countdays (T1) <<endl;return 0;}
#ifndef canlendar_h#define canlendaa_h#include<iostream> #include <string>using namespace std;string ntos (int t);//number converted to character class Time{private:int year;int month;int day;unsigned int weekday;int weekdaycount () const;// Calculates the day of the week on which the day of the int daycount () const;//calculates the day of the year, the number of days int daysyearcount () const;//calculates the day of the day is the number of days bool Isleapyear () const;// Determine if the year is not a leap years bool Check () const;//Check if the time format is correct void Numtostr (string &sy, String &sm, String &sd, String &sw) const;//numeric date converted to character date public:void initialtime ();//input initialization time void Show () const;//display time information void Strshow () const;// Use the expression of a string to display the date int countdays (const time & T2) const;//calculates the number of days of the two-day period};int Time::weekdaycount () const//calculates the day of the week {return Daycount ()% 7;} int time::D aycount () const//calculates the day of the day {int days = 0;days = (year-1) * 365 + (YEAR-1)/4-(YEAR-1)/100+ (year-1 )/+ + daysyearcount (); return days;} int time::D aysyearcount () const//calculates the day of the year, {int days = 0;int mtemp = month-1;while (mtemp > 0) {switch (mtemp) {case (1 ): Case (3): Case (5): Case (7): Case (8): CASE (+): Case (N): Days + = 31; Break;case (4): Case (6): Case (9): Case (one): Days + = 30; Break;case (2):d ays + = 28; Break;default:break;} --mtemp;} if (Isleapyear ()) ++days;//if it is a leap year, plus a day return days + day;//Returns the calculated number plus the number of days in the month}bool time::isleapyear () const//Determine if the years are leap Year% 4 = = 0 && Yearly% = 0) Return true;//is a multiple of four and is not a multiple of 100, is a leap years if (year% = = 0) return true;else return FA LSE;} BOOL Time::check () const//Check if the time format is correct {if (year <= 0 | | (Month <= 0 | | month>12) | | Day <= 0) return false;else{if (month = = 1 | | Month = = 3 | | Month = = 5 | | Month = = 7| | Month = = 8 | | Month = = 10 | | month = =) && Day > Return false;else{if (month = = 4 | | Month = = 6 | | Month = = 9 | | month = = one) && Day > Return false;else{if (month = = 2) {if (Isleapyear ()) {if (Day >) return false; else return true;} Else{if (Day >) return false; else return true;}}}}} void Time::numtostr (String &sy, String &sm, String &sd, string &sw) const//The numeric date is converted to the character date {sy = Ntos (year/1000) + ntos (year/100-10 * (year/1000)) + Ntos (year/10-10 * (year/100)) + Ntos (ye Ar%), SD = Ntos (DAY/10) + ntos (day%), switch (month) {case (1): SM = "January"; Break;case (2): SM = "February"; BR Eak;case (3): SM = "March"; Break;case (4): SM = "April"; Break;case (5): SM = "may"; Break;case (6): SM = "June"; Break;case (7): SM = "July"; Break;case (8): SM = "August"; Break;case (9): SM = "September"; Break;case (Ten): SM = "October"; Break;case (one): SM = "November"; Break;case (): SM = "December"; Break;default:break;} Switch (Weekday) {case (1): SW = "Monday"; Break;case (2): SW = "Tuesday"; Break;case (3): SW = "Wednesday"; Break;case (4) : SW = "Thursday"; Break;case (5): SW = "Friday"; Break;case (6): SW = "Saturday"; Break;case (7): SW = "Sunday"; Break;default:break;}} void Time::initialtime ()//input initialization time {cout << "Enter the Times (year,month,day): \ n"; Cin >> Year;cin.get (); CIN >> Month;cin.get (); Cin >> Day;cin.get (); if (!check ()) {cout << "Try again:\n"; Initialtime ();} Elseweekday = Weekdaycount ();} void Time::show () const//display time information {cout << year: "<< years <<" \ T "; cout <<" Month: "<< Month & lt;< "\ t"; cout << "Day:" << day << "\ t"; cout << "Weekday:" << Weekday << Endl;cou T << "This is a"; if (Isleapyear ()) cout << "Leap"; else cout << "Nonleap"; cout << "year.\n"; cout << "Today is the" << Daysyearcount () << "Da Ys of the year.\n ";} void Time::strshow () const//uses the expression of the string to display the date {string St, sy, SM, SD, SW;NUMTOSTR (sy, SM, SD, SW); st = sy + "/" + SM + "/" + SD + "," + "Today is" + SW + "."; String::iterator it = St.begin (); for (; It! = St.end (); ++it) cout << *it;cout << endl;cout << "This is a "; if (Isleapyear ()) cout <<" Leap "; else cout << "Nonleap"; cout << "year.\n"; cout << "Today is the" << Daysyearcount () << "Da Ys of the YeaR.\n ";} the int time::countdays (const time & T2) const//calculates the number of days in the two-day period {int t = daycount ()-T2. Daycount (); if (T < 0) Return-t;return T;} string ntos (int t)//number converted to character {switch (t) {case (0): return "0", Case (1): Return "1", Case (2): Return "2", Case (3): Return "3" Case (4): Return "4", Case (5): Return "5", Case (6): Return "6", Case (7): Return "7", Case (8): Return "8", Case (9): return "9";d Efault:break;}} #endif
if you want to reprint, please indicate the source.
Enter a date to determine which day of the year the date is, the days of the week, the day of the two days, and the date used for the string output