参考代码:
#include
using namespace std;
struct d
{
int day, month, year;
}a;
int sum(d &a)
{
int i;
switch (a.month)
{
case 1:i=0;break;
case 2:i=31;break;
case 3:i=59;break;
case 4:i=90;break;
case 5:i=120;break;
case 6:i=151;break;
case 7:i=181;break;
case 8:i=212;break;
case 9:i=243;break;
case 10:i=273;break;
case 11:i=304;break;
case 12:i=334;break;
}
return i;
}
int main()
{
int i;
cout<cin>>a.year;
cout<<"输入月:" ;
cin>>a.month;
cout<<"输入日:";
cin>>a.day;
i=sum(a);
if ((a.year%400==0||(a.year%4==0&&a.year%100!=0))&&a.month>2) //闰年大于三月加一天
i=i+a.day+1;
else
i=i+a.day;
cout<system("pause");
return 0;
}
主要参考代码:
int y,m,d;sum;
cin>>y;
cin>>m;
cin>>d;
if((y%4==0&&y%100!=0)||y%400==0){
switch (m)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=60;break;
case 4:sum=91;break;
case 5:sum=121;break;
case 6:sum=152;break;
case 7:sum=182;break;
case 8:sum=213;break;
case 9:sum=244;break;
case 10:sum=274;break;
case 11:sum=305;break;
case 12:sum=336;break;
}
sum = sum +d;
}else{
switch (m)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
}
sum = sum +d;
}
cout>>sum