电子万年历

来源:互联网 发布:鸦片战争观后感知乎 编辑:程序博客网 时间:2024/04/27 20:20
 
  1. /************电子万年历******************
  2.  *功能:
  3.  *    可以查看当前日期的任意上一个月
  4.  *    和下一个月的日期列表
  5.  *
  6.  *输入:输入j,查看上一个月。
  7.  *      输入k,查看下一个月。
  8.  *      输入q,退出。
  9.  *
  10.  *作者:晓*
  11.  *****************************************/
  12. #include <iostream>
  13. #include <time.h>
  14. using namespace std;
  15. /*输入参数为某一年,判断是否为闰年,是返回1,否返回0*/
  16. #define check_runyear(year) (((year%4==0)&&(year%100!=0))||(year%400==0))
  17. /*计算这个月开始时需要输入的空格数*/
  18. #define get_space(day, week)  (7-(6-week+day)%7)
  19. /************************************
  20.  *功能:根据当前的日期计算上个月对应的星期
  21.  *参数:year --- 当前年号
  22.  *      month --- 前一个月
  23.  *      week  --- 当前日期的星期
  24.  *
  25.  *返回:前一个月与当前日期对应的星期
  26.  ************************************/
  27. #define get_upweek(year, month, week)   ((week-get_lastday(year, month)%7+7)%7)
  28. /************************************
  29.  *功能:根据当前的日期计算上个月对应的星期
  30.  *参数:year --- 当前年号
  31.  *      month --- 当月
  32.  *      week  --- 当前日期的星期
  33.  *
  34.  *返回:后一个月与当前日期对应的星期
  35.  ************************************/
  36. #define get_downweek(year, month, week) ((week+get_lastday(year, (month))%7)%7)
  37. int get_lastday(int year, int month);
  38. struct tm* get_currday();
  39. void print_date(int year, int month, int day, int curweek);
  40. /*返回某年某月的最后一天,即那个月总共有多少天*/
  41. int get_lastday(int year, int month)
  42. {
  43.     int all_month[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  44.     
  45.     if (check_runyear(year)&&(month==2))
  46.         return all_month[month-1]+1;
  47.     else
  48.         return all_month[month-1];
  49. }
  50. /*获取系统当前的年,月,日等信息*/
  51. struct tm* get_currday()
  52. {
  53.     time_t tmp_time;
  54.     struct tm* curdate;
  55.     tmp_time = time(NULL);
  56.     curdate = localtime(&tmp_time);
  57.     return curdate;
  58. }
  59. /*打印某年某月的万年历*/
  60. void print_date(int year, int month, int day, int curweek)
  61. {
  62.     int space = 0;
  63.     int allday = 0;
  64.     int i = 1;
  65.     space = get_space(day, curweek);
  66.     allday = get_lastday(year, month);
  67. /*******************************************************/
  68.     printf("%12d年%d月%d日/n", year, month, day);
  69.     printf("%16s%d/n/n""星期", curweek);
  70.     printf("  日  一  二  三  四  五  六/n");
  71.     printf("  --------------------------/n");
  72. /********************************************************/
  73.     for (i = 1; i<=space+allday; i++)
  74.     {
  75.         if (i>=1 && i<=space)
  76.             printf("    ");
  77.         else
  78.             printf("%4d", i-space);
  79.         if (i%7 == 0)
  80.             printf("/n");
  81.     }
  82.     printf("/n");
  83.     return ;
  84. }
  85. void test()
  86. {
  87.     int p_year, p_month, p_day, p_week;
  88.     struct tm *tmp;
  89.     char tmpchar;
  90.     tmp = get_currday();
  91.     p_year = tmp->tm_year+1900;
  92.     p_month = tmp->tm_mon+1;
  93.     p_day = tmp->tm_mday;
  94.     p_week = tmp->tm_wday;
  95.     
  96.     print_date(p_year, p_month, p_day, p_week);
  97.     while (1)
  98.     {
  99.         tmpchar = getchar();
  100.         switch (tmpchar)
  101.         {
  102.             case 'l':
  103.                 print_date(p_year, p_month, p_day, p_week);
  104.                 break;
  105.             case 'j':/*上一个月*/
  106.                 p_month--;
  107.                 if (p_month == 0)
  108.                 {
  109.                     p_year--;
  110.                     p_month = 12;
  111.                 }
  112.                 p_week = get_upweek(p_year, p_month, p_week);
  113.                 print_date(p_year, p_month, p_day, p_week);
  114.                 break;
  115.             case 'k':/*下一个月*/
  116.                p_week = get_downweek(p_year, p_month, p_week);
  117.                 p_month++;
  118.                 if (p_month == 13)
  119.                 {
  120.                     p_year++;
  121.                     p_month = 1;
  122.                 }                             
  123.                 print_date(p_year, p_month, p_day, p_week);
  124.                 break;
  125.             case 'q':return ;
  126.         }
  127.     }
  128.     return ;
  129. }
  130. int main(int argc, char *argv[])
  131. {
  132.     test();
  133.     return 0;
  134. }