简单的月历程序

在MS vs 2008 下编译通过。

今天的音乐:
类似爱情——萧亚轩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// yueli.cpp : 定义控制台应用程序的入口点。
/*
月历
输入年、月,输出月历。

*/


#include <stdio.h>
#include <string.h>
#include <tchar.h>
#include <time.h>
#include <malloc.h>

int
day2week(int year,int month,int day);

int
is_leap_year(int year);

int
main(int argc, char *argv[])
{

int year,mon;
//这个月的第一天是星期几
int week;
        printf("请输入年 月\n");
    scanf("%4d %2d",&year,&mon);
    week=day2week(year,mon,1);
    //printf("%d\n",week);

    //这个月有多少天
    int month_len;
    switch(mon)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        month_len=31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        month_len=30;
        break;
    case 2:
        if(is_leap_year(year))
            month_len=29;
        else
            month_len=28;
        break;
    }

    printf("##########月历###########\n");
    printf("                 %d年%d月\n",year,mon);
    printf("日  一  二  三  四  五  六\n");

    int i;
    for(i=0;i<week;i++)
        printf("    ");

    for(i=1;i<=month_len;i++)
    {
        printf("%02d  ",i);
        if( 0 == (week + i) % 7 )
            printf("\n");
    }
   
    printf("\n#########################\n");

    return 0;
}



/*
C语言计算某年某月某日是星期几的函数 (注:由于 mktime函数的限制,目前只能计算1900年以后到2038年之间的)
By 荒野无灯
*/

int day2week(int year,int month,int day)
{
  struct   tm   tm1,*tm2;  
  time_t timep;

 int week;
  tm1.tm_year= year-1900;  
  tm1.tm_mon=month-1;  
  tm1.tm_mday=day;  
  tm1.tm_hour=12;  
  tm1.tm_min=0;  
  tm1.tm_sec=0;
  timep=mktime(&tm1);
  tm2=localtime(&timep);
  week=(int) (tm2->tm_wday);

  // 0 - 6

  return week;

}


//闰年判断
/*
 公历闰年的精确计算方法:(按一回归年365天5小时48分45.5秒)
  ①、普通年能被4整除的为闰年。(如2004年就是闰年,1901年不是闰年)
[地球公转示意图]
地球公转示意图
②、世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
  ③、对于数值很大的年份能整除3200,但同时又能整除172800则又是闰年.(如 172800年是闰年,86400年不是闰年)

  */


int
is_leap_year(int year)
{
    int is_leap=0;
    if( 0 == year % 400 )
        is_leap=1;
    else
        if(0 != year%100 && 0 == year%4 )
            is_leap=1;
    return is_leap;
}

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:

随机日志

回复 (5)

  1. Fanr  / 回复

    :evil: 嗯,值得一看,可惜啊,我是java方向的~~

  2. qlj  / 回复


    你这个让我想到了
    linux下的 cal

  3. 万戈  / 回复

    只有佩服的份啊

  4. Ludou  / 回复

    将 #include “stdafx.h”
    改成 #include

    能够增加平台的适用性,可同时支持 GCC、VC 6.0等多平台。stdafx.h、int _tmain这些应该都是MS VS 2005以上的特色吧?

    博主的编程风格很严谨,

    0 == (week + i) % 7

    这不是一般的新手能够注意的。

    另外似乎应该注意一下输入数据的认证,不过写来练练手就不必了。

    能够支持向下一个月、上一个月翻页,那会更好

    • 荒野无灯  / 回复

      嗯 ,有道理。 :mrgreen:
      时间仓促,没有修改 :smile: 。只是当学习练手了,呵呵,如果真正要用到实际中,像scanf 和 printf这样不安全的函数都是尽量不用的。

发表评论 修改评论取消编辑

允许使用的标签 - 您可以在评论中使用如下的 HTML 标签以及属性。

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

引用通告 (0)

› 尚无引用通告。

开灯