年月日闰年平年判断
作者:互联网
#include <iostream>
using namespace std;
class date
{
int Year, Month, Day;
public:
void SetYear(int year);
void SetMonth(int month);
void SetDay(int day);
int isLeap(int year);//布尔判断闰年
int Check(int year, int month, int day);//布尔判断年月日是否合法(年随意正负
//void SetDate(int year,int month,int day);//输入年月日
//void Show(); //告诉大家是否闰年
};
void date::SetYear(int year)//
{
Year = year;
}
void date::SetMonth(int month)
{
Month = month;
//调用check检查是否合法
/* if (check(month)=true) cout<<month;
else cout<<"error";*/
}
void date::SetDay(int day)
{
Day = day;
//调用check检查是否合法
/* if (check(day)=true) cout<<day;
else cout<<"error";*/
}
//布尔判断闰年
int date::isLeap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//闰年的判断条件
return 1;
}
else {
return 0;
}
}
//布尔判断月日
int date::Check(int year, int month, int day)
{
//1.年月日是否合法(年随意正负
if (month < 0 || month>12 || day > 31 || day < 0)
{
return 0;
} //总的满足情况
//2.闰月情况
/* if( month==2 ) {
if(!isLeap(year)&&day>28) return 0;
if(isLeap(year)&&day>29) return 0;
else return 1;
}
*/
if (month == 2)
{
if (isLeap(year) == 1)
{
if (day > 29)
{
return 0;
}
}
}
else {
if (day > 28)
{
return 0;
}
}
//3.小月情况
if (month == 4 || month == 6 || month == 9 || month == 11)
{
{ if (day > 30)
{
return 0;
} }
return 1;
}
}
int main(int argc, char** argv)
{
date c;
int year, month, day;
cin >> year >> month >> day;
c.SetYear(year);
c.SetMonth(month);
c.SetDay(day);
// c.SetDate(year,month,day);
c.Check(year, month, day);
c.isLeap(year);
// c.Show();
if (!c.Check(year, month, day))
{
cout << "该日期不合法";
}
else {
if (!c.isLeap(year))
//cout << year << "/" << month << "/" << day << endl ;
cout<< "no";
else
//cout << year << "/" << month << "/" << day << endl ;
cout<< "yes";
}
return 0;
}
using namespace std;
class date
{
int Year, Month, Day;
public:
void SetYear(int year);
void SetMonth(int month);
void SetDay(int day);
int isLeap(int year);//布尔判断闰年
int Check(int year, int month, int day);//布尔判断年月日是否合法(年随意正负
//void SetDate(int year,int month,int day);//输入年月日
//void Show(); //告诉大家是否闰年
};
void date::SetYear(int year)//
{
Year = year;
}
void date::SetMonth(int month)
{
Month = month;
//调用check检查是否合法
/* if (check(month)=true) cout<<month;
else cout<<"error";*/
}
void date::SetDay(int day)
{
Day = day;
//调用check检查是否合法
/* if (check(day)=true) cout<<day;
else cout<<"error";*/
}
//布尔判断闰年
int date::isLeap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//闰年的判断条件
return 1;
}
else {
return 0;
}
}
//布尔判断月日
int date::Check(int year, int month, int day)
{
//1.年月日是否合法(年随意正负
if (month < 0 || month>12 || day > 31 || day < 0)
{
return 0;
} //总的满足情况
//2.闰月情况
/* if( month==2 ) {
if(!isLeap(year)&&day>28) return 0;
if(isLeap(year)&&day>29) return 0;
else return 1;
}
*/
if (month == 2)
{
if (isLeap(year) == 1)
{
if (day > 29)
{
return 0;
}
}
}
else {
if (day > 28)
{
return 0;
}
}
//3.小月情况
if (month == 4 || month == 6 || month == 9 || month == 11)
{
{ if (day > 30)
{
return 0;
} }
return 1;
}
}
int main(int argc, char** argv)
{
date c;
int year, month, day;
cin >> year >> month >> day;
c.SetYear(year);
c.SetMonth(month);
c.SetDay(day);
// c.SetDate(year,month,day);
c.Check(year, month, day);
c.isLeap(year);
// c.Show();
if (!c.Check(year, month, day))
{
cout << "该日期不合法";
}
else {
if (!c.isLeap(year))
//cout << year << "/" << month << "/" << day << endl ;
cout<< "no";
else
//cout << year << "/" << month << "/" << day << endl ;
cout<< "yes";
}
return 0;
}
标签:判断,return,cout,闰年,int,month,year,年月日,day 来源: https://www.cnblogs.com/pfwvan666/p/16685895.html