小弟求解.临表涕零
这是个vc可运行的程序.#include <stdio.h>
int rn(int nianfen)/*判断某年是否是闰年*/{ if( ((nianfen % 4 == 0) && (nianfen % 100 != 0)) || (nianfen % 400 == 0)) return 1; else return 0;}
int yf(int yuefen, int runnian)/*获得当月天数*/{ int dyts; switch(yuefen) { case 1: dyts=31;break; case 2: dyts=28 + runnian;break; case 3: dyts=31;break; case 4: dyts=30;break; case 5: dyts=31;break; case 6: dyts=30;break; case 7: dyts=31;break; case 8: dyts=31;break; case 9: dyts=30;break; case 10: dyts=31;break; case 11: dyts=30;break; case 12: dyts=31;break; } return dyts;}
int yuandan(int nian)/*元旦是星期几*/{ return (nian-1+(nian-1)/4-(nian-1)/100+(nian-1)/400 + 1) % 7;}
void prin(int yuefen,int nian)/*输入月份表*/{ int i,dyts,xqdw,sun=0,x=0; dyts = yf(yuefen,rn(nian));/*获得当月天数*/ printf("\n\t\t\t%d年%d月\n",nian,yuefen); printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n"); for(i=1; i<yuefen; i++)/*计算这个月的第一天是星期几*/ { sun += yf(i,rn(nian)); } xqdw = (sun+yuandan(nian)) % 7; for(i=0; i<xqdw; i++)/*把当月1日的前几天空过去*/ { printf("\t"); x++; } for(i=1; i<=dyts; i++)/*输入月份表*/ { printf("%d",i); x++; if(x == 7) { printf("\n"); x=0; } else { printf("\t"); } } printf("\n");}
void gn1()/*第一功能项*/{ int nian,yue,ri,ri2=0,xq,x=1,i; char pd; printf("请输入年月日(格式:xxxx,xx,xx):"); scanf("%d,%d,%d",&nian,&yue,&ri); for(i=1; i<yue; i++) { ri2 += yf(i,rn(nian)); } ri2 += ri;/*日期是当年的那一天*/ xq = (nian-1+(nian-1)/4-(nian-1)/100+(nian-1)/400+ri2) % 7;/*计算星期*/ do { if(xq == 0) printf("%d年%d月%d日是星期日,是否继续查询(Y/N)\n",nian,yue,ri); else printf("%d年%d月%d日是星期%d,是否继续查询(Y/N)\n",nian,yue,ri,xq); scanf("%c",&pd); scanf("%c",&pd); if(pd == 'y' || pd == 'Y') { x = 1; gn1(); } else { if(pd == 'n' || pd == 'N') { x = 1; } else { x = 0; printf("选择错误请重新输入:\n"); } } } while(x != 1);}
void gn2()/*第二功能项*/{ int nian,x=1; char pd; printf("请输入要查哪一年? (格式:xxxx):"); scanf("%d",&nian); do { if(rn(nian) == 1) printf("%d年是闰年,是否继续查询(Y/N)?",nian); if(rn(nian) == 0) printf("%d年是平年,是否继续查询(Y/N)?",nian); scanf("%c",&pd); scanf("%c",&pd); if(pd == 'y' || pd == 'Y') { x = 1; gn2(); } else { if(pd == 'n' || pd == 'N') { x = 1; } else { x = 0; printf("选择错误请重新输入:\n"); } } } while(x != 1);}
void gn3()/*第三功能项*/{ int nian,x=1,i; char pd; printf("请输入要打印的年份(格式:xxxx):"); scanf("%d",&nian); do { if(x == 1)/*正常输出*/ { printf("\n\n"); for(i=1; i<=12; i++)/*输出月份表*/ { prin(i,nian); } } printf("是否继续打印(Y/N)?"); scanf("%c",&pd); scanf("%c",&pd); if(pd == 'y' || pd == 'Y') { x = 1; gn3(); } else { if(pd == 'n' || pd == 'N') { x = 1; } else { x = 0; printf("选择错误请重新输入:\n"); } } } while(x != 1);}void gn4()/*第四功能项*/{int y,m,d,i;int days,m2;int day=0;int num[12];printf("请输入今天的日期:(年-月-日) ");scanf("%d-%d-%d",&y,&m,&d);if((y%4==0&&y%100!=0)||y%400==0){m2=29;}else{m2=28;}if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){days=31;}if(m==4||m==6||m==9||m==11){days=30;}if(m==2){days=m2;}num[0]=31;num[1]=m2;num[2]=31;num[3]=30;num[4]=31;num[5]=30;num[6]=31;num[7]=31;num[8]=30;num[9]=31;num[10]=30;num[11]=31;if(d<=days){for(i=0;i<=m-1;i++){day+=num[i];}day+=d;printf("\n今天是%d年的第%d天\n",y,day);}else{printf("\n输入日期无效!\n");}}int caidan()/*菜单项*/{ int x; printf("万年历功能菜单\n1. 查询某年某月某日是星期几\n2. 查询某年是否是闰年\n3. 打印某年的全年日历\n4. 计算输入日期是今年第几天\n5. 退出\n"); printf("请输入您的选择:"); scanf("%d",&x); switch(x) { case 1:gn1();return 0; case 2:gn2();return 0; case 3:gn3();return 0; case 4:gn4();return 0; case 5:return -1; default:printf("输入错误请重新输入!\n");return 0; }}
void main(){ int pd=0; do { printf("\n\n"); if(pd == 0) { pd = caidan(); } } while(pd != -1); printf("程序成功退出!\n");}
这也是个可运行的程序.
#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; }
小弟想将第二个程序嵌入到第一个程序中.作为菜单选项中的一个功能.怎么做呢?
[ 本帖最后由 hjx_67 于 2010-5-10 21:27 编辑 ]