1编写程序判断一个年是否是闰年,是闰年返回ture,不是闰年返回false
#include<iostream>
using namespace std;
int year(long int x);
int main()
{
long int a;
cout<<"请输入一个年份:";
cin>>a;
if(year(a)==1)
cout<<"false!"<<'\n';
else
cout<<"true!"<<'\n';
return 0;
}
int year(long int x)
{
int num;
if(x%4==0)
{
if(x%100==0)
{
if(x%400==0)
num=1;
else
num=0;
}
else num=1;
}
else num=0;
return (num);
}
(2)编写一个程序判断输入的年月日是否合法。输入例如 2012 2 12形式
#include<iostream>
using namespace std;
int year(long int x)
{
int num;
if(x%4==0)
{
if(x%100==0)
{
if(x%400==0)
num=1;
else
num=0;
}
else num=1;
}
else num=0;
return (num);
}
int main()
{
long int a;
int b,c;
cout<<"请输入一个日期:";
cin>>a>>b>>c;
if(a<=0)
cout<<"该日期格式不合法";
else
{
if(b<1||b>=12)
cout<<"该日期格式不合法";
if(b==2)
{
if(year(a)==1)
{
if(c<1||c>29)
cout<<"该日期格式不合法";
else cout<<"该日期格式合法";
}
else
{
if(c<1||c>28)
cout<<"该日期格式不合法";
else cout<<"该日期格式合法";
}
}
if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{
if(c>31) cout<<"该日期格式不合法";
else cout<<"该日期格式合法";
}
if(b==4||b==6||b==9||b==11)
{
if(c>30) cout<<"该日期格式不合法";
else cout<<"该日期格式合法";
}
}
return 0;
}