c语言题目,求助大神撒<( ̄ˇ ̄)/~ 输入日期,调用函数判断该日期是这一年的第几天。 如:输入是

c语言题目,求助大神撒<( ̄ˇ ̄)/~
输入日期,调用函数判断该日期是这一年的第几天。
如:输入是2009年1月31日,输出第31天。
函数格式为:int day(int y,int m, int d)
定义一维数组存放一年12个月的天数:
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}
(提示:考虑是否为闰年情况。)

#include<stdio.h>
int main()
{int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31},y,m,d,i,s;
 scanf("%d%d%d",&y,&m,&d);
 if(y%4==0&&y%100||y%400==0)a[2]++;
 if(m<1||d<1||m>12||y<1||d>a[m])
 {printf("Date error!\n"); return 0;}
 s=d;
 for(i=1;i<m;i++)s+=a[i];
 printf("%d-%d-%d is the %dth day in the year.\n",y,m,d,s);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-03-16
#include <stdio.h>

int isLeap(int year) {
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) 
return 1;
return 0;
}

int main() {
int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int i,year,month,day,days;
printf("年 月 日:\n");
while(scanf("%d%d%d",&year,&month,&day) == 3) {
days = day;
if(month > 2) days += isLeap(year);
for(i = 0; i < month - 1; ++i) days += months[i];
printf("第 %d 天\n",days);
printf("年 月 日(q to quit):\n");
}
return 0;
}

本回答被提问者和网友采纳
第2个回答  2015-06-06
急着要吗,不着急的话?我可以帮你追问

不是太着急,明天下午之前吧

追答

楼下写的很清楚了,你采纳他的吧

相关了解……

你可能感兴趣的内容

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