C语言实现万年历语言实现万年历
C语言实现的万年历显示,按下上下左右按键来更换日期和年份,供大家参考,具体内容如下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<conio.h>
typedef struct today
{
int day;
int month;
int year;
} today;
int day_cankao[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
char *week[]=
{
"Sun","Mon","Tue","Wen","Thu","Fir","Sat"
};
struct tm *todayuse;//struct tm定义c语言中time的结构体
today today_current;
int getweekday(today today_usenow)
{
// w = y + [y/4] + [ c/4 ] – 2c+ [13 * (m+1) / 5] + d – 1
//其中,c是年份的前两位,y是年份的后两位,m是月份,d是日期,这里需要注意的是,如果是1月和2月,c和y需要按照上
一年来取值。
int w=0;
int year=today_usenow.year;
int month=today_usenow.month;
if(today_usenow.month==1 || today_usenow.month==2)
{
month+=12;
year--;
}
int y=year%100;
int c=year/100;
int m=month;
w=y + y/4 + c/4 - 2*c+ 26* (m+1) / 10 + today_usenow.day -1;
while(w<0)
{
w+=7;
}
return (w%7);
}
int is_leap(int year)
{
if( (year%4==0 && year%100!=0) || (year%400==0))
{
return 1;
}
else
return 0;
}
int getmonthdays(int year,int month)
{
return day_cankao[is_leap(year)][month];
}
void print_calendar(today today_usenow)
{
printf("---------------------------");