阳历(公历)转农历 1801-2099

找了个1801-2099的数据
#include <iostream>

using namespace std;

struct LunarDate
{
	int year;
	int month;
	int day;
	bool isLeap;
	int yearCyl;//cyclic天干地支
	int monCyl;
	int dayCyl;
};


unsigned short g_LunarInfo[]={   
	0x6b50, 0x2ba0, 0xab62, 0x9370, 0x52e6, 0xd160, 0xe4b0, 0x6d25, 0xda90, 0x5b50, // 1801 ~ 1810   
	0x36d3, 0x2ae0, 0xa2e0, 0xe2d2, 0xc950, 0xd556, 0xb520, 0xb690, 0x5da4, 0x55d0, // 1811 ~ 1820   
	0x25d0, 0xa5b3, 0xa2b0, 0xa8b7, 0xa95f, 0xb4a0, 0xb2a5, 0xad5f, 0x55b0, 0x2b74, // 1821 ~ 1830   
	0x2570, 0x52f9, 0x52b0, 0x6950, 0x6d56, 0x5aa0, 0xab50, 0x56d4, 0x4ae0, 0xa570, // 1831 ~ 1840   
	0x4553, 0xd2af, 0xe8a7, 0xd55f, 0x5aa0, 0xada5, 0x95d0, 0x4ae0, 0xaab4, 0xa4d0, // 1841 ~ 1850   
	0xd2b8, 0xb290, 0xb550, 0x5757, 0x2da0, 0x95d0, 0x4d75, 0x49b0, 0xa4b0, 0xa4b3, // 1851 ~ 1860   
	0x6a9f, 0xad98, 0x6b50, 0x2b60, 0x9365, 0x937f, 0x4970, 0x6964, 0xe4a0, 0xea6a, // 1861 ~ 1870   
	0xda90, 0x5ad0, 0x2ad6, 0x2aef, 0x92e0, 0xcad5, 0xc950, 0xd4a0, 0xd4a3, 0xb69f, // 1871 ~ 1880   
	0x57a7, 0x55b0, 0x25d0, 0x95b5, 0x92b0, 0xa950, 0xd954, 0xb4a0, 0xb550, 0x6d52, // 1881 ~ 1890   
	0x55b0, 0x2776, 0x2570, 0x52b0, 0xaaa5, 0xe950, 0x6aa0, 0xbaa3, 0xab50, 0x4bd8, // 1891 ~ 1900   

	0x4ae0, 0xa570, 0x54d5, 0xd260, 0xd950, 0x16554/*0x5554*/, 0x56af, 0x9ad0, 0x55d2, 0x4ae0, // 1901 ~ 1910   
	0xa5b6, 0xa4d0, 0xd250, 0xd255, 0xb54f, 0xd6a0, 0xada2, 0x95b0, 0x4977, 0x497f, // 1911 ~ 1920   
	0xa4b0, 0xb4b5, 0x6a50, 0x6d40, 0xab54, 0x2b6f, 0x9570, 0x52f2, 0x4970, 0x6566, // 1921 ~ 1930   
	0xd4a0, 0xea50,/* 0x6a95, 0x5adf*/0x06E95, 0x05AD0, 0x2b60, 0x86e3, 0x92ef, 0xc8d7, 0xc95f, 0xd4a0, // 1931 ~ 1940   
	0xd8a6, 0xb55f, 0x56a0, 0xa5b4, 0x25df, 0x92d0, 0xd2b2, 0xa950, 0xb557, 0x6ca0, // 1941 ~ 1950   
	0xb550, 0x5355, 0x4daf, 0xa5b0, 0x4573, 0x52bf, 0xa9a8, 0xe950, 0x6aa0, 0xaea6, // 1951 ~ 1960   
	0xab50, 0x4b60, 0xaae4, 0xa570, 0x5260, 0xf263, 0xd950, 0x5b57, 0x56a0, 0x96d0, // 1961 ~ 1970   
	0x4dd5, 0x4ad0, 0xa4d0, 0xd4d4, 0xd250, 0xd558, 0xb540, 0xb6a0, 0x95a6, 0x95bf, // 1971 ~ 1980   
	0x49b0, 0xa974, 0xa4b0, 0xb27a, 0x6a50, 0x6d40, 0xaf46, 0xab60, 0x9570, 0x4af5, // 1981 ~ 1990   
	0x4970, 0x64b0, 0x74a3, 0xea50, 0x6b58, 0x5ac0, 0xab60, 0x96d5, 0x92e0, 0xc960, // 1991 ~ 2000   

	0xd954, 0xd4a0, 0xda50, 0x7552, 0x56a0, 0xabb7, 0x25d0, 0x92d0, 0xcab5, 0xa950, // 2001 ~ 2010   
	0xb4a0, 0xbaa4, 0xad50, 0x55d9, 0x4ba0, 0xa5b0, 0x5176, 0x52bf, 0xa930, 0x7954, // 2011 ~ 2020   
	0x6aa0, 0xad50, 0x5b52, 0x4b60, 0xa6e6, 0xa4e0, 0xd260, 0xea65, 0xd530, 0x5aa0, // 2021 ~ 2030   
	0x76a3, 0x96d0, 0x4afb, 0x4ad0, 0xa4d0, 0xd0b6, 0xd25f, 0xd520, 0xdd45, 0xb5a0, // 2031 ~ 2040   
	0x56d0, 0x55b2, 0x49b0, 0xa577, 0xa4b0, 0xaa50, 0xb255, 0x6d2f, 0xada0, 0x4b63, // 2041 ~ 2050   
	0x937f, 0x49f8, 0x4970, 0x64b0, 0x68a6, 0xea5f, 0x6b20, 0xa6c4, 0xaaef, 0x92e0, // 2051 ~ 2060   
	0xd2e3, 0xc960, 0xd557, 0xd4a0, 0xda50, 0x5d55, 0x56a0, 0xa6d0, 0x55d4, 0x52d0, // 2061 ~ 2070   
	0xa9b8, 0xa950, 0xb4a0, 0xb6a6, 0xad50, 0x55a0, 0xaba4, 0xa5b0, 0x52b0, 0xb273, // 2071 ~ 2080   
	0x6930, 0x7337, 0x6aa0, 0xad50, 0x4b55, 0x4b6f, 0xa570, 0x54e4, 0xd260, 0xe968, // 2081 ~ 2090   
	0xd520, 0xdaa0, 0x6aa6, 0x56df, 0x4ae0, 0xa9d4, 0xa4d0, 0xd150, 0xf252, 0xd520  // 2091 ~ 2100   
};  

int g_SolarMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   
int g_SolarDays[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

//==== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
int GetLunarLeapMonth(int y) 
{ 
	int lm = g_LunarInfo[y - 1801] & 0xf;   
	return lm == 0xf ? 0 : lm;   
}

//==== 传回农历 y年闰月的天数
int GetLunarLeapDays(int y) 
{
	if(GetLunarLeapMonth(y))   
		return (g_LunarInfo[y - 1800] & 0xf) == 0xf ? 30 : 29;   
	else   
		return 0;   
}

//==== 传回农历 y年的总天数
int GetLunarYearDays(int y) 
{
	int i, sum = 29*12;
	for(i=0x8000; i>0x8; i>>=1) 
	{
		sum += (g_LunarInfo[y-1801] & i)? 1: 0;
	}
	return sum+GetLunarLeapDays(y);
}

//====================================== 传回农历 y年m月的总天数
int GetLunarMonthDays(int y,int m) 
{ 
	if ( y >= 1801 )   
		return (g_LunarInfo[y - 1801] & (0x10000 >> m)) ? 30 : 29; 
	else   
		return 0; 
}

int GetSolarYearDays(int year) 
{ 
	if(year%4==0&&year%100!=0||year%400==0) 
		return 366; 
	else 
		return 365; 
}
int GetSolarMonthDays(int year,int month) 
{ 
	if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) 
		return 31; 
	if(month==4||month==6||month==9||month==11) 
		return 30; 
	if(month==2&&GetSolarYearDays(year)==366) 
		return 29; 
	else 
		return 28; 
} 

int GetDaysFrom0000(int solarYear, int solarMonth,int solarDay)   
{   
	int ofs = solarMonth > 2 && (GetSolarYearDays(solarYear)==366);   
	solarYear--;   
	return solarYear * 365 + solarYear / 4 - solarYear / 100 + solarYear / 400 + g_SolarDays[solarMonth-1] + solarDay + ofs; 
}

//==== 算出农历, 传入日期物件, 传回农历日期物件
//     该物件属性有 .year .month .day .isLeap .yearCyl .dayCyl .monCyl
struct LunarDate GetLunar2(int solaryear, int solarmonth,int day) 
{
	struct LunarDate lunar;
	int i, leap=0, temp=0;
	int offset   = GetDaysFrom0000(solaryear,solarmonth,day) - GetDaysFrom0000(1801, 2, 13);
	lunar.dayCyl = offset + 40;
	lunar.monCyl = 14;  
	for(i=1801; i< 2100 && offset>0; i++) 
	{ 
		temp=GetLunarYearDays(i); 
		if (offset < temp)
			break;
		offset-=temp; 
	}

	lunar.year = i;
	lunar.yearCyl = i-1864;
	leap = GetLunarLeapMonth(i); //闰哪个月
	lunar.isLeap = false;

	for(i=1; i<13; i++) 
	{
		//闰月
		if(leap>0 && i==(leap+1) && !lunar.isLeap)
		{ 
			--i; 
			lunar.isLeap = true; 
			temp = GetLunarLeapDays(lunar.year); 
		}
		else
		{ 
			temp = GetLunarMonthDays(lunar.year, i);
		}
		//解除闰月
		if(lunar.isLeap && i==(leap+1)) 
		{
			lunar.isLeap = false;
		}
		if (offset < temp)
			break;
		offset -= temp;
	}

	lunar.month = i;
	lunar.day = offset + 1;
	return lunar;
}

int main(int argc,char** args)
{
	struct LunarDate lunar = GetLunar2(2011, 8,23);
	cout<<lunar.year<<"-"<<lunar.month<<"-"<<lunar.day<<endl;
	return 0;
}

你可能感兴趣的:(c,struct,2010,X86)