c语言实现万年历包含农历
时间: 2023-07-13 18:06:27 浏览: 349
实现万年历需要考虑到公历和农历的转换。以下是一个使用C语言实现的万年历代码,包含农历:
```c
#include <stdio.h>
int monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //每月天数(不含闰月)
char *months[13] = {" ","一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; //每月名称
char *weekDays[7] = {"日","一","二","三","四","五","六"}; //每周名称
int lunarYear(int year) //判断是否为闰年
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 1;
else
return 0;
}
int yearDays(int year) //计算公历一年的天数
{
if (lunarYear(year))
return 366;
else
return 365;
}
int monthDaysLunar(int year, int month) //计算农历每月天数
{
int leapMonth = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 0 : 1; //判断是否有闰月
int lunarMonthDays[13] = {0,29,30,29,30,29,30,29,30,29,30,29,30}; //每月天数(包含闰月)
if (leapMonth) //若有闰月
{
if (month > leapMonth) //若查询的月份在闰月之后
month++; //月份需要加1
}
return lunarMonthDays[month]; //返回该月天数
}
int lunarYearDays(int year) //计算农历一年的天数
{
int days = 0;
for (int i=1; i<=12; i++)
days += monthDaysLunar(year, i);
return days;
}
int lunarMonthDays(int year, int month) //计算农历某个月的天数
{
int days = 0;
for (int i=1; i<=month; i++)
days += monthDaysLunar(year, i);
return days - monthDaysLunar(year, month) + 1;
}
int weekDay(int year, int month, int day) //计算公历某天是星期几
{
if (month == 1 || month == 2)
{
month += 12;
year--;
}
int century = year / 100;
year %= 100;
int week = year + year/4 + century/4 - 2*century + 26*(month+1)/10 + day - 1;
return (week+7) % 7;
}
void printHeader(int year, int month) //打印日历头部
{
printf("%d年 %s\n", year, months[month]);
for (int i=0; i<7; i++)
{
printf("%s ", weekDays[i]);
}
printf("\n");
}
void printCalendar(int year, int month) //打印公历日历
{
int days = monthDays[month];
int week = weekDay(year, month, 1);
int count = 0;
printHeader(year, month);
for (int i=0; i<week; i++)
{
printf(" ");
count++;
}
for (int i=1; i<=days; i++)
{
printf("%2d ", i);
count++;
if (count % 7 == 0)
{
printf("\n");
}
}
if (count % 7 != 0)
{
printf("\n");
}
}
void printLunar(int year, int month) //打印农历日历
{
int lunarYearDaysCount = lunarYearDays(year); //计算农历一年的天数
int lunarMonthDaysCount = lunarMonthDays(year, month); //计算该月天数
int lunarYearStartWeek = weekDay(year, 1, 1); //计算农历年初一是星期几
int lunarMonthStartWeek = weekDay(year, month, 1); //计算农历月初一是星期几
int count = 0;
printf("农历\n");
for (int i=0; i<7; i++)
{
printf("%s ", weekDays[i]);
}
printf("\n");
for (int i=0; i<lunarYearStartWeek; i++)
{
printf(" ");
count++;
}
for (int i=1; i<=lunarYearDaysCount; i++)
{
int lunarMonth; //农历月份
int lunarDay; //农历日份
int lunarMonthDaysCountTemp; //该月天数的临时变量
int leapMonth = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 0 : 1; //判断是否有闰月
int leapMonthMonth = 0; //闰月的月份
if (leapMonth) //如果有闰月
leapMonthMonth = 5; //闰月一般在5月份
if (i < lunarMonthDaysCountLunar(year, 1)) //计算出农历1月初一对应公历的日期
{
lunarMonth = 1;
lunarDay = i;
}
else if (i >= lunarMonthDaysLunar(year, 2) && i < lunarMonthDaysLunar(year, 3)) //计算出农历2月初一对应公历的日期
{
if (leapMonth && lunarMonthStartWeek >= 2 && month > leapMonthMonth) //如果有闰月,且该年闰月之后
lunarMonth = 3;
else
lunarMonth = 2;
lunarDay = i - lunarMonthDaysLunar(year, lunarMonth-1);
}
else //计算出农历其他月份初一对应公历的日期
{
for (int j=3; j<=12; j++) //从3月份开始计算
{
lunarMonthDaysCountTemp = lunarMonthDaysLunar(year, j);
if (i >= lunarMonthDaysLunar(year, j) && i < lunarMonthDaysLunar(year, j+1))
{
if (leapMonth && lunarMonthStartWeek >= 2 && j > leapMonthMonth) //如果有闰月,且该年闰月之后
lunarMonth = j+1;
else
lunarMonth = j;
lunarDay = i - lunarMonthDaysLunar(year, lunarMonth-1);
break;
}
}
}
printf("%2d ", lunarDay);
count++;
if (count % 7 == 0)
{
printf("\n");
}
}
if (count % 7 != 0)
{
printf("\n");
}
}
int main()
{
int year, month;
printf("请输入年份和月份:");
scanf("%d%d", &year, &month);
printCalendar(year, month);
printLunar(year, month);
return 0;
}
```
该代码中使用了两个数组,分别存储了公历每月天数和农历每月天数。同时,通过调用函数可以计算出公历某天是星期几,以及农历某个月的天数和某一天的农历日期。最后,通过打印头部和循环打印每一天即可实现万年历的输出。
阅读全文