#include #include using namespace std; /* #################################################################### # Tim thu khi biet ngay thang nam # #################################################################### */ // Kiem tra ngay hop le bool check_date(int day, int month) { if ((month == 2) && (day > 0) && (day < 30)) { return true; } if (( (month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12) ) && ((day > 0) && (day < 32))) { return true; } if (( (month == 4) || (month == 6) || (month == 9) || (month == 11) ) && ((day > 0) && (day < 31))) { return true; } return false; } // Kiem tra thang hop le bool check_month(int month) { if ((month > 0) && (month < 13)) { return true; } else { return false; } } // Kiem tra nam hop le bool check_year(int year) { if ((year > 999) && (year < 10000)) { return true; } else { return false; } } // Kiem tra nam nhuan bool check_leap_year(int year) { if (((year % 4) == 0) && ((year % 100) != 0)) { return true; } else if ((year % 400) == 0) { return true; } else { return false; } } // Tim chính xác thu theo ngày tháng nam dã biêt // // http://en.wikipedia.org/wiki/Julian_day#Calculation // const char* get_day(int day, int month, int year) { int JMD; JMD = (day + ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) + (365 * (year + 4800 - ((14 - month) / 12))) + ((year + 4800 - ((14 - month) / 12)) / 4) - ((year + 4800 - ((14 - month) / 12)) / 100) + ((year + 4800 - ((14 - month) / 12)) / 400) - 32045) % 7; //cout << JMD; const char* weekday[] = { "2", "3", "4", "5", "6", "7", "8" }; return weekday[JMD]; } int main() { // Khai bao bien int year, month, date, x, y; cout << "\n[+] CHUONG TRINH TINH THU KHI BIET NGAY THANG NAM."; // Take Inputs cout << "\n[+] Vui long nhap nam [YYYY] : "; cin >> year; // cout << "\n[+] Kiem tra [YYYY] : "; // verify year if (check_year(year)) { cout << "[-] Xac nhan!"; } else { cout << "\n\t [-] Nhap sai!\n"; exit(0); } cout << "\n[+] Vui long nhap thang [MM] : "; cin >> month; // verify month if (check_month(month)) { cout << "[-] Xac nhan!"; } else { cout << "[-] Nhap sai!"; exit(0); } cout << "\n[+] Vui long nhap ngay [DD] : "; cin >> date; // verify date if (check_date(date, month)) { cout << "[-] Xac nhan!\n\n"; } else { cout << "[-] Nhap sai!"; exit(0); } cout << "[+] Ngay : " << date << ", Thang : " << month << ", Nam : " << year << " [ "; if (check_leap_year(year)) { cout << "Nam nhuan" << " ]"; } else { cout << "Khong phai nam nhuan" << " ]"; } cout << "\n[-] Ngay trong tuan: "; cout << get_day(date, month, year); cout << "\n\n"; int date1, date2, m1, m2, y1, y2; cout << "Nhap ngay thang nam 1"; cin >> date1 >> m1 >> y1; cout << "Nhap ngay thang nam 2"; cin >> date2 >> m2 >> y2; int i; if (m1 == 1 || m1 == 3 || m1 == 5 || m1 == 7 || m1 == 8 || m1 == 10 || m1 == 12) { if (m1 < m2) { for ( i = date1;i < 31;i++) { if (get_day(i, m1, year) == "2") { cout << "that day" << endl; cout << year << " " << m1 << " " << i; break; } if (i == 31) { m1++; i = 1; } } } } else if (m1 == 4 || m1 == 6 || m1 == 9 || m1 == 12) { if (m1 < m2) { for ( i = date1;i < 30;i++) { if (get_day(i, m1, year) == "2") { cout << "that day" << endl; cout << year << " " << m1 << " " << i; break; } if (i == 30) { m1++; i = 1; } } } } //ngang trên là in ra ngày thứ 2 gần nhất được rồi .còn giải quyết từ ngày 1/4 đến 31/4 in ra các ngày thứ 2 thì chưa nè while (i < date2 && m1 < m2) { cout << year << " " << m1 << " " << i; if (m1 == 1 || m1 == 3 || m1 == 5 || m1 == 7 || m1 == 8 || m1 == 10 || m1 == 12) { if (m1 < m2) { for (i;i < 31;i+=7) { if (i > 31) { m1++; i = i - 31; } } } } else if (m1 == 4 || m1 == 6 || m1 == 9 || m1 == 12) { if (m1 < m2) { for (i = date1;i < 31;i += 7) { if (i > 31) { m1++; i = i - 31; } } } } } }