Viết chương trình nhập vào ngày, tháng, năm. Máy sẽ hiện lên ngày, tháng, năm hôm sau 06/08/2021 Bởi Valentina Viết chương trình nhập vào ngày, tháng, năm. Máy sẽ hiện lên ngày, tháng, năm hôm sau
#include <bits/stdc++.h>using namespace std;bool laNamNhuan(int nYear){ if ((nYear % 4 == 0 && nYear % 100 != 0) || nYear % 400 == 0) { return true; } return false;}int tinhSoNgayTrongThang(int nMonth, int nYear){ int nNumOfDays; switch (nMonth) { case 1:case 3:case 5:case 7:case 8:case 10:case 12: nNumOfDays = 31; break; case 4:case 6:case 9:case 11: nNumOfDays = 30; break; case 2: if (laNamNhuan(nYear)) { nNumOfDays = 29; } else { nNumOfDays = 28; } break; } return nNumOfDays;}bool laNgayHopLe(int nDay, int nMonth, int nYear){ bool bResult = true; if (!(nYear > 0 && nMonth)) { bResult = false; } if (!(nMonth >= 1 && nMonth <= 12)) { bResult = false; } if (!(nDay >= 1 && nDay <= tinhSoNgayTrongThang(nMonth, nYear))) { bResult = false; } return bResult;}void timNgayHomSau(int& nDay, int& nMonth, int& nYear){ nDay++; if (nDay > tinhSoNgayTrongThang(nMonth, nYear)) { nDay = 1; nMonth++; if (nMonth > 12) { nMonth = 1; nYear++; } }}int main(){ int nDay, nMonth, nYear; cout << “Ngay: “; cin >> nDay; cout << “Thang: “; cin >> nMonth; cout << “Nam: “; cin >> nYear; if (laNgayHopLe(nDay, nMonth, nYear)) { int tmpDay = nDay; int tmpMonth = nMonth; int tmpYear = nYear; timNgayHomSau(tmpDay, tmpMonth, tmpYear); cout << “Ngay hom sau: ” << tmpDay << ” / ” << tmpMonth << ” / ” << tmpYear; } else { cout << “Khong hop le.” << endl; } //samon247 return 0;} Bình luận
#include <bits/stdc++.h>
using namespace std;
bool laNamNhuan(int nYear)
{
if ((nYear % 4 == 0 && nYear % 100 != 0) || nYear % 400 == 0)
{
return true;
}
return false;
}
int tinhSoNgayTrongThang(int nMonth, int nYear)
{
int nNumOfDays;
switch (nMonth)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
nNumOfDays = 31;
break;
case 4:case 6:case 9:case 11:
nNumOfDays = 30;
break;
case 2:
if (laNamNhuan(nYear))
{
nNumOfDays = 29;
}
else
{
nNumOfDays = 28;
}
break;
}
return nNumOfDays;
}
bool laNgayHopLe(int nDay, int nMonth, int nYear)
{
bool bResult = true;
if (!(nYear > 0 && nMonth))
{
bResult = false;
}
if (!(nMonth >= 1 && nMonth <= 12))
{
bResult = false;
}
if (!(nDay >= 1 && nDay <= tinhSoNgayTrongThang(nMonth, nYear)))
{
bResult = false;
}
return bResult;
}
void timNgayHomSau(int& nDay, int& nMonth, int& nYear)
{
nDay++;
if (nDay > tinhSoNgayTrongThang(nMonth, nYear))
{
nDay = 1;
nMonth++;
if (nMonth > 12)
{
nMonth = 1;
nYear++;
}
}
}
int main()
{
int nDay, nMonth, nYear;
cout << “Ngay: “;
cin >> nDay;
cout << “Thang: “;
cin >> nMonth;
cout << “Nam: “;
cin >> nYear;
if (laNgayHopLe(nDay, nMonth, nYear))
{
int tmpDay = nDay;
int tmpMonth = nMonth;
int tmpYear = nYear;
timNgayHomSau(tmpDay, tmpMonth, tmpYear);
cout << “Ngay hom sau: ” << tmpDay << ” / ” << tmpMonth << ” / ” << tmpYear;
}
else
{
cout << “Khong hop le.” << endl;
}
//samon247
return 0;
}