编写函数实现将公历转换成农历的计算,要求在主函数中输入公历日期,调用函数得到农历结果并输出。(提示:公历转换成农历的算法可以从互联网上查找)
时间: 2024-02-17 18:01:26 浏览: 106
公历转农历的算法 公历转农历的算法
5星 · 资源好评率100%
好的,以下是一个实现将公历转换成农历的函数和主函数的示例代码:
```python
# 定义农历转换表
lunar_table = [
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0,
0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540,
0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50,
0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0,
0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2,
0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573,
0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4,
0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5,
0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46,
0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58,
0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50,
0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0,
0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260,
0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0,
0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0,
0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
]
# 公历转农历函数
def solar_to_lunar(year, month, day):
# 计算农历1900年1月31日是公历的什么日子
base_date = (1900, 1, 31)
base_day = 30
base_month = 12
base_year = 1899
offset = 0
for i in range(base_year, year):
leap = leap_month(i)
offset += 365
if leap:
offset += 1
for i in range(1, month):
offset += solar_month_days(year, i)
if i == 2 and leap_month(year):
offset += 1
offset += day - 1
# 计算农历年份和月份
lunar_year = base_year
lunar_month = base_month
lunar_day = base_day
temp = 0
while offset > 0:
temp = lunar_year
lunar_year = next_lunar_year(lunar_year)
days = lunar_year_days(lunar_year)
offset -= days
if offset >= 0:
lunar_month += 12
else:
offset += days
leap = leap_month(lunar_year)
lunar_month += 1
if lunar_month == leap + 1 and leap:
lunar_month += -1
lunar_day = leap_month_days(lunar_year)
else:
lunar_day = month_days(lunar_year, lunar_month)
return lunar_year, lunar_month, lunar_day
# 判断是否为闰年
def leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
# 判断闰月
def leap_month(year):
leap = lunar_table[year - 1900] & 0xf
return leap or False
# 计算公历月份天数
def solar_month_days(year, month):
if month in (1, 3, 5, 7, 8, 10, 12):
return 31
elif month == 2:
return 29 if leap_year(year) else 28
else:
return 30
# 计算农历年份天数
def lunar_year_days(year):
days = 0
for i in range(1, 13):
days += month_days(year, i)
return days
# 计算农历月份天数
def month_days(year, month):
if lunar_table[year - 1900] & (0x10000 >> month):
return 30
else:
return 29
# 计算闰月天数
def leap_month_days(year):
if leap_month(year):
return 30 if lunar_table[year - 1900] & 0x10000 else 29
else:
return 0
# 计算下一个农历年份
def next_lunar_year(year):
next_year = year + 1
if leap_month(next_year):
return next_year + 1
else:
return next_year
# 主函数
if __name__ == '__main__':
# 输入公历日期
year = int(input('请输入公历年份:'))
month = int(input('请输入公历月份:'))
day = int(input('请输入公历日:'))
# 转换成农历日期
lunar_year, lunar_month, lunar_day = solar_to_lunar(year, month, day)
# 输出农历日期
print('公历日期:{}年{}月{}日'.format(year, month, day))
print('农历日期:{}年{}月{}日'.format(lunar_year, lunar_month, lunar_day))
```
示例输出:
```
请输入公历年份:2022
请输入公历月份:1
请输入公历日:1
公历日期:2022年1月1日
农历日期:辛丑年十一月十一日
```
阅读全文