您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页C语言万年历(代码+说明)

C语言万年历(代码+说明)

来源:化拓教育网


/*本程序在 Microsoft Visual Studio 2010 旗舰版中测试通过*/

#include \"stdio.h\"

#include \"stdlib.h\"

#include \"time.h\"

#include \"conio.h\"

#define KEYUP 72//宏定义键盘的键值(↑)

#define KEYDOWN 80//宏定义键盘的键值(↓)

#define KEYLEFT 75//宏定义键盘的键值(←)

#define KEYRIGHT 77//宏定义键盘的键值(→)

//函数声明部分

const int isLeap(int year);

const int getMonthDays(int year,int month);

const int yearDays(int year);

void printCalendar(int year,int month);

void main(){

int year,month;

int action = 0;

//获取本地当前的年份和月份

time_t timep;

struct tm *p;

time(&timep);

p = localtime(&timep);

year = p->tm_year+1900;//获取本地当前的年份

month = p->tm_mon + 1;//获取本地当前的月份

while(1){

printf(\"\\ %d 年%d 月\\n\

printCalendar(year,month);

action = getch();

system(\"cls\");

switch(action){

case KEYUP:

year++;

break;

case KEYDOWN:

year--;

break;

case KEYLEFT:

month--;

if(month < 1){

month = 12;

}

break;

case KEYRIGHT:

month++;

if(month > 12){

month = 1;

}

break;

}

}

}

//判断 year 是否是润年 返回 1 为闰年

const int isLeap(int year){

if(year%4==0&&year%100!=0||year%400==0){

return 1;

}

else{

return 0;

}

}

/*计算 year 年的 month 月是多少天

*返回值:整型,天数

*/

const int getMonthDays(int year,int month){

switch(month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

return 31;

break;

case 4:

case 6:

case 9:

case 11:

return 30;

break;

case 2:

if(isLeap(year)){

return 29;

}else{

return 28;

}

break;

default:

return 0;

break;

}

}

//计算 year 年的天数

const int yearDays(int year){

if(isLeap(year)){

return 366;

}else{

return 365;

}

}

/*判断 year 年 month 月 day 天时星期几

*返回值:0,1,2,3,4,5,6

*/

const int isWeek(int year,int month,int day){

int days = 0;

int i;

//计算前 year 年有多少天

for(i=1;idays = days + yearDays(i);

}

//计算 year 年的前 month 个月有多少天

for(i=1;idays = days + getMonthDays(year,i);

}

//从公元 1 年

days = days + day;

return days%7;

}

//按日历的格式打印 year 年 month 月的日历

void printCalendar(int year,int month){

const char *week[]

= {\"日\一\二\三\四\五\六\

int i;int row = 0;

for(i=0;i<7;i++){

printf(\"%s\\

}

printf(\"\\n\");

//判断 year 年 month 月 1 日时星期几

for(i=0;iprintf(\"\\");

}

for(i=0;iprintf(\"%d\\

//如果是星期六就换行打印日期

if(isWeek(year,month,i+1) == 6){

row ++;

if(row == 2){

printf(\" ↑\");

}

if(row == 3){

printf(\"← →\");

}

if(row == 4){

printf(\" ↓\");

}

printf(\"\\n\\n\");

}

}

printf(\"\\n\");

}

CreateDBW制作2012年12月7日

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务