What is the Mulao of the one-month calendar?

Source: Internet
Author: User

Simulation implementation date Perpetual calendar, including date plus minus days, date minus date, calendar that can output a certain year month includes its own implementation constructor, copy constructor, operator overloading, and other default member functionsThe output calendar instance is similar to:          "Thinking Analysis"to print out a date calendar first you need to know if it is a leap year? is a leap year that has 366 days, non-leap years, 365 days; After a leap year, you also need to know the total number of days of each month in the years, where I used an array to store the number of days per month , a leap year to change February to 29 days, a non-leap year without modification (28 days), When the storage month is set to waste the subscript 0 element, more close to the actual; With these basics, here's how to print the calendar.What is the first day of the month that is required on the date of printing? Here I offer two ways of thinking:idea One:Set the date origin, I set the date origin is 1600.1.1 (Saturday), you can set the date origin according to their preferences, preferably set to Monday convenient later calculation; After setting the date origin, we need to calculate the number of days between the first day of the target month and the date of the origin. In this case, we will use the date minus date function we implemented before, we need to find out the number of days after the difference between the number of days plus the previous date origin of Saturday again%7 until the week is less than 7 (seven days a week), if the last week will be 7, we only need to reduce 7 can be handled well ; the date in which the year is less than the date origin (1600.1.1) is not processedidea two:using the Caille formula, interested in children's shoes can go on their own Baidu, here I will not add to the introduction offind out the first day of the month after the week is the format of printing the calendar, with a double for loop can be implemented, see the Code Implementation section for detailshere I would like to introduce a statement about the background color in C + +:system ("Color 0A") where color after the 0 is the background color code, a is the foreground color code. The color codes are as follows:
0 = Black 1= Blue 2 = Green 3= Lake Blue 4 = Red 5 = Purple 6 = Yellow 7 = White 8 = Gray 9 = light blue a= light green b= light green c= light Red d= lavender e= light yellow f= bright white readers can set the background color according to their preferences "Code Implementation section"Date.h      
#define _crt_secure_no_warnings 1#include <iostream> using namespace std;  Class Date {friend void printdate (int year, int month); Friend function public:date (int year=2016,int month=1,int day=1)//How to initialize the list: _year (year), _month (month), _day (day) {if (month ; 12 | |    Day > Initday (*this) {cout<< "entry date not valid" <<endl; }} Date (const date& D): _year (D._year), _month (D._month), _day (d._day) {}date& operator= (const date& D);    Public:void Display () {cout<<_year<< "-" <<_month<< "-" <<_day<<endl;    } Date operator+ (int day);  Date plus minus days date operator-(int day);     int operator-(const date& D);   Date minus date bool operator== (const date& D);  Operator overload BOOL Operator> (const date& D);  BOOL date::operator< (const date& D);p rivate:bool judgeleap (int year); Check leap year int initday (const date& D);      Calculates the number of days of the month in that date Private:int _year;      int _month;  int _day; };void printdate (int year,int month); Print a calendar

#define _crt_secure_no_warnings 1#include "Date.h" Date date::operator+ (int day) {int tmp=0;      Date d (*this);              while (Day > 0) {if (D._month = = 13) {//If it is 1 March then jump directly into the next year d._year++;          D._month = 1;   } tmp=initday (d)-d._day;              The number of days remaining in the month if (D._day+day > Initday (*this)) {//The number of days to add is greater than the number of days remaining in the month day-= TMP;              d._month++;          D._day = 0;              } else {//the number of days to add is less than the number of days remaining in that month d._day + = day;          Day = 0;  }} return D;      } date date::operator-(int day)//similar date plus days {Date d (*this);              while (Day > 0) {if (D._month = = 0) {//If the element is subscript 0 directly to the previous year d._year--;          D._month = 12;              } if (d._day-day <= 0) {d._month--;              Day-= D._day;  D._day = Initday (d);              } else {d._day-= day; Day = 0;          }} return D;  } int date::operator-(const date& d) {int day = 0;      Date Small (d) for the number of days between statistical dates; Date Large (*this);//large points to the large date, small points to the small date if ((!) (          *this > D))//<= {small = *this;      large = D;              } while (Small._year < large._year)//different years of the situation {if (Judgeleap (small._year))//is a leap year {          Day + = 366;          } else {day + = 365;          } small._year++;              while (Small._month > Large._month) {Day-= Initday (small);          small._month--;          }} while (Small._month < large._month)//different months of the same year {day + initday (small);      small._month++;  } Day + = (large._day-small._day); The same month directly subtract the number of days if ((!)  *this < D))//>= return day where a negative number may appear;  return 0-day; } bool Date::judgeleap (int year)//check whether the years are leap (_year%4 = = 0 && _year%100! = 0 | | _year%400 = = 0) return true;    is a leap year return false;} int Date::initday (const date& D)//Initialize the number of months of the year {//waste dropped to 0 position int day[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31      };      if (Judgeleap (d._year)) {day[2] = 29;  } return Day[d._month];  }static int Countweek (int year, int month)//calculates the first day of the month is the week {if (Year >= && month >= 1) {int week =  6;//1600.1.1 for Saturday (set date origin) int day = 0;  Date D1 (1600,1,1);  Date D2 (year, month, 1);  day=d2-d1;//Statistics The first day of the target month differs from the date origin by how many days week = Day%7+week;  if (Week > 7) {week-= 7;   } return week;  } return-1;  Do not process}void printdate (int year, int month)//print Calendar {int Day=1 when the calendar is required to be less than 1600.1.1 months;  int I=0,j=0;int week = Countweek (year, month);  if (Week! =-1) {cout<<year<< "year" <<month<< "month" <<endl; cout<< "Day" << "one" << "two" << "three" << "four" << "five" << "six" <<endl;  Date d (year,month,1);  for (; i < week; i++) {cout<< ""; } for (j = 0; J < 6; J + +)//j control the most outputHow many lines {for (; i < 7; i++)//i control the number of output dates per line {if (Day <= d.initday (d)) {printf ("%2d", "Days");  day++;  } Elsebreak;  } i = 0;  cout << Endl; }}else{cout<< "Your input is incorrect, this calendar cannot be calculated" <<endl;return;}} BOOL date::operator== (const date& D) {return _year = = D._year &&_month = = D._month &&_day = = d.  _day;      } bool Date::operator> (const date& D) {if (_year > D._year) {return true;          } else if (_year = = d._year) {if (_month > D._month) {return true; } else if (_month = = D._month) {if (_day > D._day) {ret              Urn true;              } else {return false;          }} else {return false;      }} else {return false; }} date& date::operator= (const date& D) {if (*this = = d) {return *this;  } this->_year=d._year; When (*this! = d) is executed, continuous assignment is possible this->_month=d._month;    this->_day=d._day;  return *this;      } bool date::operator< (const date& D) {if (_year < d._year) {return true;          } else if (_year = = d._year) {if (_month < D._month) {return true; } else if (_month = = D._month) {if (_day < d._day) {RET              Urn true;              } else {return false;          }} else {return false;      }} else {return false;   }  }

#define _crt_secure_no_warnings 1#include "Date.h" void test1 ()//date plus minus days {date D1 (2016,8,6); D1. Display ();  Date d2=d1+24;////date d2=d1+80; Date d2=d1+501;//d2.   Display (); 2016-8-30 2016-10-25 2017-12-19date d2=d1-80;d2.  Display ();  2016-5-18} void Test2 ()//date minus date {int day = 0;    Date D1 (2016,8,6);  Date D2 (2016,8,31); Date D2 (2016,10,5);     Date D2 (2017,10,1); day=d2-d1;cout<< "Difference:" <<day<< "Day" &LT;&LT;ENDL;//25 421} void Test3 () {Date D1 (2016,8,6);D ate D2 (d1    );D ate D3 (2016,9,5);//cout<< (D1 = = D2) <<endl;    1 ==//cout<< (D1 = = D3) <<endl; 0 ==//cout<< (!) ( D1 = = D2)) <<endl; 0!=d1.  Display (); 2016-8-6d1=d3;d1.  Display ();       2016-9-5cout<< (D1 < D2) <<endl; 0 <cout<< (!) (    D1 < D2) <<endl;       1 >=cout<< (D1 > D3) <<endl; 0 >cout<< (!) (    D1 > D3) <<endl; 1 <=}void test4 () {intyear=0,month=0;cin>>year>>month;  PrintDate (Year,month);  } int main () {//test1 ();   Test2 ();    Test3 (); test4 (); System ("color 03");      System ("pause");  return 0;   }


What is the Mulao of the one-month calendar?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.