//**********************************
// 公历日期与农历日期的相互转换程序,单片机c语言程序
// 公元1800年1月25日~2101年1月28日
// 请在VC++6.0平台运行
//**********************************
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<windows.h>
#include<iostream.h>
struct date{ short year,month,day; }
d1={ 1800,1,25 },//农历1800正月初一(星期六)
d2={ 2101,1,28 };//农历2100腊月廿九(未使用)
struct lunarYear //农历年
{ char run; //闰月月份(0表示无闰月)
char ZL[13]; //农历i月初一对应公历i月ZL[i]日(广义)
} Year[2101 //但是ZL[i]的最高位1表示大月0表示小月
-1800]={4};//农历1800年闰四月
#define leap(y) !(y%(y%100?4:400)) //公元闰年宏定义
unsigned char days[]={ 0,31,28,31,30,31,30,31,31,30,31,30,31 },
b[]= //农历1800-2100年大月(30天)小月(29天)闰月数据(hex string)
{
"adca5aa92bb525bba25bb493b64a5ba9add496ea8a6ed14eea4a6da5b6"
"545baa4bba45bba82b75a5b654abaa55d526eda4aed495da526daa5655"
"abb295ba5257aa4b75a95655abaa56ea4a5da92ed965d594adaa56692b"
"75a5ba64977493b6ca5a69add496da925dd24dda495da9add45a6a4b75"
"4937692775a5b6545baa2dd526dda49dd495ba525baa5555ab6a937652"
"57ea4a75a95655abaa55da4a5da92bd925db54abaa56592b75a5ae6497"
"ec92aed25659abb495da925dd24bba495ba9add4566a4b75492fe9266d"
"a5aed4566a2db525bba49bb493ba525baa55d5aa6a936ed24eea4a6da9"
"36d5aaaa55b64a5ba92b75a5ba54abaa55d52a6da5aed495ec926daa56"
"55abb495ba525bb24b76495769abb455da4a6dc92ee925dda4add4566a"
"4bb5a5baa4977493b652576a4db5a6da926dd24dda4a5da92dd5aa6a55"
"b54937692775a5b654abaa55d52a5da5add495ba925daa55d5aa6c95b6"
"5257724b76c95659abac55da4a5da92dd925bda4abb4555a4b6da5b6649"
"7f492aed2566a4bb5a5da925dd24bba495ba92bb5aa6a53754937e9266d"
"a5aed4966a55b52a5ba5abb495ba525baa55d5aa6a95aed256ea4a6ec94"
"ed9aa6a55b64a5ba92bb945bb64abb455d64a6da5aee416eda26ed25502"
};
short rs[22]={ //农历1800-2100年111个闰月所形成的110个间距的数据
32198,25072,32079,29168,31350,3554,29118,28494,25464,28215,7608,
27766,3975,30902,28494,21489,28214,7132,27766,3975,25078,28039};
char f(short m,short x)//1月x日相当于m月f日
{
for(short i=1;i<m;i++)x-=days[i];
return char(x);
}
void initial(void)//初始化Year结构数组
{ short i,t,ir=Year[0].run;
for(short r=1;r<=110;r++)
{ if(r%5==1)t=rs[r/5];
ir+=29+t%8;t>>=3;
Year[(ir-r)/12].run=(ir-r)%12;
} lunarYear*p=&Year[0];r=p->run;
for(t=0,i=0;b[i]!='\0';i+=2,t++) //hex string
#define VAL(h) (h<'a'?(h-'0'):(h-'a'+10)) //hex string
b[t]=16*VAL(b[i])+VAL(b[i+1]); //hex string
short M,y=1800,hao=25;//1800年1月25日为正月初一
for(M=1,i=0;i<3723;i++,M++)
{ if(M>12+!!r){ M=1;hao-=365+leap(y);y++;r=(++p)->run; }
t=b[i/8];t>>=i%8; days[2]=28+leap(y);
if(r==0||M<=r){p->ZL[M]=f(M,hao);if(t%2)p->ZL[M]|=0x80;}
else if(M>1+r){p->ZL[M-1]=f(M-1,hao);if(t%2)p->ZL[M-1]|=0x80;}
else {p->ZL[0]=f(r,hao);if(t%2)p->ZL[0]|=0x80;}
hao+=29+t%2; }
}
date getSolar(date D)//农历→公历
{
short im,m;im=m=D.month;
if(im<0){m=-im;im=0;}
lunarYear*p=&Year[D.year-1800];
date d;d.year=D.year;
d.day=(0x7f&p->ZL[im])-1+D.day;
days[2]=28+leap(d.year);
while(d.day>days[m])
{ d.day-=days[m];m=m%12+1;if(m==1)d.year++; }
d.month=m;return d;
}
date getLunar(date d)//公历→农历
{
lunarYear*p=&Year[d.year-1800];
days[2]=28+leap(d.year);
short m=d.month;
d.day-=(p->ZL[m]&0x7f)-1;
while(d.day<1){
if(m==0)m=p->run;
else if(m==1){ m=12;d.year--;p--; }
else if(p->run==m-1)m=0;
else m--;
d.day+=29+(p->ZL[m]<0);
}
if(m==0)m=-p->run;
d.month=m;return d;
}
long total(date a)//公元以来的天数
{
days[2]=28+leap(a.year);long C,m,x=a.year-1;
for(C=a.day,m=1;m<a.month;m++)C+=days[m];
return 365*x+x/4-x/100+x/400+C;
}
short ok(date d)
{
short y=d.year,m=d.month,t=d.day,ok=1;
if(y<1800||y>2100)ok=-1;
else if(m<1||m>12)ok=0;
else{days[2]=28+leap(y);
if(t<1||t>days[m])ok=0;}
if(total(d)<total(d1))ok=-1;
return ok;
}
short OK(date D)//若D.month取负值则表示农历闰月
{
&nbnbsp; short Y=D.year,M=D.month,T=D.day;
if(Y<1800||Y>2100)return -1;
if(T<=0||T>30)return 0;//排除明显的非法"日"号
if(M==0||M>12)return 0;//排除明显的非法"月"号
lunarYear* p=&Year[Y-1800];
if((M<0?-M==p->run?29+(p->ZL[0]<0):0
:29+(p->ZL[M]<0))<T)return 0;
return 1;
}
#define BIG(i) p->ZL[i]<0
void gotoxy(int x, int y)
{
COORD c;
c.X = x - 1;c.Y = y - 1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main( )
{ char*xq[]={"日","一","二","三","四","五","六",
"七","八","九","十","冬","腊" };
int cho,Y;
ios::sync_with_stdio( );
initial();
Menu:
do{
system("cls");
gotoxy(10, 7-2);cout<<"1.将公历转换为农历";
gotoxy(10,10-2);cout<<"2.将农历转换为公历";
gotoxy(10,13-2);cout<<"3.叁百年农历闰月表";
gotoxy(10,16-2);cout<<"4.农历某年大月小月";
gotoxy(10,19-2);cout<<"0.运行结束返回系统";
gotoxy(10,23-2);cout<<"选择: ";
cho=getch( );
}
while(cho<'0'||cho>'4');
if(cho=='4')
{
gotoxy(10,23);
cout<<"年份? ";
{
int tmp;
Y=0;
while(1)
{
tmp=getche();
if(tmp<'0'||tmp>'9')break;
Y=Y*10+(tmp-'0');
}
}
if(Y<1800||Y>2100)Y=2008;
}
system("cls");
if(cho<='2')cout<<"\n\n\n\n";
if(cho=='1')
while(1)
{ char c; date DT,dt; int o;
do{ cout<<"公元(yyyy-mm-dd): ";
cin>>dt.year>>c>>dt.month>>c>>dt.day;//c为分隔符
o=ok(dt);if(o==-1)goto Menu; }while(o<=0);
DT=getLunar(dt);//将公历日期dt转换为对应的农历DT
cout<<"农历"<<DT.year<<"年"<<(DT.month<0?"闰":"")
<<abs(DT.month)<<"月"<<DT.day<<"日"
<<"〔星期"<<xq[total(dt)%7]<<"〕\n"<<endl;
}
else if(cho=='2')
while(1)
{ char c; date DT,dt; int o;
do{ cout<<"农历(YYYY.MM.DD): ";
cin>>DT.year>>c>>DT.month>>c>>DT.day;//c为分隔符
o=OK(DT);if(o==-1)goto Menu; }while(o<=0);
dt=getSolar(DT);//将农历日期DT转换为对应的公历dt
cout<<"公元"<<dt.year<<"年"
<<dt.month<<"月"<<dt.day<<"日"
<<"〔星期"<<xq[total(dt)%7]<<"〕\n"<<endl;
}
else if(cho=='3')
{ int counter=0,r;lunarYear* p=&Year[1800-1800];
gotoxy(19,1);cout<<"1800—2100年间农历闰月列表"<<endl;
for(int i=0;i<=2100-1800;i++,p++)
if((r=Year[i].run)!=0)
{ if(counter++%5==0)cout<<"\n ";
cout<<1800+i<<"闰"<<xq[r]<<"月"
<<(BIG(0)?"大":"小")<<" ";
} getch();goto Menu;
}
else if(cho=='4')
{ int counter=0;lunarYear* p=&Year[Y-1800];
cout<<"\n\n 农历"<<Y<<"年大月小月列表\n\n\n\n ";
for(int m=1;m<=12;m++)
{ cout<<(m>1?xq[m]:"正")<<"月"
<<(BIG(m)?"大":"小")<<" ";
if(++counter%4==0)cout<<"\n\n ";
if(p->run!=m)continue;
cout<<"闰"<<xq[m]<<"月"
<<(BIG(0)?"大":"小")<<" ";
if(++counter%4==0)cout<<"\n\n ";
} getch();goto Menu;
}
else if(cho=='0')system("cls");
return 0;