⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wannianli.c

📁 本源码是用c语言编写的一个万年历程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>   
#include<conio.h>
#include<time.h>
typedef struct 
{
	//农历年份
	int lunar_year;
	//农历月份
	int lunar_month;
	//农历日期
	int lunar_day;
	//本月天数
	int current_mday;
	//本年闰月
	int leap_month;
	//闰月标记
	int leap_flag;
}Lunar_info;
typedef struct
{
	//阳历年份
	int era_year;
	//阳历月份
	int era_month;
	//阳历日期
	int era_day;
}Chinese_era;   
int s_year;
int s_month;
int s_day;
/*1900-2050年的农历数据
数据格式说明:
5位十六进制数字 例:04bd8
1位:是否为闰月(30天) 0:不是 1:是
2、3、4位:转换二进制为:0100 1011 1101(1为30天,0为29天)
04bd8表示为(13个月):29,30,29,29,30,29,30,30,30(闰月),30,30,29,30;
5位:如果有闰月,则为月份,没有则为0, 此为8月*/
int lunar_info[]=
{
	0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
	0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
	0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
	0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
	0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
	0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
	0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
	0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
	0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
	0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
	0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
	0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
	0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
	0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
	0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
	0x14b63
};
/*天干*/
char Gan[10][3] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
/*地支*/
char Zhi[12][3] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
/*生肖*/
char Animals[12][3] = {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
/*24节气
char solar_term[24][5] = 
{
	 "小寒","大寒","立春","雨水","惊蛰","春分",
     "清明","谷雨","立夏","小满","芒种","夏至",
     "小暑","大暑","立秋","处暑","白露","秋分",
     "寒露","霜降","立冬","小雪","大雪","冬至"
};*/
/*农历日*/
char chinese_day[30][5] = 
{
	 "初一","初二","初三","初四","初五","初六","初七",
      "初八","初九","初十","十一","十二","十三","十四",
      "十五","十六","十七","十八","十九","廿十","廿一",
      "廿二","廿三","廿四","廿五","廿六","廿七","廿八",
      "廿九","卅十"
};
/*农历月*/
char chinese_month[12][5] = {"正","二","三","四","五","六","七","八","九","十","冬","腊"};
/******************************************************************************************************
** 函数名称:TIME
** 功能描述:取得系统当前时间	
**          如:TIME() 返回系统当前时间
** 输 入:             
** 输 出:  系统当前时间  成功
**  Error:  
** 作 者:  阿龙         
** 日 期:  2007年09月26日
*******************************************************************************************************/
void TIME()                                      
{
	static char *week[]={"日","一","二","三","四","五","六"};
	time_t t;
	struct tm *tp;
	t=time(NULL);
	tp=localtime(&t);
	printf("%d年%02d月%02d日",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday);   
	printf("  %02d:%02d:%02d ",tp->tm_hour,tp->tm_min,tp->tm_sec); 
	printf("星期%s\t       ",week[tp->tm_wday]);
}
/******************************************************************************************************
** 函数名称:buffer
** 功能描述:延迟刷新时间	
**          如:buffer(2) 返回2秒后执行下一操作 
** 输 入:  n           秒
** 输 出:  对应秒数    成功
**  Error:  输入错误    失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
*******************************************************************************************************/
void buffer(int n)                       
{
	time_t start,end;
	start=time(NULL);
    end=time(NULL);
    while(end-start<n)
        end=time(NULL);
}
/*******************************************************************************************************
** 函数名称:leap
** 功能描述:返回阳历某年的天数,闰年返回366天,平年返回365天	
**          如:leap(2000) 返回366天 
** 输 入:  year        公历年
** 输 出:  对应天数    成功
**  Error:  输入错误    失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
********************************************************************************************************/
int leap(int year) 
{ 
	if(year%4==0&&year%100!=0||year%400==0) return 366; 
	else return 365; 
}
/*******************************************************************************************************
** 函数名称:day
** 功能描述:返回阳历当月的天数	
**          如:day(1) 返回31天 
** 输 入:  month       公历月
** 输 出:  对应天数    成功
**  Error:  输入错误    失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
********************************************************************************************************/
int day(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&&leap(s_year)==366) return 29; 
	else return 28; 
} 
/*******************************************************************************************************
** 函数名称:get_solar_total
** 功能描述:返回1900年01月01日到M年M月的天数	
**          如:get_solar_total(2000,01) 返回36555天
** 输 入:  s_year,s_month          
** 输 出:  M年M月-1900-01-01   成功
**  Error:  输入错误              失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
********************************************************************************************************/
int get_solar_total(int solar_year, int solar_month)
{ 
	int total;
	int temp_num;
	total=0;
	for (temp_num=1900;temp_num<solar_year;temp_num++)
	{
		total+=leap(temp_num);
	}
	for(temp_num=1;temp_num<solar_month;temp_num++)
	{
		total+=day(temp_num);
	}
	return total;
}
/*******************************************************************************************************
** 函数名称:get_week
** 功能描述:返回M年M月的1号为星期几,以1900.01.01为基准1为星期日
**          如:get_week(2000,01) 返回星期六
** 输 入:  s_year,s_month          
** 输 出:  M年M月的一号是星期几   成功
**  Error:  输入错误               失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
********************************************************************************************************/
int get_week(int solar_year, int solar_month)
{ 
	int week;
	int total;
	/* 1900.01.01为星期一*/
	week=2; 
	total=get_solar_total(solar_year, solar_month);
	total%=7;
	week=week+total;
	week%=7;
	if (week==0)
		week=7;
	return week;
}
/******************************************************************************************************
** 函数名称:get_leap_month
** 功能描述:确定是否存在农历的闰月	
**          如:get_leap_month(1982) 返回存在闰4月
** 输 入:  lunar_year           
** 输 出:  是否存在闰月  成功
**  Error:  输入错误      失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
*******************************************************************************************************/
int get_leap_month(int lunar_year)
{ 
	return lunar_info[lunar_year-1900]&0xf;
}
/******************************************************************************************************
** 函数名称:get_leap_month_day
** 功能描述:若存在闰月,返回闰月的天数,30?29	
**          如:get_leap_month_day(1982) 返回30天
** 输 入:  lunar_year           
** 输 出:  存在的是大月还是小月  成功
**  Error:  输入错误              失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
*******************************************************************************************************/
int get_leap_month_day(int lunar_year)
{ 
	if (get_leap_month(lunar_year))
		return ((lunar_info[lunar_year-1900]&0x10000)?30:29);
	else
		return(0);
}
/******************************************************************************************************
** 函数名称:get_lunar_month_total
** 功能描述:确定农历当月天数,30?29	
**          如:get_lunar_month_total(1982,4) 返回30天
** 输 入:  lunar_year           
** 输 出:  存在的是大月还是小月  成功
**  Error:  输入错误              失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
*******************************************************************************************************/
int get_lunar_month_total(int lunar_year, int lunar_month)
{ 
	return ((lunar_info[lunar_year-1900]&(0x10000>>lunar_month))?30:29);
}
/******************************************************************************************************
** 函数名称:get_lunar_year_total
** 功能描述:农历当年总天数,354?355	
**          如:get_lunar_year_total(1982) 返回355天
** 输 入:  lunar_year           
** 输 出:  一年的总天数  成功
**  Error:  输入错误      失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
*******************************************************************************************************/
int get_lunar_year_total(int lunar_year)
{ 
	/*348 = 29*12 */
	int sum=348; 
	int i;
	for(i=0x8000;i>0x8; i>>=1)
		sum+=(lunar_info[lunar_year-1900]&i)?1:0;
	return(sum+get_leap_month_day(lunar_year));
}
/******************************************************************************************************
** 函数名称:get_chinese_era
** 功能描述:确定天干地支	
**          如:get_chinese_era(2007-1864,str) 返回str='丁亥'
** 输 入:  s-year-1864,str           
** 输 出:  干+支         成功
**  Error:  输入错误      失败
** 作 者:  阿龙         
** 日 期:  2007年09月26日
*******************************************************************************************************/
void get_chinese_era(int num, char *str)
{ 
	strcpy(str, Gan[num%10]);
	strcat(str, Zhi[num%12]);
}
/*24节气算法
void sTerm()
{}*/
/*返回农历信息*/
int get_lunar_info(int solar_year, int solar_month, Lunar_info *l_info,Chinese_era *china_era)
{ 
	int i;
	int total;
	int temp_num;
	/*阳历1900年01月31日为农历1900年01月01日*/
	total=get_solar_total(solar_year,solar_month)-30;
	china_era->era_day=total+40;
	/*将13和14月作为下一年的1月和2月,便于确定Y年M月D日是星期几
	蔡勒公式求星期几:W=[C/4]-2C+y+[y/4]+[13×(M+1)/5]+d-1*/
	china_era->era_month=14;
	for(i=1900;i<2051&&total>=0;i++)
	{
	  /*temp_num记录从1900到2050年闰年和平年的天数*/
	  temp_num=get_lunar_year_total(i);
	  total-=temp_num;
	  /*累计月*/
	  china_era->era_month+=12;
	}
	l_info->lunar_year=i;
	if(total<0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -