用C语言实现阴历和阳历的转换
时间: 2024-03-23 21:43:05 浏览: 21
实现阳历和阴历的转换需要用到一些数学算法和历法知识。下面是一个简单的阴历和阳历的转换函数:
```C
#include <stdio.h>
// 阳历转阴历
void solarToLunar(int year, int month, int day, int* lunarYear, int* lunarMonth, int* lunarDay) {
int lunarMonthDays[13] = {29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29}; // 阴历每月的天数
int lunarLeapMonth = 0; // 闰月
int lunarLeapMonthDays = 0; // 闰月的天数
int lunarYearDays = 0; // 阴历年的天数
int lunarYearStartDay = 0; // 阴历年的起始日
int lunarMonthDaysCount = 0; // 阴历月已经过的天数
int i, j;
for (i = 1901; i < year; i++) {
lunarYearDays += 365 + isLeapYear(i);
}
for (i = 1; i < month; i++) {
lunarMonthDaysCount += lunarMonthDays[i - 1];
}
lunarYearDays += lunarMonthDaysCount + day - 1;
lunarYearStartDay = (lunarYearDays + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 1) % 7;
if (isLeapYear(year)) {
if (month > 2) {
lunarYearDays++;
}
if (month > 1) {
lunarYearStartDay++;
}
}
for (i = 1; i <= 12; i++) {
if (lunarYearDays >= lunarMonthDays[i - 1]) {
lunarYearDays -= lunarMonthDays[i - 1];
} else {
break;
}
}
if (i <= 12 && lunarYearDays >= lunarMonthDays[i - 1]) {
lunarLeapMonth = i;
lunarLeapMonthDays = lunarMonthDays[i - 1];
if (i == 12) {
i = 0;
}
for (j = i + 1; j <= 12; j++) {
if (lunarYearDays >= lunarMonthDays[j - 1]) {
lunarYearDays -= lunarMonthDays[j - 1];
} else {
break;
}
}
i = j - 1;
}
*lunarYear = year;
*lunarMonth = i;
*lunarDay = lunarYearDays + 1;
if (lunarLeapMonth > 0 && i > lunarLeapMonth) {
*lunarMonth = i - 1;
}
}
// 阴历转阳历
void lunarToSolar(int lunarYear, int lunarMonth, int lunarDay, int* year, int* month, int* day) {
int lunarMonthDays[13] = {29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29}; // 阴历每月的天数
int lunarLeapMonth = 0; // 闰月
int lunarLeapMonthDays = 0; // 闰月的天数
int lunarYearDays = 0; // 阴历年的天数
int lunarYearStartDay = 0; // 阴历年的起始日
int lunarMonthDaysCount = 0; // 阴历月已经过的天数
int i, j;
for (i = 1901; i < lunarYear; i++) {
lunarYearDays += 365 + isLeapYear(i);
}
for (i = 1; i < lunarMonth; i++) {
lunarMonthDaysCount += lunarMonthDays[i - 1];
}
lunarYearDays += lunarMonthDaysCount + lunarDay - 1;
lunarYearStartDay = (lunarYearDays + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 1) % 7;
for (i = 1; i <= 12; i++) {
if (isLeapMonth(lunarYear, i)) {
lunarLeapMonth = i;
lunarLeapMonthDays = lunarMonthDays[i - 1];
}
if (i != lunarLeapMonth) {
if (lunarYearStartDay == 0 && lunarMonthDays[i - 1] == 29) {
break;
}
if (lunarYearStartDay <= 7 - lunarMonthDays[i - 1] % 7) {
break;
}
lunarYearStartDay -= 7 - lunarMonthDays[i - 1] % 7;
}
}
if (lunarLeapMonth > 0 && lunarMonth > lunarLeapMonth) {
lunarMonth++;
}
*year = 1901;
*month = 1;
*day = 1;
while (lunarYearDays > 0) {
int daysOfYear = 365 + isLeapYear(*year);
if (lunarYearDays >= daysOfYear) {
lunarYearDays -= daysOfYear;
(*year)++;
} else {
break;
}
}
for (i = 1; i <= 12; i++) {
int daysOfMonth = getDaysOfMonth(*year, i);
if (lunarYearDays >= daysOfMonth) {
lunarYearDays -= daysOfMonth;
(*month)++;
} else {
break;
}
}
*day = lunarYearDays + 1;
}
int main() {
int year, month, day;
int lunarYear, lunarMonth, lunarDay;
printf("请输入阳历年月日(例如:2022 6 21):");
scanf("%d %d %d", &year, &month, &day);
solarToLunar(year, month, day, &lunarYear, &lunarMonth, &lunarDay);
printf("阴历:%d年%d月%d日\n", lunarYear, lunarMonth, lunarDay);
printf("请输入阴历年月日(例如:2022 5 18):");
scanf("%d %d %d", &lunarYear, &lunarMonth, &lunarDay);
lunarToSolar(lunarYear, lunarMonth, lunarDay, &year, &month, &day);
printf("阳历:%d年%d月%d日\n", year, month, day);
return 0;
}
```
该程序可以输入阳历年月日,输出对应的阴历年月日;也可以输入阴历年月日,输出对应的阳历年月日。你可以根据需要进行扩展,实现更复杂的功能。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)