第2个回答 2009-12-13
//-------------系统库定义-------------------
#include "stdio.h"
#include "BIOS.h"
#include "dos.h"
#define X " Sun Mon Tue Wed Thu Fri Sat"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define PGUP 0x4900
#define PGDW 0x5100
#define ESC 0x011b
#define QUERY 0x1071
#define QUERYD 0x1051
//--------------------------------------------
struct date dm ;//时间结构定义
int Cyear,Cmonth,Cday,Cmonnum,Lmonnum ;//全局变量
int isleap(int year)//判断闰年
{
if(year%4==0&&year%100||year%400==0)
return 1 ;
else
return 0 ;
}
int getday(int year,int month,int day)//计算X年X月X日是星期几
{
int flag,s,i ;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31} ;
int cont=0 ;
flag=isleap(year);
if(flag==1)
a[2]++;
for(i=1;i<month;i++)
{
cont=cont+a[i];
}
cont=cont+day ;
s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont ;
return s%7 ;
}
void printspace(int n)//输出空格
{
int i ;
for(i=0;i<n;i++)printf(" ");
}
void spacer()//输出分隔符(分隔线)
{
int i ;
for(i=0;i<30;i++)
{
printf("=");
}
printf("\n");
}
void keyinfo()//输出键盘操作信息
{
printf("\n-------Operation manual-------\n");
printf("Year:Up key and Down key\n");
printf("Month:Left key and Right key\n");
printf("Day:PageUp key and PageDown key\n");
printf("Query date of the calendar:Q key\n");
printf("Exit calendar:Esc key\n");
}
void week()//输出当前日期是星期几
{
int day ;
day=getday(Cyear,Cmonth,Cday);
if(day==0)
printf("\n%d-%d-%d is Sunday!\n",Cyear,Cmonth,Cday);
if(day==1)
printf("\n%d-%d-%d is Monday!\n",Cyear,Cmonth,Cday);
if(day==2)
printf("\n%d-%d-%d is Tuesday!\n",Cyear,Cmonth,Cday);
if(day==3)
printf("\n%d-%d-%d is Wednesday!\n",Cyear,Cmonth,Cday);
if(day==4)
printf("\n%d-%d-%d is Thursday!\n",Cyear,Cmonth,Cday);
if(day==5)
printf("\n%d-%d-%d is Friday!\n",Cyear,Cmonth,Cday);
if(day==6)
printf("\n%d-%d-%d is Saturday!\n",Cyear,Cmonth,Cday);
}
void calendar(int year,int month,int day)//显示日历
{
int i,j,d,m,flag,week;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
m=0,d=0,week=0;
Cmonnum=a[month];//当前月的天数
Lmonnum=a[month-1];//上一月的天数
week=d=getday(year,month,1);//取得X年X月的1日是星期几
flag=isleap(year);
if(flag==1)
{
a[2]++;
}
clrscr();
printf("\n The calendar of year %d\n",year);
spacer();
switch(month)
{
case 1 :
printf(" January 1 ");
break ;
case 2 :
printf(" February 2 ");
break ;
case 3 :
printf(" March 3 ");
break ;
case 4 :
printf(" April 4 ");
break ;
case 5 :
printf(" May 5 ");
break ;
case 6 :
printf(" June 6 ");
break ;
case 7 :
printf(" July 7 ");
break ;
case 8 :
printf(" August 8 ");
break ;
case 9 :
printf(" September 9 ");
break ;
case 10 :
printf(" October 10 ");
break ;
case 11 :
printf(" Nevember 11 ");
break ;
case 12 :
printf(" December 12");
break ;
}
printf("\n");
printf(X);
printf("\n");
for(i=0;i<6;i++)
{
if(i==0)//如果是第一个星期时,执行!
{
printspace(d*4);
for(j=0;j<7-d;j++)
{
if(m==day-1)//m到达day的前一天时,输出[%d]
{
printf(" [%d]",++m);
}
else if(m==day && week!=0)//m到达当天时的,输出%3d
{
printf("%3d",++m);
}
else
{
printf("%4d",++m);
}
week=(week<6)?week+1:0;//计算当天是星期几
}
printf("\n");
}
else//不是第一个星期时,执行!
{
for(j=0;j<7;j++)
{
if(m<a[month])
{
if(m==day-1)//m到达day的前一天时,输出[%d]
{
if(m<9)//代表只有一位数的数字如9,8,7.....等。
printf(" [%d]",++m);
else//代表只有两位数的数字如10,11,12.....等。
printf(" [%d]",++m);
}
else if(m==day && week!=0)
{
printf("%3d",++m);
}
else
{
printf("%4d",++m);
}
}
week=(week<6)?week+1:0;//计算当天是星期几
}
printf("\n");
if(m==a[month])break ;
}
}
spacer();
if(flag==1)
printf("\nThe year %d is leap year!\n",year);
if(flag==0)
printf("\nThe year %d is not leap year!\n",year);
week();
printf("\nThe current date is: %d-%d-%d\n",dm.da_year,dm.da_mon,dm.da_day);
keyinfo();
}
//keyboard Operation
void key()
{
int key ;
Cyear=dm.da_year,Cmonth=dm.da_mon,Cday=dm.da_day ;
calendar(Cyear,Cmonth,Cday);
while(1)
{
key=bioskey(0);
if(key==RIGHT)
{
if(Cmonth<12&&Cmonth>=1)
{
Cmonth++;
}
else
{
Cyear++;
Cmonth=1 ;
}
}
if(key==LEFT)
{
if(Cmonth<=12&&Cmonth>1)
{
Cmonth--;
}
else
{
Cyear--;
Cmonth=12 ;
}
}
if(key==UP)
{
Cyear++;
}
if(key==DOWN)
{
Cyear--;
}
if(key==PGUP)
{
if(Cday!=1)
{
Cday--;
}
else if(Cday==1&&Cmonth==1)
{
Cyear--;
Cmonth=12 ;
Cday=31 ;
}
else
{
Cmonth--;
Cday=Lmonnum ;
}
}
if(key==PGDW)
{
if(Cmonnum!=Cday)
{
Cday++;
}
else if(Cmonnum==Cday&&Cmonth==12)
{
Cyear++;
Cmonth=1 ;
Cday=1 ;
}
else
{
Cmonth++;
Cday=1 ;
}
}
if(key==QUERY||key==QUERYD)
{
printf("Input date format(YYYY,MM,DD):");
scanf("%d,%d,%d",&Cyear,&Cmonth,&Cday);
}
if(key==ESC)break ;
calendar(Cyear,Cmonth,Cday);
}
}
void main()
{
clrscr();
getdate(&dm);
key();
}