代码:
#include <iostream> #include <string> #include <vector> using namespace std; int hashMapping(int mouth) { vector<int> days{31,28,31,30,31,30,31,31,30,31,30,31}; return days[mouth - 1]; } int IsLeapYear(int year, int mouth) { if(((year%4 == 0 && year%100 != 0) || (year%400 == 0)) && mouth == 2) { return hashMapping(mouth) + 1; } return hashMapping(mouth); } int CalculateDays(int start_year, int end_year, int start_mouth, int end_mouth) { int days = 0; if (end_mouth >= start_mouth) { if (start_year < end_year) { for (int i = start_year; i < end_year; i++) { for (int j = start_mouth; j <= 12; j++) days += IsLeapYear(i, j); for (int j = 1; j <= end_mouth; j++) days += IsLeapYear(i, j); } } else { for (int j = start_mouth; j <= end_mouth; j++) days += IsLeapYear(start_year, j); } } else { for (int i = start_year; i < end_year; i++) { for (int j = start_mouth; j <= 12; j++) days += IsLeapYear(i, j); for (int j = 1; j <= end_mouth; j++) days += IsLeapYear(i, j); } } return days; } string hashFunc(int days, int week) { vector<string> str{"星期一","星期二","星期三","星期四","星期五","星期六","星期天"}; return str[(week - 1 + days%7)%7]; } void PrintWeek(int start_year, int start_mouth, int start_day, int week, int end_year, int end_mouth, int end_day) { string we; int days; switch(week) { case 1: days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day); we = hashFunc(days, week); break; case 2: days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day); we = hashFunc(days, week); break; case 3: days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day); we = hashFunc(days, week); break; case 4: days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day); we = hashFunc(days, week); break; case 5: days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day); we = hashFunc(days, week); break; case 6: days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day); we = hashFunc(days, week); break; case 7: days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day); we = hashFunc(days, week); break; } cout << start_year << ‘/‘ << start_mouth << ‘/‘ << start_day << ‘~‘ << end_year << ‘/‘ << end_mouth << ‘/‘ << end_day << " 共有:" << days << "天 " << endl; cout << end_year << "年" << end_mouth << "月" << end_day << "日是" << we << endl; } int main() { int start_year, start_mouth, start_day; int end_year, end_mouth, end_day; int week; cout << "请输入起始年/月/日 以及 星期几:" << endl; cin >> start_year; cin >> start_mouth; cin >> start_day; cin >> week; cout << "请输入结束年/月/日:" << endl; cin >> end_year; cin >> end_mouth; cin >> end_day; PrintWeek(start_year, start_mouth, start_day, week, end_year, end_mouth, end_day); return 0; }
运行结果: