k&r上的年月日转具体某一天,具体某一天转年月日,对新手应该有帮助
程序代码:
#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_of_year(int year, int month, int day); void month_day(int year1, int day1, int *pmonth, int *pday); int year, month, day; int year1, day1, yearmonth, yearday; printf("Please input year, month, day converted to year.day!\n"); scanf("%d%d%d", &year, &month, &day); printf("This day is year : %d days : %d\n", year, day_of_year(year, month, day)); printf("\nPlease input year, day converted to month , day\n"); scanf("%d%d", &year1, &day1); month_day(year1, day1, &yearmonth, &yearday); printf("This times is : %d %d %d", year1, yearmonth, yearday); return 0; } int day_of_year(int year, int month, int day) { int i, leap; leap = !(year % 4) && year % 100 || !(year % 400); for (i = 1; i < month; ++i) day += daytab[leap][i]; return day; } void month_day(int year1, int day1, int *pmonth, int *pday) { int i, leap; leap = !(year1 % 4) && year1 % 100 || !(year1 % 400); for (i = 1; daytab[leap][i] < day1; ++i) day1 -= daytab[leap][i]; *pmonth = i; *pday = day1; }