程序要求:
把某年某月某天这种日期形式转换为某年中第几天的表示形式,反之亦然。下列两个例子实现日期转换,前一个中函数day_of_year将某年某月某日转化为某年中的第几天。而第二个例子则相反。
eg1:
- #include <stdio.h>
- #include <stdlib.h>
- static char daydtab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
- {0,31,29,31,30,31,30,31,31,30,31,30,31}
- };
- int main()
- {
- int year, month,day;
- int days;
- printf("please input year:mouth:day\n");
- scanf("%d:%d:%d",&year,&month,&day);
- days=day_of_year(year,month,day);
- printf("%d\n",days);
- return 0;
- }
- int day_of_year(int year,int month,int day)
- {
- int leap;
- int i;
- if(year%4==0 && year %100!=0 || year %400==0)
- leap=1;
- if(month<1||month>12)
- {
- printf("your input month error");
- return -1;
-
- }
- if(day<1 || day>daydtab[leap][month])
- {
- printf("your input day error");
- return -1;
-
- }
- for(i = 1; i < month; i++)
- day = day + daydtab[leap][i];
-
- return day;
- }
eg2:
- #include <stdio.h>
- static char daytab[2][13]={
- {0,31,28,31,30,31,30,31,31,30,31,30,31},
- {0,31,29,31,30,31,30,31,31,30,31,30,31}
- };
- int main()
- {
-
- //int *day="0";
- int yearday;
- int year;
- printf("please input year:day:\n");
- scanf("%d:%d",&year,&yearday);
- month_day(year,yearday,0,0);
- return 0;
- }
- void month_day(int year,int yearday,int *pmonth,int *pday)
- {
- int i;
- int leap;
- leap = year%4==0 && year%100 !=0 || year%400==0;
- for(i=1;i<12 && yearday>daytab[leap][i];i++)
- yearday-=daytab[leap][i];
- if(i>12 && yearday>daytab[leap][12])
- {
- *pmonth=-1;
- *pday=-1;
-
-
- }
- else
- {
- *pmonth=i;
- *pday=yearday;
- return ;
- }
- }
其中第二个例子出现段错误,求高手赐教指点。
阅读(1500) | 评论(0) | 转发(0) |