1. 程式人生 > >編寫一個函式,要求輸入年月日時分秒, 輸出該年月日時分秒的下一秒

編寫一個函式,要求輸入年月日時分秒, 輸出該年月日時分秒的下一秒

/************************************* 
    編寫一個函式,要求輸入年月日時分秒, 
    輸出該年月日時分秒的下一秒。 
    如輸入2004年12月31日23時59分59秒, 
    則輸出2005年1月1日0時0分0秒 
**************************************/ 
 
#include <stdio.h>  
#include <stdlib.h>  
 
/* define function */ 
void InputData(void);  
int LeapYear(int year);  
void NextSec(void);  
 
/* month[1]:leap year; month[0]: common year */ 
int Amonth[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 leap = 0;  
int year, month, day, hour, minute, second;  
/* 
 * Function Name: LeapYear() 
 * Describe: judge the current year is leap year or not 
 * Paramete: int year:current year 
 * Return Value: 1-leap year; 0-not 
 */ 
int LeapYear(int year)  
{  
    if ( (year%4 == 0) && (year%100 != 0) ||   
         (year%400 == 0) )  
    {  
         return 1;  
    }  
    else 
    {  
        return 0;  
    }  
}  
 
/* 
 * Function Name: InputData() 
 * Describe: input data 
 * Paramete: void 
 * Return Value: void 
 */ 
void InputData(void)  
{  
     printf("Input year(Press Enter to end input):");  
     if (scanf("%d", &year) != 1 || (year < 1000))  
     {  
                     puts("Input year error");  
                     getchar();  
                     exit(0);  
     }  
     leap = LeapYear(year);  
        
     printf("Input month:");  
     if (scanf("%d", &month) != 1 || month<1 || month>12)  
     {  
                     puts("Input month error!");  
                     getchar();  
                     exit(0);  
     }  
       
     printf("Input Day:");  
     if (scanf("%d", &day) != 1 || day<1 || day>Amonth[leap][month])  
     {  
                     puts("Input day error!");  
                     getchar();  
                     exit(0);  
     }  
       
     printf("Input Hour:");  
     if (scanf("%d", &hour) != 1 || hour<0 || hour>59)  
     {  
                     puts("Input hour error!");  
                     getchar();  
                     exit(0);  
     }  
       
     printf("Input Minute:");  
     if (scanf("%d", &minute) != 1 || minute<0 || minute>59)  
     {  
                     puts("Input minute error!");  
                     getchar();  
                     exit(0);  
     }  
       
     printf("Input Second:");  
     if (scanf("%d", &second) != 1 || second<0 || second>59)  
     {  
                     puts("Input second error!");  
                     getchar();  
                     exit(0);  
     }  
}  
 
/* 
 * Function Name: NextSec() 
 * Describle: Count the next second by the current time 
 * Paramete; void 
 * ReturnValue: void  
 */ 
void NextSec(void)  
{  
     if (++second == 60)  
     {  
                  second = 0;  
                  minute++;  
     }  
     if (minute == 60)  
     {  
                minute = 0;  
                hour++;  
     }   
     if (hour == 24)  
     {  
              hour = 0;  
              day++;  
     }  
     if (day == Amonth[leap][month]+1)  
     {  
             day = 1;  
             month++;  
     }  
     if (month == 13)  
     {  
               month = 1;  
               year++;  
     }  
       
     printf("The next second is %d-%d-%d %d:%d:%d/n/n",  
             year, month, day, hour, minute, second);  
}  
 
int main(void)  
{  
    InputData();  
      
    NextSec();  
      
    system("PAUSE");  
    return 0;