c++ 输入某年某月某日,输出这天在这一年是第几天 例如,2009.2.3 是第34天
第1个回答 2015-10-18
参考代码:
#include <iostream>
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<<endl<<"输入年:";
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<<endl<<a.year<<"年"<<a.month<<"月"<<a.day<<"日"<<"是"<<a.year<<"年的第"<<i<<"天"<<endl;
system("pause");
return 0;
}
本回答被网友采纳
第2个回答 2015-10-18
主要参考代码:
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
Warning: Invalid argument supplied for foreach() in /www/wwwroot/aolonic.com/skin/templets/default/contents.html on line 45
相似回答