虽然开始错了,但还是解决了

来源:互联网 发布:mac搜狗输入法怎么设置 编辑:程序博客网 时间:2024/05/04 21:27
.编写一个函数判断输入的年月日是否合法。输入例如 2012 2 12形式

#include <iostream>
using namespace std;
int year(int,int,int);
int main()
{
int y,m,t,i;
cout<<"请输入年月日:";
cin>>y>>m>>t;
i=year(y,m,t);
if(i==1)
cout<<"形式合法"<<endl;
else
cout<<"形式不合法"<<endl;
return 0;
}
int year(int a,int b,int c)
{
if((a%4==0&&a%100!=0)||(a%400!=0))
{ if(b==2)
{ if(c>0&&c<=29)
return 1;
else
return 0;
}
else if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{ if(c>0&&c<=31)
return 1;
else
return 0;
}
else if(b==4||b==6||b==9||b==11)
{ if(c>0&&c<=30)
return 1;
else
return 0;
}
}
else
{
if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{ if(c>0&&c<=31)
return 1;
else
return 0;
}
else if(b==4||b==6||b==9||b==11)
{ if(c>0&&c<=30)
return 1;
else
return 0;
}
else if(b==2)
{
if(c>0&&c<=28)
return 1;
else
return 0;
}
else
return 0;
}
}