#include
Bool IsleApyear; Bool IsdateValid (Long Dwyear, Long Dwmonth, long dwday);
/ / -------------------------------------------------------------------------------------------- ------------------------------ // Zeller formula (only after October 15, 1582 Situation): // w = y [Y / 4] [C / 4] - 2C [13 (M 1) / 5] D - 1 //// The symbols in the formula are as follows: / / C: Century minus one (year high two digits); // Y: Year (year with low two digits); //m: month (M is greater than equal to 3, less than or equal 14, ie in Caile formula In the first and second months of a year, it should be calculated as the previous year // 13th and 14 months. Day; // [] represents the finishing, that is, as long as the integer part. // W: Week; W: 0 - Sunday, 1-Monday, 2- Tuesday, 3-Wednesday, 4-Thursday, // 5-Friday, 6-Saturday // Note Negative number The remainder of the remainder is expensive, and only the remainder of the remainder in the mark. To / / conveniently calculate, we can add a 7-degree multiplier to make it a positive number, and then ask for a rest. //// Take February 14, 2005 as an example: c = 20, y = 4, m = 14, D = 14 // w = 4 [4/4] [20/4] - 2 * 20 [26 * (14 1) / 10] 14 - 1 // = 4 1 5 - 40 39 14 - 1 / / = 22 (divided by more than 7) // So in February 2005 14th is Monday. //// ------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------- Long Calcdayofweek (long dwyear, long dwmonth, long dwday) {long dwweek; long Long dwnewyear, dwnewmonth;
IF (! isdatevalid (dwyear, dwmonth, dwday) {return -1;}
From January 1, February, the last year 13, 14 month IF (dwmonth <3) {dwnewyear = (dwyear - 1)% 100;} else {dwnewmonth = dwmonth; Dwnewyear = dwyear% 100;
DWcent = dwyear / 100;
/ / To avoid the last result is negative, plus 7C, the formula becomes: // w = y [y / 4] [C / 4] - 2C [13 (M 1) / 5] D - 1 7C // w = y [y / 4] [c / 4] 5C [13 (m 1) / 5] D - 1 dwweek = dwnewyear dwnewyear / 4 dWcentury / 4 5 * DWCENTURY 13 * (DWNEWMONTH 1) / 5 DWDAY - 1; dwweek = dwweek% 7;
Return dwweek;}
INT Main (int Argc, char * argv []) {char szday [25] = {''};
Long w = CalcDayofweek (2005, 3, 1);
Switch (w) {case 0: Sprintf (szday, "sunday"); break; case 1: sprintf (szday, "monday"); break; case 2: sprintf (szday, "tuesday); break; case 3: Sprintf (Szday, "Wednesday"); Break; Case 4: Sprintf (Szday, "Thursday"); Break; Case 5: Sprintf (Szday, "Friday"); Break; Case 6: Sprintf (szday, "Saturday") Break; Default: Sprintf (Szday, "Invalid Date!"); Break;} cout << szday << endl; return 0;}
/ / / Decision a year is a leap year // BOOL IsleApyear (((Dwyear% 4 == 0) && (DWYear% 100! = 0)) || (dwyear% 400 == 0) ) {RETURN true;} Return False;
//// judgment date is effective // Bool IsdateValid (long dwyear, long dwmonth, long dwday) {if (Dwyear <1) || (dwmonth <1) || (dwMonth> 12) || (DWDAY <1 )) {RETURN FALSE;
// The second month of the leap year is 29 days, otherwise only 28 days if (dwMonth == 2) {if (isleApyear (dwyear)) {if (dwday> 29) {return false;}} else if (dwday> 28) { Return false;}} else {// Dalgo 31 days, Xiaoyue is only 30 days if (DWMONTH == 1) || (dwmonth == 3) || (dwmonth == 5) || (dwmonth == 7) || (dwmonth == 8) || (dwmonth == 10) || (dwMonth == 12)) {if (dwday> 31) {RETURN FALSE;}} else} ((dwmonth == 4) | | (DWMONTH == 6) || (dwmonth == 9) || (dwmonth == 11)) {if (dwday> 30) {Return false;}}}} return