标签:style blog color strong 问题 for
代码实现:
#include<stdio.h> int 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 day_of_year(int year, int month, int day){ int result = 0; int flag = year%4 == 0 && year%100 != 0 || year%400==0; //判断是否为闰年。是则返回1,否则返回0 int i = 0; // 错误检查 if (year < 1752 || month < 1 || month > 12 || day < 1) return -1; if(day > daytab[flag][month]){ return -1; } // 循环 for(; i < month; i++){ result += daytab[flag][i]; } result += day; return result; } /* 把某年中第几天表示形式转换为某月某日这种日期形式 */ int month_day(int year, int theDays, int *pmonth, int *pday){ int flag = year%4 == 0 && year%100 != 0 || year%400==0; //判断是否为闰年。是则返回1,否则返回0 int i = 0; // 错误检查 if (year < 1752 || theDays < 1){ return -1; } if ((flag && theDays > 366) || (!flag && theDays > 365)){ return -1; } for(; theDays > daytab[flag][i] && i <= 12 ; i++){ theDays -= daytab[flag][i]; } *pmonth = i; *pday = theDays; return 0; } /* 主函数 */ void main(){ int month, day; printf("%d\n",day_of_year(1988, 3, 1)); month_day(1988, 61, &month, &day); printf("month = %d, day = %d\n", month, day); }
标签:style blog color strong 问题 for
原文地址:http://www.cnblogs.com/ningvsban/p/3813418.html