代码改变世界

简易电子时刻表

2013-05-20 17:24  夏洛克·福尔摩斯  阅读(246)  评论(0编辑  收藏  举报

  例如输入当前时间2006年12月20日19时17分59秒,计算后输出时间是2006年12月20日19时18分00秒。

  

  #include<stdio.h>

  #include<stdlib.h>

  #include<memory.h>

  • #include<windows.h>

    #include<string.h>

     

    int months[]={31,29,31,30,31,30,31,31,30,31,30,31};

    void process(int *year,int *month,int *day,int *hour,int *min,int *second);

    int main()

    {

          

           char year1[5];

           char month1[3];

           char day1[3];

           char hour1[3];

           char min1[3];

           char second1[3];

          

           printf("请输入当前时间(格式:2006年02月20日09时07分59秒)\n");

           char time[27];

           memset(time,0,sizeof(time));

           scanf("%s",time);

    //根据用户输入的格式提取字段

           sscanf(time,"%4s%*2s%2s%*2s%2s%*2s%2s%*2s%2s%*2s%2s",year1,month1,day1,hour1,min1,second1);

    //字符串转换为数字

           int year=atoi(year1);

           int month=atoi(month1);

           int day=atoi(day1);

           int hour=atoi(hour1);

           int min=atoi(min1);

           int second=atoi(second1);

     

           while(1)

           {

                  Sleep(1000);

                  system("cls");

                  process(&year,&month,&day,&hour,&min,&second);

           }

           return 0;

    }

    void process(int *year,int *month,int *day,int *hour,int *min,int *second)

    {

           int max_day=0;

           *second=*second+1;

           if(*second==60)

           {

                  *second=*second%60;

                  *min=*min+1;

                  if(*min==60)

                  {

                         *min=*min%60;

                         *hour=*hour+1;

                         if(*hour==24)

                         {

                                *hour=*hour%24;

                                if((*year%4==0 && *year%100!=0) || *year%400==0)

                                {

                                       switch(*month)

                                       {

                                              case 1:max_day=months[0];break;

                                              case 2:max_day=months[1];break;

                                              case 3:max_day=months[2];break;

                                              case 4:max_day=months[3];break;

                                              case 5:max_day=months[4];break;

                                              case 6:max_day=months[5];break;

                                              case 7:max_day=months[6];break;

                                              case 8:max_day=months[7];break;

                                              case 9:max_day=months[8];break;

                                              case 10:max_day=months[9];break;

                                              case 11:max_day=months[10];break;

                                              case 12:max_day=months[11];break;

                                       }

                                       if(*day==max_day)

                                       {

                                              *day=*day%max_day+1;

                                              if(*month==12)

                                              {

                                                     *month=*month%12+1;

                                                     *year=*year+1;

                                              }

                                              else

                                                     *month=*month+1;

                                       }

                                       else

                                       {

                                              *day=*day+1;

                                       }

                                }

                                else

                                {

                                       switch(*month)

                                       {

                                              case 1:max_day=months[0];break;

                                              case 2:max_day=28;break;

                                              case 3:max_day=months[2];break;

                                              case 4:max_day=months[3];break;

                                              case 5:max_day=months[4];break;

                                              case 6:max_day=months[5];break;

                                              case 7:max_day=months[6];break;

                                              case 8:max_day=months[7];break;

                                              case 9:max_day=months[8];break;

                                              case 10:max_day=months[9];break;

                                              case 11:max_day=months[10];break;

                                              case 12:max_day=months[11];break;

                                       }

                                       if(*day==max_day)

                                       {

                                              *day=*day%max_day+1;

                                              if(*month==12)

                                              {

                                                     *month=*month%12+1;

                                                     *year=*year+1;

                                              }

                                              else

                                                     *month=*month+1;

                                       }

                                       else

                                       {

                                              *day=*day+1;

                                       }

                                }

                         }

                  }

           }

    printf("%d年%d月%d日%d时%d分%d秒\n",*year,*month,*day,*hour,*min,*second);

    }