#include<stdio.h> //定義全局變量 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; //函數的聲明 int inputDate(void); int nextSceond(void); int leapYear(int year); int dayMonth(int month); /* *函數名:inputDate *參數 :void *返回值:void *功能 :接受用戶的輸入,並對數據進行檢查 */ int inputDate() { int loop = 0; for(; loop < 3; loop++) { printf("請輸入年月日:"); scanf("%d %d %d", &year, &month, &day); if(month < 1 || month > 12) { printf("月份輸入錯誤!\n"); continue; } else { if(day < 1 || day > dayMonth(month)) { printf("day輸入錯誤\n"); continue; } else { break; } } } for(; loop < 3; loop++) { printf("請輸入時分秒:"); scanf("%d %d %d", &hour, &minute, &second); if(hour < 0 || hour > 23) { printf("hour輸入錯誤!\n"); continue; } else { if(minute < 0 || minute > 59) { printf("minute !\n"); continue; } else { if(second < 0 || second > 59) { printf("second!\n"); continue; } else { break; } } } } } /* *函數名:nextSecond *參數 :void *返回值:void *功能 :計算下一秒的時間 */ int nextSceond() { if(59 == second) { minute += 1; second = 0; if(60 == minute) { hour += 1; minute = 0; if( 24 == hour) { day += 1; hour = 0; if(day > dayMonth(month)) { month += 1; day = 1; if(13 == month) { year += 1; month = 1; } } } } } else { second += 1; } printf("%2d-%2d-%2d\n%2d:%2d:%2d\n",year,month,day,hour,minute,second); } /* *函數名:leapYear *參數 :int year *返回值:int *功能 :判斷是否爲閏年 */ int leapYear(int year) { if(0 == year % 4 && 0 != year % 100 || 0 == year % 400) return 1; else return 0; } /* *函數名:dayMonth *參數 :int month *返回值:int *功能 :返回每月份對應的天數 */ int dayMonth(int month) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if(0 == year % 4 && 0 != year % 100 || 0 == year %400) return 29; else return 28; } } int main() { inputDate(); leapYear(year); dayMonth(month); nextSceond(); return 0; }