现在的位置: 首页 > 综合 > 正文

显示时间的程序

2013年01月24日 ⁄ 综合 ⁄ 共 1929字 ⁄ 字号 评论关闭
  1. /********************************************
  2. **
  3. **      Windows XP sp2, Micrsoft Visual C++ 6.0
  4. **      myDate.c
  5. **
  6. **********************************************/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <windows.h>
  11. typedef struct
  12. {
  13.      int year;
  14.      int month;
  15.      int day;
  16.      int hour;
  17.      int minute;
  18.      int second;
  19.      int week;
  20. }myDate;
  21. int leap(int year)
  22. {
  23.      if (year%4 == 0 && year%100 !=0 || year%400 == 0)
  24.      {
  25.           return 1;
  26.      }
  27.      return 0;
  28. }
  29. int main()
  30. {
  31.      int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  32.      char * weekday[] = {"星期日""星期一""星期二""星期三""星期四""星期五", "星期六"};
  33.      time_t t = time(NULL);
  34.      struct tm *cur = localtime(&t);
  35.      myDate Date = {cur->tm_year+1900, cur->tm_mon + 1, cur->tm_mday, cur->tm_hour, cur->tm_min, cur->tm_sec, cur->tm_wday};
  36.      printf("%4d年%02d月%02d日[%s]%02d时%02d分%02d秒/r", Date.year, Date.month, Date.day, weekday[Date.week], Date.hour, Date.minute, Date.second);
  37.      while(1)
  38.      {
  39.             Sleep(1000);
  40.             days[2] = (leap(Date.year) ? 28:29);
  41.             Date.second++;
  42.             if ( Date.second == 60)
  43.             {
  44.                   Date.second = 0;
  45.                   Date.minute++;
  46.             }
  47.             if (Date.minute == 60)
  48.             {
  49.                   Date.minute = 0;
  50.                   Date.hour++;
  51.             }
  52.             if (Date.hour == 24)
  53.             {
  54.                   Date.hour = 0;
  55.                   Date.week = (++Date.week)%7;
  56.                   Date.day++;
  57.             }
  58.             if (Date.day == (days[Date.month] + 1))
  59.             {
  60.                    Date.day = 1;
  61.                    Date.month++;
  62.             }
  63.             if (Date.month == 13)
  64.             {
  65.                    Date.month = 1;
  66.                    Date.year++;
  67.             }
  68.             printf("%4d年%02d月%02d日[%s]%02d时%02d分%02d秒/r", Date.year, Date.month, 
  69. Date.day, weekday[Date.week], Date.hour, Date.minute, Date.second);
  70.      }
  71.      return 0;
  72. }

抱歉!评论已关闭.