用C语言编写万年历程序

2024-12-13 16:48:45
推荐回答(2个)
回答1:

// test1.cpp : Defines the entry point for the console application.
//

#include
//输入年份,输出年历。

//---------------------------------------------------------------------------

#include
#include
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("===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;
}
}
//--------------------------------------------------------------------
楼上的运行不出来吧

回答2:

// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//输入年份,输出年历。

//---------------------------------------------------------------------------

#include
#include
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("===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;
}
}
//---------------------------------------------------------------------------