已经编译运行确认过。
#include
#include
int leap(int); /*判断是否为闰年*/
int md(int m,int y); /*返回本月的天数*/
void prtweek(void); /*输出表头*/
int fw(int m,int y); /*返回本月一号是星期几*/
void prtmonth(int m); /*输出月份表头*/
int main(void)
{
int y,m,i,test,week;
printf("请输入您要查询的年份: ");
scanf("%d",&y);
for (m=1; m<=12; m++) {
test=1;
prtmonth(m);
prtweek();
week=fw(m,y);
if (week!=6)
for (i=0; i<=week; i++) {
putchar('\t');
test++;
}
for (i=1; i<=md(m,y); i++,test++) {
printf("%d\t",i);
if (test%7==0) {
putchar('\n');
}
}
putchar('\n');
system("pause");//暂停,以便观察输出
}
return 0;
}
int leap(int y)
{
return (y%4==0&&y%100||y%400==0)?1:0;
}
int md(int m,int y)
{
int d;
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d=31;break;
case 4:
case 6:
case 9:
case 11:d=30;break;
case 2:d=leap(y)?29:28;break;
}
return d;
}
void prtweek(void)
{
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n","Sun","Mon","Tue","Wed","Thur","Fri","Sat");
}
int fw(int m,int y)
{
int f;
if (m<3) {
m+=12;
y--;
}
f=(1+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
return f;
}
void prtmonth(int m)
{
switch (m) {
default:
case 1:puts("===January===");break;
case 2:puts("===February===");break;
case 3:puts("===March===");break;
case 4:puts("===April===");break;
case 5:puts("===May===");break;
case 6:puts("===June===");break;
case 7:puts("===July===");break;
case 8:puts("===August===");break;
case 9:puts("===September===");break;
case 10:puts("===October===");break;
case 11:puts("===November===");break;
case 12:puts("===December===");break;
}
}
#include
#define YES 1 // 定义符号常数"是"
#define NO 0 // 定义符号常数"否"
// 函数 isleap(): 判断某年是否闰年
int isleap(int year)
{
int leap = NO;
if(year%4==0 && year%100!=0 || year%400==0)
leap = YES;
return leap;
}
// 函数 week_of_newyears_day(): 求元旦是星期几
int week_of_newyears_day(int year)
{
int n = year-1900;
n = n+(n-1)/4+1; // 要n-1是因为1904是闰年,但是只有在1905年才能才能将其2月的多的一天加上!!
n = n%7; // 多的几天要以7作为循环!!!!
return n;
}
// 主函数: 打印年历
int main()
{
int year, month, day, weekday, len_of_month, i;
cout << "Please input year: ";
cin >> year;
// 打印年历
cout << endl << year << endl; // 打印年份
weekday = week_of_newyears_day(year); // 求元旦是星期几
for(month=1; month<=12;month=month+1) // 打印12个月的月历
{
cout << endl << month << endl;
cout << "---------------------------------" << endl;
cout << "SUN MON TUE WED THU FRI SET" << endl;
cout << "---------------------------------" << endl;
for(i=0;i
if(month==4 || month== 6 || month==9 || month==11)
len_of_month = 30;
else if(month==2)
{
if(isleap(year))
len_of_month = 29;
else
len_of_month = 28;
}
else
len_of_month = 31;
for(day=1;day<=len_of_month;day=day+1) // 打印当月日期
{
if(day>9)
cout << day << " ";
else
cout << day << " ";
weekday = weekday+1;
if(weekday==7) // 打满一星期应换行
{
weekday = 0;
cout << endl;
}
}
cout << endl; // 打完一月应换行
}
return 0;
}