日期部分处理(C/C++)

目录

1  每个月的天数存储

2  闰年判断

3   闰年处理

4  回文

5  abab判断(在回文的前提下)

6  日期合法


1  每个月的天数存储

int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

2  闰年判断

bool is_leap(int y)//闰年判断 
{
	return y%400==0||y%4==0&&y%100!=0;
}

3   闰年处理

int DaysOfMonth(int y,int m)//2月处理 
{
	if(m==2)
	{
		return is_leap(y)+28;
	}
	return day[m];
}

4  回文

bool is_pali(int x)//是否回文 
{
	string str=to_string(x);
	string s=str;
	reverse(s.begin(),s.end());
	return s==str;
}

5  abab判断(在回文的前提下)

bool is_ab(int x)//abab判断 
{
	string str=to_string(x);
	return str[0]==str[2]&&str[1]==str[3];
}

6  日期合法

bool is_valid(int x)//日期合法 
{
	string str=to_string(x);
	month=(str[4]-48)*10+str[5]-48;
	day=str[6]*10+str[7];
	if(month<=12&&day<=day[month]) return true;
	else return false;
}

猜你喜欢

转载自blog.csdn.net/m0_71934846/article/details/129697869