阴阳历如何转换?

my_bluesky 2002-09-02 10:02:13
请问阴历如何转换到阳历?或反之。

thanks
...全文
82 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
blh 2002-09-02
  • 打赏
  • 举报
回复

/* 西历农历转换 */
int calconv( struct convdate *cd )
{
int leap, d, sm, y, im, l1, l2, acc, i, lm, kc;
if ( cd->source == 0 ) /* solar */
{
if ( cd->solaryear <= firstyear || cd->solaryear > lastyear )
return 1;
sm = cd->solarmonth - 1;
if ( sm < 0 || sm > 11 )
return 2;
leap = getleap( cd->solaryear );
if ( sm == 1 )
d = leap + 28;
else
d = solarcal[sm];
if ( cd->solardate < 1 || cd->solardate > d )
return 3;
y = cd->solaryear - firstyear;
acc = solardays[leap][sm] + cd->solardate;
cd->weekday = ( acc + lunarcal[y].baseweekday ) % 7;
kc = acc + lunarcal[y].basekanchih;
cd->kan = kc % 10;
cd->chih = kc % 12;
if ( acc <= lunarcal[y].basedays )
{
y--;
cd->lunaryear = cd->solaryear - 1;
leap = getleap( cd->lunaryear );
sm += 12;
acc = solardays[leap][sm] + cd->solardate;
}
else
cd->lunaryear = cd->solaryear;
l1 = lunarcal[y].basedays;
for ( i=0; i<13; i++ )
{
l2 = l1 + lunarcal[y].monthdays[i] + 29;
if ( acc <= l2 )
break;
l1 = l2;
}
cd->lunarmonth = i + 1;
cd->lunardate = acc - l1;
im = lunarcal[y].intercalation;
if ( im != 0 && cd->lunarmonth > im )
{
cd->lunarmonth--;
if ( cd->lunarmonth == im )
cd->lunarmonth = -im;
}
if ( cd->lunarmonth > 12 )
cd->lunarmonth -= 12;
}
else /* lunar */
{
if ( cd->lunaryear < firstyear || cd->lunaryear >= lastyear )
return 1;
y = cd->lunaryear - firstyear;
im = lunarcal[y].intercalation;
lm = cd->lunarmonth;
if ( lm < 0 )
{
if ( lm != -im )
return 2;
}
else if ( lm < 1 || lm > 12 )
return 2;
if ( im != 0 )
{
if ( lm > im )
lm++;
else if ( lm == -im )
lm = im + 1;
}
lm--;
if ( cd->lunardate > lunarcal[y].monthdays[lm] + 29 )
return 3;
acc = lunarcal[y].basedays;
for ( i=0; i acc += lunarcal[y].monthdays[i] + 29;
acc += cd->lunardate;
leap = getleap( cd->lunaryear );
for ( i=13; i>=0; i-- )
if ( acc > solardays[leap][i] )
break;
cd->solardate = acc - solardays[leap][i];
if ( i <= 11 )
{
cd->solaryear = cd->lunaryear;
cd->solarmonth = i + 1;
}
else
{
cd->solaryear = cd->lunaryear + 1;
cd->solarmonth = i - 11;
}
leap = getleap( cd->solaryear );
y = cd->solaryear - firstyear;
acc = solardays[leap][cd->solarmonth-1] + cd->solardate;
cd->weekday = ( acc + lunarcal[y].baseweekday ) % 7;
kc = acc + lunarcal[y].basekanchih;
cd->kan = kc % 10;
cd->chih = kc % 12;
}
return 0;
} 若须扩充,则增加lunarcal[]
blh 2002-09-02
  • 打赏
  • 举报
回复
贴别人的,呵呵,太长,分两段法

程序为:
#define firstyear 1936 /* the first year in lunarcal[] */

struct convdate
{
int source;
int solaryear;
int solarmonth;
int solardate;
int lunaryear;
int lunarmonth;
int lunardate;
int weekday;
int kan;
int chih;
};

struct taglunarcal
{
int basedays; /* 到西历 1 月 1 日到农历正月初一的累积日数 */
int intercalation; /* 闰月月份. 0==此年没有闰月 */
int baseweekday; /* 此年西历 1 月 1 日为星期几再减 1 */
int basekanchih; /* 此年西历 1 月 1 日之干支序号减 1 */
int monthdays[13]; /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日)*/
};

struct taglunarcal lunarcal[] = {
{ 23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 }, /* 1936 */
{ 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 },
{ 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 },
{ 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1,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1, 0, 1, 0, 0, 1, 0, 1 },
{ 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
{ 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 } };

#define lastyear (firstyear+sizeof(lunarcal)/sizeof(struct taglunarcal)-1)

/* 西历年每月之日数 */
int solarcal[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/* 西历年每月之累积日数, 平年与闰年 */
int solardays[2][14] = {
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396 },
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 } };

/* 求此西历年是否为闰年, 返回 0 为平年, 1 为闰年 */
int getleap( int year )
{
if ( year % 400 == 0 )
return 1;
else if ( year % 100 == 0 )
return 0;
else if ( year % 4 == 0 )
return 1;
else
return 0;
}
zijun28 2002-09-02
  • 打赏
  • 举报
回复
其实哪个都差不多啊?不会又是老师布置的作业吧!
my_bluesky 2002-09-02
  • 打赏
  • 举报
回复
呵呵,贴个C吧,看的累啊!

避免重复劳动 :)
freewing 2002-09-02
  • 打赏
  • 举报
回复
不好意思,我选错了,本来要选C的,不过,java也一样,能看明白C的就应该没问题!;->
freewing 2002-09-02
  • 打赏
  • 举报
回复
公历转换农历的算法及其JavaScript实现
------------------------------------------以下为程序内容

var CalendarData=new Array(20);
var madd=new Array(12);
var TheDate=new Date();
var tgString="甲乙丙丁戊己庚辛壬癸";
var dzString="子丑寅卯辰巳午未申酉戌亥";
var numString="一二三四五六七八九十";
var monString="正二三四五六七八九十冬腊";
var weekString="日一二三四五六";
var sx="鼠牛虎兔龙蛇马羊猴鸡狗猪";
var cYear;
var cMonth;
var cDay;
var cHour;
var cDateString;
var DateString;
var Browser=navigator.appName;

function init()
{
CalendarData[0]=0x41A95;
CalendarData[1]=0xD4A;
CalendarData[2]=0xDA5;
CalendarData[3]=0x20B55;
CalendarData[4]=0x56A;
CalendarData[5]=0x7155B;
CalendarData[6]=0x25D;
CalendarData[7]=0x92D;
CalendarData[8]=0x5192B;
CalendarData[9]=0xA95;
CalendarData[10]=0xB4A;
CalendarData[11]=0x416AA;
CalendarData[12]=0xAD5;
CalendarData[13]=0x90AB5;
CalendarData[14]=0x4BA;
CalendarData[15]=0xA5B;
CalendarData[16]=0x60A57;
CalendarData[17]=0x52B;
CalendarData[18]=0xA93;
CalendarData[19]=0x40E95;
madd[0]=0;
madd[1]=31;
madd[2]=59;
madd[3]=90;
madd[4]=120;
madd[5]=151;
madd[6]=181;
madd[7]=212;
madd[8]=243;
madd[9]=273;
madd[10]=304;
madd[11]=334;
}

function GetBit(m,n)
{
return (m>>n)&1;
}

function e2c()
{
var total,m,n,k;
var isEnd=false;
var tmp=TheDate.getYear();
if (tmp<1900) tmp+=1900;
total=(tmp-2001)*365
+Math.floor((tmp-2001)/4)
+madd[TheDate.getMonth()]
+TheDate.getDate()
-23;
if (TheDate.getYear()%4==0&&TheDate.getMonth()>1)
total++;
for(m=0;;m++)
{
k=(CalendarData[m]<0xfff)?11:12;
for(n=k;n>=0;n--)
{
if(total<=29+GetBit(CalendarData[m],n))
{
isEnd=true;
break;
}
total=total-29-GetBit(CalendarData[m],n);
}
if(isEnd)break;
}
cYear=2001 + m;
cMonth=k-n+1;
cDay=total;
if(k==12)
{
if(cMonth==Math.floor(CalendarData[m]/0x10000)+1)
cMonth=1-cMonth;
if(cMonth>Math.floor(CalendarData[m]/0x10000)+1)
cMonth--;
}
cHour=Math.floor((TheDate.getHours()+3)/2);
}

function GetcDateString()
{ var tmp="";
tmp+=tgString.charAt((cYear-4)%10); //年干
tmp+=dzString.charAt((cYear-4)%12); //年支
tmp+="年(";
tmp+=sx.charAt((cYear-4)%12);
tmp+=")";
if(cMonth<1)
{
tmp+="闰";
tmp+=monString.charAt(-cMonth-1);
}
else
tmp+=monString.charAt(cMonth-1);
tmp+="月";
tmp+=(cDay<11)?"初":((cDay<20)?"十":((cDay<30)?"廿":"卅"));
if(cDay%10!=0||cDay==10)
tmp+=numString.charAt((cDay-1)%10);
if(cHour==13)tmp+="夜";
tmp+=dzString.charAt((cHour-1)%12);
tmp+="时";
cDateString=tmp;
return tmp;
}

function GetDateString()
{
var tmp="";
var t1=TheDate.getYear();
if (t1<1900)t1+=1900;
tmp+=t1
+"-"
+(TheDate.getMonth()+1)+"-"
+TheDate.getDate()+" "
+TheDate.getHours()+":"
+((TheDate.getMinutes()<10)?"0":"")
+TheDate.getMinutes()
+" 星期"+weekString.charAt(TheDate.getDay());
DateString=tmp;
return tmp;
}

init();
e2c();
GetDateString();
GetcDateString();
document.write(DateString,"<br>",cDateString);
你可以这样写一个HTML文件调用date.js: <html>
<head>
</head>
<body>
<script language="JavaScript" src="date.js"></script>
</body>
</html>

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧