公历转农历(C版本)

      前段时间做软件写了个公历转农历的函数,目前公开给大家参考,有兴趣的朋友可以在此基础上进一步完善其功能

/*------------农历转换函数-----------*/
char *GetDayOf(PSYSTEMTIME pSt)

 /*天干名称*/
 const char *cTianGan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
 /*地支名称*/
 const char *cDiZhi[] = {"子","丑","寅","卯","辰","巳","午",
       "未","申","酉","戌","亥"};
 /*属相名称*/
 const char *cShuXiang[] = {"鼠","牛","虎","兔","龙","蛇",
       "马","羊","猴","鸡","狗","猪"};
 /*农历日期名*/
 const char *cDayName[] = {"*","初一","初二","初三","初四","初五",
       "初六","初七","初八","初九","初十",
       "十一","十二","十三",&#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C语言中可以使用公历农历的算法来实现阳历阴历。这个算法的实现比较复杂,需要涉及到一些数学计算和历法知识。 以下是一个简单的C语言程序,可以将给定的阳历日期换为对应的阴历日期: ```c #include <stdio.h> #include <stdlib.h> // 农历数据表 int lunar_month_days[] = {0,31,59,90,120,151,181,212,243,273,304,334}; char lunar_month_names[][10] = {"正","二","三","四","五","六","七","八","九","十","十一","十二"}; char lunar_day_names[][10] = {"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十", "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"}; // 获取指定年份的天数 int get_year_days(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return 366; } else { return 365; } } // 获取指定月份的天数 int get_month_days(int year, int month) { if ((month == 2) && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) { return 29; } else { return lunar_month_days[month] - lunar_month_days[month-1]; } } // 获取指定阳历日期的阴历日期 int get_lunar_date(int year, int month, int day) { int lunar_year = 1900; // 农历年份从1900年开始 int lunar_month = 1; int lunar_day = 1; int leap_month = 0; // 闰月,默认为0表示无闰月 // 计算指定阳历日期与1900年1月31日的天数差 int days = 0; for (int i = 1900; i < year; i++) { days += get_year_days(i); } for (int i = 1; i < month; i++) { days += get_month_days(year, i); } days += day - 1; // 计算农历日期 while (days > 0) { int month_days = get_month_days(lunar_year, lunar_month); if (days >= month_days) { days -= month_days; lunar_month++; // 判断是否有闰月 if (leap_month > 0 && lunar_month == leap_month + 1) { lunar_month--; leap_month = 0; } // 判断当前月份是否是闰月 if (lunar_month > 12) { lunar_month = 1; lunar_year++; } if (lunar_month == leap_month) { leap_month = get_month_days(lunar_year, 13) == 29 ? 13 : 12; } } else { lunar_day = days + 1; break; } } printf("%d年%s月%s\n", lunar_year, lunar_month_names[lunar_month-1], lunar_day_names[lunar_day-1]); return 0; } int main() { int year, month, day; printf("请输入阳历日期(YYYY-MM-DD): "); scanf("%d-%d-%d", &year, &month, &day); printf("输入的阳历日期为: %d年%d月%d日\n", year, month, day); get_lunar_date(year, month, day); return 0; } ``` 注意:这个程序只是简单地实现了阳历日期到阴历日期的换,可能存在一些误差。如果需要更准确的结果,可以参考更复杂的历法计算方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值