输入几月几号 计算出是一年中的第几天

用C怎么写

#include "stdio.h"
#include "conio.h"
main()
{
int day,month,year,sum,leap;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month) /*先计算某月以前月份的总天数*/
{
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;
default:printf("data error");break;
}
sum=sum+day; /*再加上某天的天数*/
if(year%400==0||(year%4==0&&year%100!=0)) /*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1&&month>2) /*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
printf("It is the %dth day.",sum);
getch();
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-01
大体说一下思路吧!我手机上网没法写!菜鸟级别别介意哈! 先定义一个数组!存每个月的天数,例如b[1]=31但是二月随便写日期!用三个变量存年,月,日;写两个函数,第一个是判断闰年的,在输入年月日后调用!返回值是二月的天数,赋给b[2],然后调用算天数的函数,用,函数中用循环把月数之前的时间相加,如十月,则把b[1]到b[9]相加,然后循环结束后把年月日中的日加上就可以了!最后返回!具体细节慢慢想吧!
第2个回答  2008-12-02
//这是一个很简单但很完善的程序,已运行成功,可判断年月日的错误输入,希望能令你满意:
#include<iostream>
using namespace std;
int main()
{
int Year,Month,Day,Sum=0,i;
int a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cin>>Year>>Month>>Day;
if(Year>0&&Month>0&&Month<13&&Day>0&&Day<32)
{
if(Year%4==0&&Year%100!=0)//判断是否是闰年
{
if(Day<=a[Month])//输入的Day必须小于等于Month的总天数
{
for(i=1;i<Month;i++)
Sum+=a[i];
Sum+=Day;
cout<<Sum<<endl;
}
else cout<<"输入有错误!"<<endl;
}
else
{
if(Day<=b[Month])
{
for(i=1;i<Month;i++)
Sum+=b[i];
Sum+=Day;
cout<<Sum<<endl;
}
else cout<<"输入有错误!"<<endl;
}
}
else cout<<"输入有错误!"<<endl;
return 0;
}
第3个回答  2008-12-02
#include <iostream>
using namespace std;
int leapyear(int);
int daynumber(int,int);

int DayNumber[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,28,31,30,31,30,31,31,30,31,30,31}};//如果不是闰年2月等于28天,否则等于29天
int main()
{
int year;
int month;
int day;

cout<<"请输入年,月,日:"<<endl;
cin>>year>>month>>day;

if((year>0)&&(month>=1&&month<=12)&&(day>=1&&day<=31))
{
int leap=leapyear(year);//判断闰年
int WholeDay=daynumber(leap,month-1)+day;//调用计算月函数
cout<<"今天是"<<year<<"的第"<<WholeDay<<"天"<<endl;

}

return 0;
}

int leapyear(int year)//闰年
{
if((year%400==0)&&((year%100!=0)&&(year%4==0))) return 1;//判断闰年
return 0;
}

int daynumber(int leap,int month)//月
{
int sum=0;
for ( int i=0;i<=month;i++)
sum+=DayNumber[leap][i];
return sum;

}
第4个回答  2008-12-01
当做平年来算好了。。
把12个月的每种情况都用switch case语句写出来;
例如,
switch (month)
{
case 1:
date = day;
break;
case 2;
date = 31 + day;
case 3;
date = 59 + day;
break;
....
...
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网