#include<stdio.h>
#include<conio.h>
void RiLi(int year,int mouth);
void print(int i,int m);
void main()
{
int year,mouth;
char cha;
while(1)
{
puts("输入年份:");
scanf("%d",&year);
if(year<=0)
puts("输入不合法!!");
else
break;
}
while(1)
{
puts("输入月份:");
scanf("%d",&mouth);
if(mouth<1||mouth>12)
puts("输入不合法!!");
else
break;
}
system("cls");
RiLi(year,mouth);
fflush(stdin);
while(1)
{
printf("********键入<进入上月*********/n********键入>进入下月*********/n*********键入#结束程序*********/n");
cha=getchar();
fflush(stdin);
if(cha=='<')
{
system("cls");
--mouth;
if(mouth<1)
{
mouth=12;
year-=1;
}
RiLi(year,mouth);
}
else if(cha=='>')
{
system("cls");
++mouth;
if(mouth>12)
{
mouth=1;
year+=1;
}
RiLi(year,mouth);
}
else if(cha=='#')
{
exit(0);
}
}
}
void RiLi(int year,int mouth)
{
int sum=1;//总天数
int y1=0;//闰年年份计数器
int y2=0;//平年年份计数器
int b=0;//闰年标识符
int i;
int m1=0;//月计数器(31天)
int m2=0;//月计数器(30天)
int m3=0;//月计数器(2月28)
int m4=0;//月计数器(2月29)
int m;
if(year%4==0)//判断接收的年份是平年或闰年然后再相应的计数器自减
{
if(year%100!=0)
{
y1-=1;
b=1;
}
else
{
if(year%400==0)
{
y1-=1;
b=1;
}
else
y2-=1;
}
}
else
{
y2-=1;
}
for(i=1;i<=year;i++)
{
if(i%4==0)
{
if(i%100!=0)
{
y1++;//计算该年份之前有多少闰年
}
else
{
if(i%400==0)
y1++;
else
y2++;
}
}
else
{
y2++;//计算该年份之前有多少平年
}
}
if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12)//判断接收的月份是30天或31天或28天或 29天然后再相应的计数器自减
{
m1-=1;
}
else if(mouth==2)
{
if(b==1)
m4-=1;
else
m3-=1;
}
else
{
m2-=1;
}
for(i=1;i<=mouth;i++)//计算接收的月份之前30,31,29,28各几天
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
m1++;
}
else if(i==2)
{
if(b==1)
m4++;
else
m3++;
}
else
{
m2++;
}
}
sum+=y1*366+y2*365+m1*31+m2*30+m3*28+m4*29;//计算接收日期经过多少天
m=sum%7;//计算这月一号是星期几
if(m==0)
{
m=7;
}
printf("%d年%d月的日历:/n",year,mouth);
puts("__________________________");
puts("一 二 三 四 五 六 日");
puts("__________________________");
for(i=1;i<m;i++)
{
printf(" ");
}
if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12)
{
for(i=1;i<=31;i++)
print(i,m);
}
else if(mouth==2)
{
if(b==1)
{
for(i=1;i<=29;i++)
print(i,m);
}
else
{
for(i=1;i<=28;i++)
print(i,m);
}
}
else
{
for(i=1;i<=30;i++)
print(i,m);
}
puts("/n__________________________");
}
void print(int i,int m)
{
if(i<=9)
{
printf("%d ",i);
if((m-1+i)%7==0)
printf("/n");
}
else
{
printf("%d ",i);
if((m-1+i)%7==0)
printf("/n");
}
}