在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 或




10:23 下午, 2010年10月1日Fanr /
3:15 下午, 2010年06月23日qlj /
哈
你这个让我想到了
linux下的 cal
7:43 下午, 2010年06月4日万戈 /
只有佩服的份啊
4:19 下午, 2010年06月4日Ludou /
将 #include “stdafx.h”
改成 #include
能够增加平台的适用性,可同时支持 GCC、VC 6.0等多平台。stdafx.h、int _tmain这些应该都是MS VS 2005以上的特色吧?
博主的编程风格很严谨,
0 == (week + i) % 7
这不是一般的新手能够注意的。
另外似乎应该注意一下输入数据的认证,不过写来练练手就不必了。
能够支持向下一个月、上一个月翻页,那会更好
4:42 下午, 2010年06月4日荒野无灯 /
嗯 ,有道理。
。只是当学习练手了,呵呵,如果真正要用到实际中,像scanf 和 printf这样不安全的函数都是尽量不用的。
时间仓促,没有修改