努力这么久了..晒一下自己写的日历,VC下通过
程序代码:
#include "stdio.h" #include "conio.h" #include "time.h" #include "stdlib.h" #define LEFT 0x4b #define RIGHT 0x4d #define UP 0x48 #define DOWN 0x50 #define ESC 0x1b struct get_time { int year; int month; int date; }days,days1; int date_table[31]={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}; /*获取年月日*/ int get_date() { struct tm *y; time_t s; time(&s); y=gmtime(&s); days.date = y->tm_mday; days.month = y->tm_mon+1; days.year = y->tm_year+1900; days1=days; return 0; } /*判断闰年是否闰年*/ int get_leap(int _year) { if( (_year%4==0) && (_year%100 != 0) || (_year%400 == 0)) return 1; return 0; } /*求出该日期是该年的多少天*/ int get_days(int _year , int _mon , int _date) { int day_table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}, i=0,total=0; for(i=0;i<_mon;i++) total+=day_table[i]; return total+_date+get_leap(_year); } /*获取星期*/ int get_week(int _year, int _month , int _date) { return ( (_year-1+(_year-1)/4-(_year-1)/100+ (_year-1)/400+get_days(_year,_month,_date) )%7 ); } /*获取该月份的天数*/ int month_of_day(int _y , int _d) { switch(_d) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11:return 30; } return 28+get_leap(_y); } //打印星期 void print_week() { switch(get_week(days1.year, days1.month , days.date)) { case 1:printf("星期一\n");break; case 2:printf("星期二\n");break; case 3:printf("星期三\n");break; case 4:printf("星期四\n");break; case 5:printf("星期五\n");break; case 6:printf("星期六\n");break; case 0:printf("星期日\n");break; } } /*打印日历*/ void print_date(int year , int month, int date ) { int k,i,j; struct get_time *p; p=&days1; system("cls"); printf("\t\t%d年%d月%d日",year,month,p->date); print_week(); printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六\n"); printf("-------------------------------------------------------\n"); k=month_of_day(year,month); j=get_week(year,month,1); for(i=0;i<k;i++) { while(j--> 0) printf("\t"); printf("%4d\t",date_table[i]); if(get_week(year,month,date_table[i]) == 6) { printf("\n"); } } printf("\n"); printf("-------------------------------------------------------\n"); printf("按方向键前进后退年月 退出按ESC\n"); } int main() { int key; int y=1; struct get_time *p,*p1; p=&days1; p1=&days; get_date(); //初始化 print_date(p->year , p->month, p->date ); do { //key=bioskey(0); key=getch(); switch(key) { case LEFT: p->month--; p->month<1?p->month=12-p->month,p->year--:p->month; print_date(p->year,p->month ,1); printf("\n"); break; case RIGHT: p->month++; p->month>12?p->month=p->month-12,p->year++:p->month; print_date(p->year,p->month , 1); printf("\n"); break; case UP: p->year++; print_date(p->year,p->month , 1); printf("\n"); break; case DOWN: p->year--; print_date(p->year,p->month , 1); printf("\n"); break; } }while(key != ESC); system("cls"); printf("\n\n\n\n\n\n\n\n\t\t\t\t谢谢使用"); _sleep(1000); return 0; }
有很多的不足..不知道怎么弄..前半个月以前写的