農歷轉換問題請教
本轉換程序1-9月農歷顯示都是正確的,就是10-12月農歷顯示不正確,請教各位,謝謝先。
void Conversion(void)
/*==轉換當前農歷信息====================================*/
{
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; //加載年月日數據,如為BCD則需轉為十進制
month=((yue/16)*10+yue%16);
day=((ri/16)*10+ri%16);
code_addr=year-1; //定位數據表地址
if(((nian/16)*10+nian%16)>>7==0)
code_addr+=100;
code_addr*=3;
temp1=(year_code[code_addr+2]&0x60)>>5; //取當年春節所在的公歷月份
temp2=year_code[code_addr+2]&0x1f; //取當年春節所在的公歷日
temp3=temp2-1; //計算當年春節離當年元旦的天數,春節只會在公歷1月或2月
if(temp1!=1)
temp3+=31; //如果不在1月則天數加上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月非閏月,天數減1
temp2=(year_code[code_addr]&0xf0)>>4; //從數據表中取該年的閏月月份,如為0則該年無閏月
if (temp4>=temp3) //判斷公歷日在春節前還是春節后
{
//公歷日在春節后或就是春節當日使用下面代碼進行運算
temp4 -=temp3;
month = 1;
flag_y = 0;
month_p= 1; //month_p為月份指向,公歷日在春節前或就是春節當日month_p指向首月
temp1=get_moon_day(month_p,code_addr); //檢查該農歷月為大小還是小月,大月返回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
{
//公歷日在春節前使用下面代碼進行運算
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;} //如果當年有閏月,一年有十三個月,月指向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); //將農歷信息寫進指定變量
Tim[1] = month;
Tim[2] = day;
// Conver_week(); //最后進行星期轉換(根據需要自行選用)
}
unsigned char get_moon_day(unsigned char month_p,unsigned int code_addr)/*讀取數據表中農歷月的大月或小月,如果該月大返回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.raoushi.com/bbs/) | Powered by Discuz! X3.1 |