int difference(struct date *d1, struct date *d2) { int days_in_months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; long int number_of_days1, number_of_days2; number_of_days1 = d1->year * 365 + d1->day; for (int i = 0; i<d1->month - 1; i++) number_of_days1 += days_in_months[i]; number_of_days1 += leapYears(d1); number_of_days2 = d2->year * 365 + d2->day; for (int i = 0; i<d2->month - 1; i++) number_of_days2 += days_in_months[i]; number_of_days2 += leapYears(d1); return number_of_days2 - number_of_days1 - 1; }
int between_days(struct node *date1head, struct node *date2head){ if((date1head==NULL)||(date2head==NULL)) return -1; int d1, d2, m1, m2, y1 = 0, y2 = 0; int i = 0, r = 1000; int month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int n1, n2; d1 = (date1head->data) * 10 + (date1head->next->data); date1head = date1head->next->next; m1 = (date1head->data) * 10 + (date1head->next->data); date1head = date1head->next->next; while (date1head != NULL) { y1 = y1+date1head->data*r; date1head = date1head->next; r = r / 10; } d2 = (date2head->data) * 10 + (date2head->next->data); date2head = date2head->next->next; m2 = (date2head->data) * 10 + (date2head->next->data); date2head = date2head->next->next; r = 1000; while (date2head != NULL) { y2 = y2 + date2head->data*r; date2head = date2head->next; r = r / 10; } n1 = y1 * 365 + d1; n2 = y2 * 365 + d2; for (int i = 0; i<m1 - 1; i++) n1 += month[i]; n1 += leapYears(y1,m1); for (int i = 0; i<m2 - 1; i++) n2 += month[i]; n2 += leapYears(y2, m2); return n2 - n1-1; }
int main(){ std::vector<std::string> daysOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; std::vector<std::string> monthsOfYear = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; std::vector<date> dateVector; std::vector<int> leapYears(100); std::vector<int> allYears(101); date test; int i = 1900; std::cout << "Generating leap year dates..." << std::endl; std::generate(leapYears.begin(), leapYears.end(), [&](){i++; if (i % 100 == 0 && i % 400 == 0) return i; if (i % 4 == 0) return i; return NULL; }); i = 1899; std::cout << "Generating remaining dates..." << std::endl; std::generate(allYears.begin(), allYears.end(), [&](){i++; return i;}); std::vector<int> leapYearList(25); int k = 0; for (int j = 0; j < leapYears.size(); j++){ if (leapYears[j] != 0){ //std::cout << leapYears[j] << std::endl; leapYearList[k] = leapYears[j]; k++; } } //leapYearList[k] contains all of the leap years int size = 31; for (int l = 0; l <= 100; l++){ //std::cout << "Year: " << allYears[l] << std::endl; for (int m = 0; m < 12; m++){ //std::cout << "Month: " << monthsOfYear[m] << std::endl; if (monthsOfYear[m] == "September" || monthsOfYear[m] == "April" || monthsOfYear[m] == "June" || monthsOfYear[m] == "November") size = 30; else if (monthsOfYear[m] == "February"){ for (int j = 0; j < leapYearList.size(); j++){ if (allYears[l] == leapYearList[j]) size = 29; } if (size != 29) size = 28; } else{ size = 31; } for (int n = 0; n < size; n++){ o = n % 7; if (allYears[l] > 1900){ test.dayOfMonth = n + 1; test.dayOfWeek = daysOfWeek[o]; test.monthOfYear = monthsOfYear[m]; test.numYear = allYears[l]; dateVector.push_back(test); } //std::cout << "Day: " << n+1 << " " << daysOfWeek[o] << std::endl; } char check = 'x'; if (daysOfWeek[o][0] == 'T' && daysOfWeek[o][1] == 'h') check = 'H'; else if (daysOfWeek[o][0] == 'S' && daysOfWeek[o][1] == 'u') check = 'R'; else check = daysOfWeek[o][0]; switch (check){ case 'R': daysOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; break; case 'M': daysOfWeek = { "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday" }; break; case 'T': daysOfWeek = { "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday"}; break; case 'W': daysOfWeek = { "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"}; break; case 'H': daysOfWeek = { "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"}; break; case 'F': daysOfWeek = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; break; case 'S': daysOfWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; break; } } } int answer = 0; std::cout << "Summing the number of days..." << std::endl; for (int i = 0; i < dateVector.size(); i++){ if (dateVector[i].dayOfMonth == 1 && dateVector[i].dayOfWeek == "Sunday" && dateVector[i].numYear > 1900) answer++; } std::cout << "The answer is: " << answer << std::endl; system("Pause"); return 0; }