農(nóng)歷轉(zhuǎn)換問題請教
本轉(zhuǎn)換程序1-9月農(nóng)歷顯示都是正確的,就是10-12月農(nóng)歷顯示不正確,請教各位,謝謝先。
void Conversion(void)
/*==轉(zhuǎn)換當(dāng)前農(nóng)歷信息====================================*/
{
unsigned char year,month,day,temp1,temp2,temp3,month_p;
unsigned int temp4,code_addr;
bit flag_y; // flag2,
year =((nian/16)*10+nian%16)&0x7f; //加載年月日數(shù)據(jù),如為BCD則需轉(zhuǎn)為十進制
month=((yue/16)*10+yue%16);
day=((ri/16)*10+ri%16);
code_addr=year-1; //定位數(shù)據(jù)表地址
if(((nian/16)*10+nian%16)>>7==0)
code_addr+=100;
code_addr*=3;
temp1=(year_code[code_addr+2]&0x60)>>5; //取當(dāng)年春節(jié)所在的公歷月份
temp2=year_code[code_addr+2]&0x1f; //取當(dāng)年春節(jié)所在的公歷日
temp3=temp2-1; //計算當(dāng)年春節(jié)離當(dāng)年元旦的天數(shù),春節(jié)只會在公歷1月或2月
if(temp1!=1)
temp3+=31; //如果不在1月則天數(shù)加上31天(1月)
if(month<10)
{temp4=day_code1[month-1]+day;}
else
{temp4=day_code2[month-10]+day;}
if((month<=2)||(year%0x04!=0))
temp4-=1; //如果公歷月小于等于2月或者該年的2月非閏月,天數(shù)減1
temp2=(year_code[code_addr]&0xf0)>>4; //從數(shù)據(jù)表中取該年的閏月月份,如為0則該年無閏月
if (temp4>=temp3) //判斷公歷日在春節(jié)前還是春節(jié)后
{
//公歷日在春節(jié)后或就是春節(jié)當(dāng)日使用下面代碼進行運算
temp4 -=temp3;
month = 1;
flag_y = 0;
month_p= 1; //month_p為月份指向,公歷日在春節(jié)前或就是春節(jié)當(dāng)日month_p指向首月
temp1=get_moon_day(month_p,code_addr); //檢查該農(nóng)歷月為大小還是小月,大月返回1,小月返回0
while(temp4>=temp1)
{
temp4-=temp1;
month_p+=1;
if(month==temp2)
{
flag_y=~flag_y;
if(flag_y==0)
month+=1;
}
else
{month+=1;}
temp1=get_moon_day(month_p,code_addr);
}
day=temp4+1;
}
else
{
//公歷日在春節(jié)前使用下面代碼進行運算
temp3-=temp4;
if(year==0)
{year=0xe3;}
else
{year-=1;}
code_addr-=3;
month = 12;
flag_y = 0;
if(temp2==0)
{month_p=12;}
else
{month_p=13;} //如果當(dāng)年有閏月,一年有十三個月,月指向13,無閏月指向12
temp1=get_moon_day(month_p,code_addr);
while(temp3>temp1)
{
temp3-=temp1;
month_p-=1;
if(flag_y==0)
month-=1;
if(month==temp2)
flag_y=~flag_y;
temp1=get_moon_day(month_p,code_addr);
}
day=temp1-temp3+1;
}
Tim[0] = year|(((nian/16)*10+nian%16)&0x80); //將農(nóng)歷信息寫進指定變量
Tim[1] = month;
Tim[2] = day;
// Conver_week(); //最后進行星期轉(zhuǎn)換(根據(jù)需要自行選用)
}
unsigned char get_moon_day(unsigned char month_p,unsigned int code_addr)/*讀取數(shù)據(jù)表中農(nóng)歷月的大月或小月,如果該月大返回1,小返回0*/
{
unsigned char temp,temp5;
temp=0x80>>((month_p+3)%8);
temp5=(month_p+3)/8;
temp=year_code[code_addr+temp5]&temp;
if(temp==0)
return(29);
else
return(30);
}
歡迎光臨 (http://www.torrancerestoration.com/bbs/) | Powered by Discuz! X3.1 |