C ++ Perpetual Calendar

Source: Internet
Author: User

// Dateuitl. h

# Ifndef _ dateutil_h _ # DEFINE _ dateutil_h _ # include <iomanip> # include <iostream> # include <string> using namespace STD; Class dateutil {public: int nowtoday (INT year, int month, int day); // The number of days bool inputdayistrue (INT year, int month, int day) from one day to January 1, January 1, 1800 ); // judge whether the input date is valid int finddayweekend (INT year, int month, int day); // find the day of the week int displaymonth (INT year, int month ); // display the calendar int disp of a calendar month of a year Layyear (INT year); // display the calendar PRIVATE: int spacesize of a year; // output length SETW () size bool isleapyear (INT year) {// determine whether it is a leap year return (0 = year % 4 & 0! = Year % 100) | (0 = year % 400);} public: int getmonthdaynumber (INT year, int month) {// return the number of days in a certain year in January int daynumber; If (2 = month) {// February if (isleapyear (year) daynumber = 29; elsedaynumber = 28;} else {If (month <8 & 1 = month % 2) | (month> = 8 & 0 = month % 2 )) // The odd month before December 31, is 31 days, and the even number after December 31 is 31 days daynumber = 31; If (month <8 & 0 = month % 2) | (month >=8 & 1 = month % 2) // 30 days of the even month before January 1, August and 30 days of the odd month after January 1, August daynumber = 30 ;} return daynumber ;};# endif

// Dateutil. cpp

# Include "dateutil. H "int dateutil: nowtoday (INT year, int month, int day) {int diday, I; for (I = 1800, diday = 0; I <year; I ++) // a single leap year + 366 days if (isleapyear (I) diday + = 366; elsediday + = 365; for (I = 1; I <month; I ++) diday + = getmonthdaynumber (year, I); diday + = day-1; return diday;} bool dateutil: inputdayistrue (INT year, int month, int Day) {int turedaynumber; If (year <18 00 | month> 13 | day> 31) {// If the input date is less than January 1, 1800, and later than 31, false return is returned ;} else {turedaynumber = getmonthdaynumber (year, month); // If the input day is greater than the actual day of the month, the system returns the error if (day> turedaynumber) return false;} return true ;} int dateutil: displaymonth (INT year, int month) {int excur, I, totalday; spacesize = 4; excur = (nowtoday (year, month, 1) + 3) % 7; // calculate the day of the month as the day of the week, and then add the number of days on the basis of cout <Year <"year" <month <"month" <Endl; cout <SETW (spacesize) <"day" <SETW (spacesize) <"1" <SETW (spacesize) <"2"; cout <SETW (spacesize) <"3" <SETW (spacesize) <"4" <SETW (spacesize) <"5"; cout <SETW (spacesize) <"6" <Endl; for (I = 1; I <= excur; I ++) cout <SETW (spacesize) <"; // first, output extra spaces totalday = getmonthdaynumber (year, month ); for (I = 1; I <= totalday; I ++) {// then auto-increment. Of course, when the length is greater than seven, wrap cout <SETW (spacesize) <I; If (0 = (I + excur) % 7) // wrap condition cout <Endl;} cout <Endl; return 0;} int dateutil: finddayweekend (INT year, int month, int Day) {// find the day of the week int totalday; char weekend [7] [4] = {"day" },{ "1" },{ "2 "}, {"3" },{ "4" },{ "5" },{ "6" }}; totalday = nowtoday (year, month, day ); cout <year <"year" <month <"month" <day <"Day is a week" <weekend [(totalday + 3)) % 7] <Endl; return 0;} int dateutil: displayyear (INT year) {int I, j, k, n, month1, mon2, month3; // output calendar month1 mon2month3 according to one quarter is the number of days that the month should have int excur1, excur2, excur3; // I, J, K represent the daily value of three months excur1, respectively, excur2, excur3 indicates the offset int sea; char months [12] [10] ={{ "January" },{ "February" },{ "March "}, {"April" },{ "May" },{ "June" },{ "July" },{ "August" },{ "September "}, {"October" },{ "November" },{ "December" }}; spacesize = 3; cout <year <"year" <Endl; fo R (SEA = 1; Sea <= 12; sea + = 3) {// display excur1 = (nowtoday (year, sea, 1) + 3) % 7 in four quarters; // calculate the offset excur2 = (nowtoday (year, sea + 1, 1) + 3) % 7; excur3 = (nowtoday (year, sea + 2, 1) + 3) % 7; month1 = getmonthdaynumber (year, sea); // calculate the number of days in three months, respectively. mon2= getmonthdaynumber (year, sea + 1 ); month3 = getmonthdaynumber (year, sea + 2); // display the three-month week cout <months [sea-1] <SETW (21) <"" <Months [Sea] <SETW (20) <"" <months [sea + 1] <Endl; cout <SETW (spacesize) <"day" <SETW (spacesize) <"1" <SETW (spacesize) <"2"; cout <SETW (spacesize) <"3" <SETW (spacesize) <"4" <SETW (spacesize) <"5"; cout <SETW (spacesize) <"6" <SETW (spacesize) <""; cout <SETW (spacesize) <"day" <SETW (spacesize) <"1" <SETW (spacesize) <"2"; cout <SETW (spacesize) <"3" <SETW (spacesize) <"4" <SETW (spacesize) <"5"; cout <SETW (spacesize) <"6" <SETW (spacesize) <""; cout <SETW (spacesize) <"day" <SETW (spacesize) <"1" <SETW (spacesize) <"2"; cout <SETW (spacesize) <"3" <SETW (spacesize) <"4" <SETW (spacesize) <"5"; cout <SETW (spacesize) <"6" <Endl; For (n = 1; n <= excur1; n ++) // The first line is processed separately. Extra spaces are then cout <SETW (spacesize) <""; // I, j, k separately Save the current date for (N; n <= 7; N ++) cout <SETW (spacesize) <n-excur1; cout <SETW (spacesize) <"; I = N-excur1; for (n = 1; n <= excur2; n ++) cout <SETW (spacesize) <""; for (N; n <= 7; n ++) cout <SETW (spacesize) <n-excur2; cout <SETW (spacesize) <"; j = N-excur2; For (n = 1; n <= excur3; n ++) cout <SETW (Spacesize) <"; for (N; n <= 7; n ++) cout <SETW (spacesize) <n-excur3; cout <Endl; k = N-excur3; while (I <= month1 | j <= mon2|| k <= month3) {// when the three-month date is greater than the expected number of days, the cycle ends for (n = I; I <n + 7; I ++) // output once in the next 7 days. However, if (I <= month1) cout <SETW (spacesize) <I; elsecout <SETW (spacesize) <""; cout <SETW (spacesize) <"; for (n = J; j <n + 7; j ++) if (j <= mon22) cout <SETW (spacesize) <j; elsecout <SETW (spacesize) <"; cout <SETW (spacesize) <""; for (n = K; k <n + 7; k ++) if (k <= month3) cout <SETW (spacesize) <K; elsecout <SETW (spacesize) <"; cout <Endl ;}cout <Endl; return 0 ;}

// Main. cpp

# Include "dateutil. H "int main () {dateutil date; int year, month, day; cout <" Enter year "<Endl; CIN> year; date. displayyear (year); cout <"----------------------" <Endl; cout <"Enter year month" <Endl; CIN> year> month; date. displaymonth (year, month); cout <"----------------------" <Endl; cout <"Enter the year, month, and day" <Endl; cin> year> month> day; date. finddayweekend (year, month, day); Return 0 ;}

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.