日历C代码.docx
- 文档编号:11129522
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:24
- 大小:18.95KB
日历C代码.docx
《日历C代码.docx》由会员分享,可在线阅读,更多相关《日历C代码.docx(24页珍藏版)》请在冰豆网上搜索。
日历C代码
/*加载头文件*/
#include
#include
#include
#include
/*定义常量*/
/*左移键*/
#defineLEFT0x4b00
/*右移键*/
#defineRIGHT0x4d00
/*下移键*/
#defineDOWN0x5000
/*上移键*/
#defineUP0x4800
/*向上翻页移键*/
#definePAGEUP0x4900
/*向下翻页移键*/
#definePAGEDOWN0x5100
/*小写字母q*/
#defineQLOWER0x1071
/*大写字母Q*/
#defineQUPPER0x1051
/*空格键*/
#defineSPACE0x3920
/*Escape键*/
#defineESC0x011b
/*定义全局变量*/
structdatesysTime;
intcurrentYear;
intcurrentMonth;
intcurrentDay;
intn_currentMon;
intn_lastMon;
/*-----------自定义函数-------------------------*/
voidcheckDate();
intisLeapyear(intyear);
intgetWeek(intyear,intmonth,intday);
voidprintSpace(intn);
voidprintSeparator();
voidprintUsage();
voidprintWeek();
voidprintWeek2(intweek);
voidshowCalendar(intyear,intmonth,intday);
voidgetKeyValue();
/*--------------------------------------------*/
/*检查日期有效性函数*/
voidcheckDate()
{
/*如果给定的日期中年份部分为负数或者0*/
if(currentYear<=0)
{
gotoxy(1,27);
printf("Theyearshouldbeapositivenumber!
\n");
gotoxy(1,28);
printf("Pressanykeytocontinue......");
getchar();
getchar();
/*以当前系统时间重新赋值*/
currentYear=sysTime.da_year;
currentMonth=sysTime.da_mon;
currentDay=sysTime.da_day;
}
/*如果给定的日期中月份部分为负数,为0或者大于12*/
if(currentMonth>12||currentMonth<1)
{
gotoxy(1,27);
printf("Themonthshouldbeanumberbetween1and12!
\n");
gotoxy(1,28);
printf("Pressanykeytocontinue......");
getchar();
getchar();
/*以当前系统时间重新赋值*/
currentYear=sysTime.da_year;
currentMonth=sysTime.da_mon;
currentDay=sysTime.da_day;
}
/*如果给定的日期中天数部分为负数,为0或者大于31*/
if(currentDay>31||currentDay<1)
{
gotoxy(1,27);
printf("Thedayshouldbeanumberbetween1and31!
\n");
gotoxy(1,28);
printf("Pressanykeytocontinue......");
getchar();
getchar();
/*以当前系统时间重新赋值*/
currentYear=sysTime.da_year;
currentMonth=sysTime.da_mon;
currentDay=sysTime.da_day;
}
}
/*判断是否是闰年函数*/
intisLeapyear(intyear)
{
/*闰年的判断:
(1)year被4整除,并且year不被100整除
或
(2)year被4整除,并且year被400整除*/
if(year%4==0&&year%100||year%400==0)
return1;
else
return0;
}
/*根据给定日期计算星期函数*/
intgetWeek(intyear,intmonth,intday)
{
intleapFlag;
intweek;
inti;
/*该数组用于保存每个月的天数*/
inta[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
intcount=0;
/*判断给定年份是否是闰年*/
leapFlag=isLeapyear(year);
/*如果是闰年则2月份的日期应该29天*/
if(leapFlag==1)
a[2]++;
/*计算从给定年份的1月1日到给定月份的1号之间的天数*/
for(i=1;i { count+=a[i]; } /*计算从给定年份的1月1日到给定日期之间的天数*/ count+=day; /*计算星期*/ week=(year+1+(year-1)/4+(year-1)/100+(year-1)/400+count)%7; returnweek; } /*输出指定个数个空格函数*/ voidprintSpace(intn) { inti; for(i=0;i printf(""); } /*输出分隔线函数*/ voidprintSeparator() { inti; for(i=0;i<38;i++) { printf("="); } printf("\n"); } /*输出使用方法函数*/ voidprintUsage() { gotoxy(45,3); /*设定字体颜色*/ textcolor(YELLOW); /*送格式化输出至屏幕*/ cprintf("-----------Usage-----------"); gotoxy(45,5); textcolor(YELLOW); cprintf("YEAR"); gotoxy(50,5); printf("Upkey()toincrease;\n"); gotoxy(50,7); printf("Downkey()todecrease.\n"); gotoxy(45,9); textcolor(YELLOW); cprintf("MONTH: "); gotoxy(50,9); printf("Rightkey()toincrease;\n"); gotoxy(50,11); printf("Leftkey()todecrease.\n"); gotoxy(45,13); textcolor(YELLOW); cprintf("DAY"); gotoxy(50,13); printf("PageDownkeytoincrease;\n"); gotoxy(50,15); printf("PageUpkeytodecrease.\n"); gotoxy(45,17); textcolor(YELLOW); cprintf("QUERY"); gotoxy(50,17); printf("Q/qkey\n"); textcolor(YELLOW); gotoxy(45,19); cprintf("RESET"); gotoxy(50,19); printf("SPACEkey.\n"); gotoxy(45,21); textcolor(YELLOW); cprintf("EXIT"); gotoxy(50,21); printf("Esckey\n"); gotoxy(59,5); /*设置寄存器,表示在指定位置输出一次指定的字符*/ _CX=01; _AH=0xa; /*指定输出字符为向上的箭头*/ _AL=24; /*产生一次中断,输出向上的箭头*/ geninterrupt(0x10); gotoxy(61,7); _CX=01; _AH=0xa; /*指定输出字符为向下的箭头*/ _AL=25; geninterrupt(0x10); gotoxy(62,9); _CX=01; _AH=0xa; _AL=26; /*指定输出字符为向右的箭头*/ geninterrupt(0x10); gotoxy(61,11); _CX=01; _AH=0xa; /*指定输出字符为向左的箭头*/ _AL=27; geninterrupt(0x10); /*还原字体颜色*/ textcolor(7); } /*根据系统日期输出星期函数*/ voidprintWeek() { intday; /*根据系统日期获得星期*/ day=getWeek(currentYear,currentMonth,currentDay); textcolor(11); /*输出星期,0表示星期天,1表示星期一......6表示星期六*/ if(day==0) cprintf("%d-%d-%d,Sunday! ",currentYear,currentMonth,currentDay); if(day==1) cprintf("%d-%d-%d,Monday! ",currentYear,currentMonth,currentDay); if(day==2) cprintf("%d-%d-%d,Tuesday! ",currentYear,currentMonth,currentDay); if(day==3) cprintf("%d-%d-%d,Wednesday! ",currentYear,currentMonth,currentDay); if(day==4) cprintf("%d-%d-%d,Thursday! ",currentYear,currentMonth,currentDay); if(day==5) cprintf("%d-%d-%d,Friday! ",currentYear,currentMonth,currentDay); if(day==6) cprintf("%d-%d-%d,Saturday! ",currentYear,currentMonth,currentDay); textcolor(7); } /*输出指定星期函数*/ voidprintWeek2(intweek) { if(week==0) cprintf(",Sunday"); if(week==1) cprintf(",Monday"); if(week==2) cprintf(",Tuesday"); if(week==3) cprintf(",Wendesday"); if(week==4) cprintf(",Thursday"); if(week==5) cprintf(",Friday"); if(week==6) cprintf(",Staturday"); } /*显示日历函数*/ voidshowCalendar(intyear,intmonth,intday) { inti; intj; /*输出的日期*/ intoutDay; intleapFlag; /*本月第一个星期中,在上月的天数*/ intdayLastMon; intweek; /*该数组用于保存每个月的天数*/ inta[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; outDay=0; dayLastMon=0; week=0; /*判断给定年份是否是闰年*/ leapFlag=isLeapyear(year); /*如果是闰年则2月应该是29天*/ if(leapFlag==1) { a[2]++; } /*如果给定的日期中,天数大于该月的最大天数*/ if(day>a[month]) { printf("Thenumberofthismonth'sdayis%datmost! \n",a[month]); printf("Pressanykeytocontinue......\n"); getchar(); getchar(); /*以当前系统时间重新赋值*/ year=sysTime.da_year; month=sysTime.da_mon; day=sysTime.da_day; currentYear=sysTime.da_year; currentMonth=sysTime.da_mon; currentDay=sysTime.da_day; } /*当前月的天数*/ n_currentMon=a[month]; /*上一月的天数*/ n_lastMon=a[month-1]; /*根据给定日期,获取给定月份1号的星期*/ week=dayLastMon=getWeek(year,month,1); /*清除文本模式窗口*/ clrscr(); printf("\nThecalendarof%d",year); textcolor(10); /*显示给定年份是否是闰年*/ if(leapFlag==1) cprintf("[leapyear]",year); if(leapFlag==0) cprintf("[notleapyear]",year); printf("\n"); textcolor(7); printSeparator(); textcolor(YELLOW); /*输出月份提示,1-12分别表示1月到12月*/ switch(month) { case1: cprintf("January1"); break; case2: cprintf("February2"); break; case3: cprintf("March3"); break; case4: cprintf("April4"); break; case5: cprintf("May5"); break; case6: cprintf("June6"); break; case7: cprintf("July7"); break; case8: cprintf("August8"); break; case9: cprintf("September9"); break; case10: cprintf("October10"); break; case11: cprintf("Nevember11"); break; case12: cprintf("December12"); break; } textcolor(7); printf("\n\n"); textcolor(13); cprintf("SunMonTueWedThuFriSat"); textcolor(7); printf("\n\n"); /*因为一个月中最多跨度6个星期,所以循环从0到5*/ for(i=0;i<6;i++) { /*如果是该月的第一个星期*/ if(i==0) { /*第一个星期中还要7-dayLastMon天本月,有dayLastMon天在上个月, 所以输出dayLastMon*4个空格符,每个日期在日历中占用4个空格*/ printSpace(dayLastMon*4); /*剩余在本月的第一个星期中的天数为7-dayLastMon*/ for(j=0;j<7-dayLastMon;j++) { /*到达day的前一天时,输出++outDay*/ if(outDay==day-1) { /*用黄色字体突出显示*/ textcolor(YELLOW); cprintf("[%d]",++outDay); textcolor(7); } /*到达day当天时,并且不是星期天*/ elseif(outDay==day&&week! =0) { printf("%3d",++outDay); } /*到达day当天时,并且是星期天*/ else { printf("%4d",++outDay); } /*计算当天的星期,如果前一天是星期六(6),则当天是星期天(0), 其他情况则直接星期加1*/ week=(week<6)? week+1: 0; } printf("\n\n"); } /*如果不是该月的第一个星期*/ else { /*则输出该星期中的7天*/ for(j=0;j<7;j++) { /*输出的天数小于该月的最大天数*/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 日历 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)