C语言写的日历程序(学生作品展示)
#include <stdio.h>
#include <stdlib.h>
int main()
{
/*定义部分*/
//定义年月(a,b)
int nYear,nMonth,nWeek,d,e,nDays,s;
printf("请输入年份:");
scanf("%d",&nYear);
//闰年情况
if((nYear % 4==0 && nYear % 100!=0) || (nYear % 400==0))
{
printf("输入的年份为闰年\n");
d=0;
}
//平年情况
else
{
printf("输入的年份为平年\n");
d=1;
}
printf("请输入月份:");
scanf("%d",&nMonth);
//闰年
if(d==0)
{
switch(nMonth)
{
case 1:e=5,nDays=31;
break;
case 2:e=1,nDays=29;
break;
case 3:e=2,nDays=31;
break;
case 4:e=5,nDays=30;
break;
case 5:e=0,nDays=31;
break;
case 6:e=3,nDays=30;
break;
case 7:e=5,nDays=31;
break;
case 8:e=1,nDays=31;
break;
case 9:e=4,nDays=30;
break;
case 10:e=6,nDays=31;
break;
case 11:e=2,nDays=30;
break;
case 12:e=4,nDays=31;
break;
default:
printf("非法的月份\n"),exit (0);
}
}
//平年
if(d==1)
{
switch(nMonth)
{
case 1:e=6,nDays=31;
break;
case 2:e=2,nDays=28;
break;
case 3:e=2,nDays=31;
break;
case 4:e=5,nDays=30;
break;
case 5:e=0,nDays=31;
break;
case 6:e=3,nDays=30;
break;
case 7:e=5,nDays=31;
break;
case 8:e=1,nDays=31;
break;
case 9:e=4,nDays=30;
break;
case 10:e=6,nDays=31;
break;
case 11:e=2,nDays=30;
break;
case 12:e=4,nDays=31;
break;
default:
printf("非法的月份\n"),exit (0);
}
}
int i=nYear - (nYear / 100 * 100);
s = (((i/4)+i)%7+e+1)%7;
switch(s)
{
case 0:nWeek=7;//周日
break;
case 1:nWeek=1;//周一
break;
case 2:nWeek=2;//周二
break;
case 3:nWeek=3;//周三
break;
case 4:nWeek=4;//周四
break;
case 5:nWeek=5;//周五
break;
case 6:nWeek=6;//周六
break;
}
/*打印部分*/
char ci[7] = " ";//4个空格
int ni[31];
int g , k = 1;
for(g = 0 ; g < 31 ; g++)
{
ni[g] = g + 1;
}
printf("\t %4d年%2d月\n一 二 三 四 五 六 日\n",nYear,nMonth);
for(g = 1 ; g < nWeek ; g++)
{
printf("%s",ci);
k++;
}
for(g = 0 ; g < nDays ; g++)
{
printf("%-6d",ni[g]);
if(k % 7 == 0) printf("\n");
k++;
}
printf("\n");
system("pause");
return 0;
}