求C++大神解答,判断输入的日期是否合理。求代码。100分。
#include "iostream"
using namespace std;
struct MyDate
{
int year;
int month;
int day;
};
int checkDate(MyDate date)
{
int flag=date.year %400==0 ||(date.year %4==0 && date.year %100!=0);
if(date.year<=0 || date.month<=0 || date.day <=0)
return 0;
if(((date.month==1 || date.month==3 || date.month==5 ||date.month==7 ||date.month==8 ||date.month==10 ||date.month==12)&&date.day<=31)
||((date.month==4 ||date.month==6 ||date.month==9 ||date.month==11) && date.day<=30)
||(flag==1 && date.month==2 && date.day<=29)
||(flag==0 && date.month==2 && date.day<=28))
return 1;
else
return 0;
}
int main()
{
MyDate date={0};
cout<<"请输入一个日期;";
cin>>date.year>>date.month>>date.day;
if(checkDate(date)==1)
cout<<date.year<<"年"<<date.month<<"月"<<date.day<<"日"<<endl;
else
cout<<"输入日期不正确!"<<endl;
}
你确定是C++语言,而不是C?
确定用结构体而不用类?
#include <iostream>
using namespace std;
struct MyDate
{
int y, m, d;
};
bool judge(MyDate &date)
{
static int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int y = date.y,
m = date.m,
d = date.d;
bool leap = (y%400==0 || y%100 && y%4==0);
return y > 0
&& (m > 0)&&(m <= 12)
&& (d > 0)&&(d <= (m==2 && leap) + mon[m]);
}
int main()
{
MyDate Date;
cin >> Date.y >> Date.m >> Date.d;
if(judge(Date))
cout << "Yes";
else
cout << "No";
}