[C]Date to Date
最近写的
为了培养自己良好的代码风格~~~
为了培养自己良好的代码风格~~~
/** * 标题: Date to Date * 描述: 返回任意两个日期之间的天数总计 * 作者: hjc * 版本: 1.1 * 时间: 2005.09.23 */ //======================================== //file head //======================================== #include <stdio.h> //======================================== //function declare //======================================== int DaysToElapse (int $year, int $month, int $day); int DaysToSurplus (int $year, int $month, int $day); int DaysToYearTotal (int $year_outset, int $year_finish); int LeapYearToCheckUp (int $year); //======================================== //main //======================================== int main () { int date_outset[3]; int date_finish[3]; printf("Please input outset date, for example: 2004/6/10\n"); printf("="); scanf ("%d/%d/%d", &date_outset[0], &date_outset[1], &date_outset[2]); printf("\nPlease input finish date, form ditto...\n"); printf("="); scanf ("%d/%d/%d", &date_finish[0], &date_finish[1], &date_finish[2]); int day_elapse = DaysToSurplus ( date_outset[0], date_outset[1], date_outset[2] ); int day_surplus = DaysToElapse ( date_finish[0], date_finish[1], date_finish[2] ); int total = 0; if(date_outset[0] == date_finish[0]) { total = DaysToElapse ( date_finish[0], date_finish[1], date_finish[2]) - DaysToElapse ( date_outset[0], date_outset[1], date_outset[2]); } else if(date_outset[0] < date_finish[0]) { total = day_elapse + day_surplus; total += DaysToYearTotal (date_outset[0], date_finish[0]); } printf("\nTotal %d days between %d/%d/%d and %d/%d/%d.\n\n\n", total, date_outset[0],date_outset[1],date_outset[2], date_finish[0],date_finish[1],date_finish[2]); printf("Please any key to exit..."); getch(); return 0; } //======================================== //function //======================================== //检测是否为闰年 int LeapYearToCheckUp (int $year) { if($year%400==0 || ($year%4==0 && $year%100!=0)) { return 1; } else { return 0; } } //计算本年逝去天数 int DaysToElapse (int $year, int $month, int $day) { int i; for (i=1; i<$month; i++) { if(i==2) { if(LeapYearToCheckUp ($year) == 1) { $day += 29; } else { $day += 28; } } else if(i==4 || i==6 || i==9 || i==11) { $day+=30; } else { $day+=31; } } return $day; } //计算本年剩余天数 int DaysToSurplus (int $year, int $month, int $day) { return DaysToElapse ($year, 12, 31) - DaysToElapse ($year, $month, $day); } //合计两年之间天数 int DaysToYearTotal (int $year_outset, int $year_finish) { int i,$temp = 0; for (i=$year_outset+1; i<$year_finish; i++) { $temp += DaysToElapse (i, 12, 31); } return $temp; }
[2005-10-28 1:17]