#include <stdio.h> #include <string.h>
/****** 1900 - 2049 阴历数据 ****** 数据格式说明
5位十六进制数字 例:04bd8
1位:闰月是否为大月(30天) 0:不是 1:是 2、3、4位:转换二进制为:0100 1011 1101 12个月的大小月 0:小月 1:大月 5位:闰月的月份 此为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,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0, 0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0, 0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,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}; char Gan[10][3] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"}; char Zhi[12][3] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"}; char Shu[12][3] = {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"}; char solar_term[24][5] = {"小寒","大寒","立春","雨水","惊蛰","春分", "清明","谷雨","立夏","小满","芒种","夏至", "小暑","大暑","立秋","处暑","白露","秋分", "寒露","霜降","立冬","小雪","大雪","冬至"}; char chinese_num[30][5] = {"初一","初二","初三","初四","初五","初六","初七", "初八","初九","初十","十一","十二","十三","十四", "十五","十六","十七","十八","十九","廿十","廿一", "廿二","廿三","廿四","廿五","廿六","廿七","廿八", "廿九","卅"}; char chinese_month[12][5] = {"一","二","三","四","五","六","七","八","九","十", "十一","十二"};
typedef struct{ int lunar_year; /* 年 */ int lunar_month; /* 月 */ int lunar_day; /* 日 */ int lunar_mday; /* 本月天数 */ int leap_month; /* 本年闰月 */ int leap_state; /* 本月是否为闰月 0:非闰月 1:闰月*/ } Lunar_info;
typedef struct{ int chinese_era_year; int chinese_era_month; int chinese_era_day; } Chinese_era;
main() { int s_year; int s_month; int s_day; int week; int l_year; int l_month; int l_day;
int s_month_total_day; int l_month_total_day; int l_year_total_day; int l_leap_month; int l_leap_month_day; int space_num; int c_num; int i,j; char tmp_str[9]; Chinese_era c_ear; Lunar_info l_info;
printf("### 日历 1900-2100 ###\n\n"); while (1) { printf("\n请输入年:"); memset(tmp_str, 0, sizeof(tmp_str)); gets(tmp_str); s_year = atoi(tmp_str);
if ((s_year < 1900) || (s_year > 2100)) { printf("年份输入错误 !!!\n"); continue; }
printf("请输入月:"); memset(tmp_str, 0, sizeof(tmp_str)); gets(tmp_str); s_month = atoi(tmp_str);
if ((s_month < 1) || (s_month > 12)) { printf("月份输入错误 !!!\n"); continue; }
week = get_solar_week(s_year, s_month);
s_month_total_day = get_solar_month_total_day(s_year, s_month);
memset(&c_ear, 0, sizeof(c_ear)); memset(&l_info, 0, sizeof(l_info));
get_lunar_info(s_year, s_month, &l_info, &c_ear);
memset(tmp_str, 0, sizeof(tmp_str)); get_chinese_era(s_year - 1864, tmp_str);
printf(" 阳历:%d年%02d月 阴历:%s年 【%s】\n", s_year, s_month, tmp_str, Shu[(s_year - 1864) % 12]); printf("------------------------------------------------------\n"); printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六\n");
printf(" ");
space_num = week;
for (i = 1; i < space_num; i++) { printf(" "); }
c_num = 0;
for (i = 1; i <= s_month_total_day; i++) { s_day = i; printf("%02d", s_day);
week += 1;
if ((week > 7) || (i == s_month_total_day)) { week = 1; printf("\n");
if (i < 7) { for (j = 1; j < space_num; j++) { printf(" "); }
week = space_num; } else week = 1;
while(1) { week += 1;
memset(tmp_str, 0, sizeof(tmp_str)); out_chinese(&l_info, &tmp_str);
printf("% 6s", tmp_str);
if (l_info.lunar_day < l_info.lunar_mday) l_info.lunar_day = l_info.lunar_day + 1; else { if ((l_info.lunar_month == l_info.leap_month) && l_info.leap_state == 0) { l_info.lunar_mday=get_leap_month_day(l_info.lunar_year); l_info.leap_state = 1; } else { l_info.lunar_month = l_info.lunar_month + 1;
if (l_info.lunar_month > 12) { l_info.lunar_month = 1;
l_info.lunar_year = l_info.lunar_year + 1; }
l_info.lunar_mday = get_lunar_month_total_day(l_info.lunar_year, l_info.lunar_month);
l_info.leap_state = 0; } l_info.lunar_day = 1; }
c_num ++;
if ((week > 7) || (c_num == s_month_total_day)) { week = 1; printf("\n "); break; } else printf(" "); } } else printf(" "); }
printf("\n------------------------------------------------------\n"); }
}
/*** 确定1900年01月01日到M年M月的天数 ***/ int get_solar_total_day(int solar_year, int solar_month) { int total_day; int temp_num;
total_day = 0;
for (temp_num = 1900; temp_num < solar_year; temp_num++) { if ((((temp_num % 4) == 0) && ((temp_num % 100) != 0)) || (temp_num % 400 == 0)) { total_day += 366; } else { total_day += 365; } }
for(temp_num = 1;temp_num < solar_month; temp_num++) { switch(temp_num) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: total_day += 31; break; case 2: if ((((solar_year % 4) == 0) && ((solar_year % 100) != 0)) || (solar_year % 400 == 0)) { total_day += 29; } else { total_day += 28; } break; case 4: case 6: case 9: case 11: total_day += 30; break; } }
return total_day; }
/*** 确定当月1日为星期几 以1900.01.01为基准 1为星期日 ***/ int get_solar_week(int solar_year, int solar_month) { int week; int total_day;
week = 2; /* 1900.01.01 为星期一 */
total_day = get_solar_total_day(solar_year, solar_month);
total_day %= 7; week = week + total_day; week %= 7;
if (week == 0) week = 7;
return week; }
/*** 确定阳历当月天数 ***/ int get_solar_month_total_day(int solar_year, int solar_month) { int solar_month_total_day;
switch(solar_month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: solar_month_total_day = 31; break; case 2: if ((((solar_year % 4) == 0) && ((solar_year % 100) != 0)) || (solar_year % 400 == 0)) { solar_month_total_day = 29; } else { solar_month_total_day = 28; } break; case 4: case 6: case 9: case 11: solar_month_total_day = 30; break; }
return solar_month_total_day; }
/*** 确定闰月 ***/ int get_leap_month(int lunar_year) { return lunar_info[lunar_year - 1900] & 0xf; }
/*** 确定闰月天数 ***/ 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); }
/*** 确定阴历当月天数 ***/ int get_lunar_month_total_day(int lunar_year, int lunar_month) { return ((lunar_info[lunar_year - 1900] & (0x10000 >> lunar_month))? 30:29); }
/*** 阴历当年总天数 ***/ int get_lunar_year_total_day(int lunar_year) { int sum = 348; /* 348 = 29*12 */ int i;
for(i = 0x8000; i > 0x8; i = (i>>1)) sum += (lunar_info[lunar_year - 1900] & i)? 1:0;
return(sum + get_leap_month_day(lunar_year)); }
/*** 确定天干地支 ***/ int get_chinese_era(int num, char *str) { strcpy(str, Gan[num % 10]); strcat(str, Zhi[num % 12]); }
int get_lunar_info(int solar_year, int solar_month, Lunar_info *l_info, Chinese_era *chinese_ear) { int i; int total_day; int temp_num; char str[200];
/* 阳历1900年01月31日为 阴历1900年01月01日 */ total_day = get_solar_total_day(solar_year, solar_month) - 30;
chinese_ear->chinese_era_day = total_day + 40;
chinese_ear->chinese_era_month = 14;
for(i = 1900; i < 2050 && total_day > 0; i++) { temp_num = get_lunar_year_total_day(i); total_day -= temp_num; chinese_ear->chinese_era_month += 12; }
l_info->lunar_year = i;
if(total_day < 0) { total_day += temp_num; l_info->lunar_year--; chinese_ear->chinese_era_month -= 12; }
chinese_ear->chinese_era_year = l_info->lunar_year - 1864;
l_info->leap_month = get_leap_month(l_info->lunar_year);
l_info->leap_state = 0;
for(i = 1; i < 13 && total_day > 0; i++) { if (l_info->leap_month > 0 && i == (l_info->leap_month + 1) && l_info->leap_state == 0) { --i; temp_num = get_leap_month_day(l_info->lunar_year); l_info->leap_state = 1; } else { temp_num = get_lunar_month_total_day(l_info->lunar_year, i); }
if (l_info->leap_state==1 && i == (l_info->leap_month + 1)) l_info->leap_state = 0;
total_day -= temp_num;
if (l_info->leap_state == 0) chinese_ear->chinese_era_month ++; }
if(total_day == 0 && l_info->leap_month>0 && i==l_info->leap_month+1) if(l_info->leap_state == 1) { l_info->leap_state = 0; } else { l_info->leap_state = 1; --i; --chinese_ear->chinese_era_month; }
if(total_day < 0) { total_day += temp_num; --i; --chinese_ear->chinese_era_month; }
l_info->lunar_month = i; l_info->lunar_day = total_day + 1;
if (l_info->leap_state == 0) { l_info->lunar_mday = get_lunar_month_total_day(l_info->lunar_year, l_info->lunar_month); } else l_info->lunar_mday = get_leap_month_day(l_info->lunar_year);
return 0; }
int out_chinese(Lunar_info *l_info, char *chinese_str) { if (l_info->lunar_day != 1) strcpy(chinese_str, chinese_num[l_info->lunar_day - 1]); else { if (l_info->leap_state == 1) { strcpy(chinese_str, "闰"); strcat(chinese_str, chinese_month[l_info->lunar_month - 1]);
strcat(chinese_str, "月"); } else { strcpy(chinese_str, chinese_month[l_info->lunar_month - 1]);
strcat(chinese_str, "月"); } }
return 0; }
----------------解决方案--------------------------------------------------------
顶!
----------------解决方案--------------------------------------------------------
顶;
有个问题想请教各位一下:
C语言编写的程序中怎么显示汉字啊?
----------------解决方案--------------------------------------------------------
顶,大大的顶
----------------解决方案--------------------------------------------------------
好 不错
----------------解决方案--------------------------------------------------------
顶;
有个问题想请教各位一下:
C语言编写的程序中怎么显示汉字啊?
楼主写得很好啊!
PS:回lkwhq,不是C语言不能显示汉字,是编译器的问题,不要用TC了.换个别的.比如Dev-C++, VC++6.0,C-free
[此贴子已经被作者于2007-9-24 18:13:42编辑过]
----------------解决方案--------------------------------------------------------
看来不仅对程序非常的精通外而且还精通万年历。。
不错。。
我喜欢这样的人才~~~!
继续努力
前途无量
----------------解决方案--------------------------------------------------------
流程图是什么样子的啊!
----------------解决方案--------------------------------------------------------
呵呵,只能说一句厉害哦!
----------------解决方案--------------------------------------------------------
编写结构排好一点就好啦
----------------解决方案--------------------------------------------------------