用C语言编程:已知2012年1月1日是星期日,打印万年历。(结果是按一下回车键,显示一个月)
1个回答
展开全部
不是我编的,见参考资料
#include <stdio.h>
#include <stdlib.h>
int JudgeLeapYear(int); // 判断是否为闰年
int MonthDay(int m,int y); // 返回本月的天数
void printHeader(void); // 输出表头
int FirstWeek(int m,int y);// 返回本月一号是星期几
void printMonth(int m); // 输出月份表头
int main(int argc, char* argv[]) {
int year,month,i,test,week;
printf("Year:");
scanf("%d",&year);
for(month = 1; month <= 12; month++) {
test = 1;
printMonth(month);
printHeader();
week = FirstWeek(month,year);
if(week < 6) { //输出每月的第一星期
for(i = 0; i <= week; i++) {
putchar('\t');
test++;
}
}
for(i = 1; i <= MonthDay(month,year); i++,test++) {
//输出每个月非第一个星期
printf("%d\t",i);
if(test%7 == 0) putchar('\n');
}
putchar('\n');
system("pause");//暂停,以便观察输出
}
return 0;
}
//判断是否润年
int JudgeLeapYear(int y) {
return ((y%4 == 0) && (y%100) || (y%400 == 0)) ? 1 : 0;
}
//返回这月的一号是星期几
int FirstWeek(int month,int year) {
if (month < 3) {
month += 12;
year--;
}
return (1 + 2*month + 3*(month + 1)/5 + year + year/4 - year/100 + year/400)%7;
}
//一月有多少天
int MonthDay(int month,int year) {
int d;
switch (month) {
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 = JudgeLeapYear(year) ? 29 : 28; break;
}
return d;
}
//打印日历头
void printHeader(void) {
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n","Sun","Mon","Tue","Wed","Thur","Fri","Sat");
}
//打印月份
void printMonth(int m) {
switch (m) {
default:
case 1:puts("\t***** January *****");break;
case 2:puts("\t***** February *****");break;
case 3:puts("\t***** March *****");break;
case 4:puts("\t***** April *****");break;
case 5:puts("\t***** May *****");break;
case 6:puts("\t***** June *****");break;
case 7:puts("\t***** July *****");break;
case 8:puts("\t***** August *****");break;
case 9:puts("\t***** September *****");break;
case 10:puts("\t***** October *****");break;
case 11:puts("\t***** November *****");break;
case 12:puts("\t***** December *****");break;
}
}
#include <stdio.h>
#include <stdlib.h>
int JudgeLeapYear(int); // 判断是否为闰年
int MonthDay(int m,int y); // 返回本月的天数
void printHeader(void); // 输出表头
int FirstWeek(int m,int y);// 返回本月一号是星期几
void printMonth(int m); // 输出月份表头
int main(int argc, char* argv[]) {
int year,month,i,test,week;
printf("Year:");
scanf("%d",&year);
for(month = 1; month <= 12; month++) {
test = 1;
printMonth(month);
printHeader();
week = FirstWeek(month,year);
if(week < 6) { //输出每月的第一星期
for(i = 0; i <= week; i++) {
putchar('\t');
test++;
}
}
for(i = 1; i <= MonthDay(month,year); i++,test++) {
//输出每个月非第一个星期
printf("%d\t",i);
if(test%7 == 0) putchar('\n');
}
putchar('\n');
system("pause");//暂停,以便观察输出
}
return 0;
}
//判断是否润年
int JudgeLeapYear(int y) {
return ((y%4 == 0) && (y%100) || (y%400 == 0)) ? 1 : 0;
}
//返回这月的一号是星期几
int FirstWeek(int month,int year) {
if (month < 3) {
month += 12;
year--;
}
return (1 + 2*month + 3*(month + 1)/5 + year + year/4 - year/100 + year/400)%7;
}
//一月有多少天
int MonthDay(int month,int year) {
int d;
switch (month) {
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 = JudgeLeapYear(year) ? 29 : 28; break;
}
return d;
}
//打印日历头
void printHeader(void) {
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n","Sun","Mon","Tue","Wed","Thur","Fri","Sat");
}
//打印月份
void printMonth(int m) {
switch (m) {
default:
case 1:puts("\t***** January *****");break;
case 2:puts("\t***** February *****");break;
case 3:puts("\t***** March *****");break;
case 4:puts("\t***** April *****");break;
case 5:puts("\t***** May *****");break;
case 6:puts("\t***** June *****");break;
case 7:puts("\t***** July *****");break;
case 8:puts("\t***** August *****");break;
case 9:puts("\t***** September *****");break;
case 10:puts("\t***** October *****");break;
case 11:puts("\t***** November *****");break;
case 12:puts("\t***** December *****");break;
}
}
参考资料: http://zhidao.baidu.com/question/409409613.html?seed=0
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询