一道VC++的题,会的请进

[复制链接]
查看11 | 回复3 | 2006-6-24 16:11:07 | 显示全部楼层 |阅读模式
我认为如下:(知识思路)先把固定一天的年月日星期放进去(一个对象)构造然后设计 一个计算闰年的函数一个计算某天至这个固定天的间隔多少天算出来,然后再除以7,余下来的再用一个函数判断.
回复

使用道具 举报

千问 | 2006-6-24 16:11:07 | 显示全部楼层
同意楼上
回复

使用道具 举报

千问 | 2006-6-24 16:11:07 | 显示全部楼层
******************************************************/#include #include #include #include #include #include "wnl.h"#define WEEK_STARTX
4#define WEEK_STARTY
4#define YL_DATE_STARTXWEEK_STARTX#define YL_DATE_STARTYWEEK_STARTY+2#define NL_DATE_STARTXWEEK_STARTX#define NL_DATE_STARTYWEEK_STARTY+3void main(){MYDATE structNlDate,structYlDate;int iWeek;system("cls");printf(" Input the year:"); scanf("%d",&structYlDate.iYear);printf(" Input the month:"); scanf("%d",&structYlDate.iMonth);structYlDate.iDay=1;iWeek=GetWeek(structYlDate);GetOfNongLiDate(&structNlDate,structYlDate);printfWeekName();printfDay(iWeek,structYlDate,structNlDate);printf("\n");/*printf("month day%d, %d , %d" ,structNlDate.iYear,structNlDate.iMonth,structNlDate.iDay);*/getch();}/*******************************************************************************根据1900年星期的月份代码及星期与月份年份的关系计算1900年星期的月份代码一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月1 4 4
0
2 5
0 3
6
1
4
6从设置代码中找规律平年规律:有数月代码相同:1和10月,2、3和11月,4和7月,9和12月相同,无相同的只有5、6、8月代码变化规律:代码相同月份,代码数逐年增加闰年规律(2004为闰年):有数月代码相同:1-4-7,2-8,3-11,9-12代码变化规律:从2月闰日之后有变化,从三月起代码+2********************************************************************************/int GetWeek(MYDATE structYlDate){unsigned char u_1900_WeekCode[]={0,
1,4,4,0,2,5,0,3,6,1,4,6};int iRunNianNum, iPingNianNum,iCalWeek;/*计算平年和闰年数注意运算:1901-1900年的间隔为0=1901-1900-1;1902-1900年的间隔为1=1902-1900-1*/iRunNianNum = (structYlDate.iYear - 1900 - 1) / 4;iPingNianNum = structYlDate.iYear - 1900 - iRunNianNum;iCalWeek = u_1900_WeekCode[structYlDate.iMonth] + iRunNianNum * 2 + iPingNianNum;
/*如果为闰年,从2月闰日之后有变化,从三月起代码+2*/if (structYlDate.iMonth > 2){
if ( (structYlDate.iYear%4==0)||
((structYlDate.iYear % 100== 0)&&(structYlDate.iYear % 400 == 0)) )
{
iCalWeek = iCalWeek + 1;
}}iCalWeek %=7;return iCalWeek;}void printfWeekName(){unsigned char WeekName[][7]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};int i;printf("\n");for(i=0;iiStartWeekDay)
{
/*
打印阳历数据
*/
gotoxy(YL_DATE_STARTX+offsetX,YL_DATE_STARTY+offsetY);
printf("%d",iDayNum);
iDayNum++;
/*
打印阴历数据
*/
gotoxy(NL_DATE_STARTX+offsetX,NL_DATE_STARTY+offsetY);
if(structNlDate.iDay == 1)
{
if(structNlDate.iMonthIsRun == 1)
{
printf("闰");
}
printf("%s",nl_MonthName[structNlDate.iMonth]);
if(structNlDate.iMonthDayNum == 30)
{
printf("大");
}
else
{
printf("小");
}
}
else
{
printf("%s" ,nl_DayName[structNlDate.iDay]);
}
structNlDate.iDay++;
if(structNlDate.iDay>structNlDate.iMonthDayNum)
{
GetMonthInfo(&nextMonthInfo,structNlDate);
structNlDate.iYear = nextMonthInfo.iYear;
structNlDate.iMonth = nextMonthInfo.iMonth;
structNlDate.iMonthDayNum = nextMonthInfo.iMonthDayNum;
structNlDate.iMonthIsRun = nextMonthInfo.iMonthIsRun;
structNlDate.iDay=1;
}
/*X向的偏移量增加*/
offsetX+=10;
if( (i % 7) == 0 )
{
offsetX=0;
offsetY+=3;
}
if(iDayNum>iMonthDay)
{
break;
}
}
else
{
gotoxy(YL_DATE_STARTX+offsetX,YL_DATE_STARTY+offsetY);
offsetX+=10;
}}}void GetOfNongLiDate(MYDATE *structNlDate,MYDATE structYlDate){
MYDATE structYuanDanDate;/*农历正月初一对应的阳历日期*/
/*int structYuanDanDate.iYear,structYuanDanDate.iMonth,structYuanDanDate.iDay;*/
int nlMonth,nlDay; /*所对应的农历日期*/
unsigned char tmp[3]={0},tmp1[2]={0},runyue[2]={0};
int SepDayNum;/*所要计算的日期距本年新年的天数*/
int RunYue=0,iFirstFlag=1,i;
structYuanDanDate.iYear=structYlDate.iYear;
InitYear:
/*得到新年(农历正月初一)所对应的阳历日期*/
memset(tmp,0,2);
memcpy(tmp,daList[structYuanDanDate.iYear-1900]+14,2);
structYuanDanDate.iMonth = atoi(tmp);
memset(tmp,0,2);
memcpy(tmp,daList[structYuanDanDate.iYear-1900]+16,2);
structYuanDanDate.iDay = atoi(tmp);
/*如果要换算阳历的日期比当年的新年(农历正月初一)的日期早,则应该根据上一年的数据来
计算农历的日期,否则以当年的数据为准即可*/
if( structYlDate.iMonth iYear= structYuanDanDate.iYear;
structNlDate->iMonth = nlMonth;
structNlDate->iDay = nlDay;
structNlDate->iMonthIsRun = RunYue;
if(RunYue != 1)
{
memcpy(tmp,daList[structNlDate->iYear-1900]+structNlDate->iMonth-1,1);
}
else
{
memcpy(tmp,daList[structNlDate->iYear-1900]+12,1);
}
if( atoi(tmp) == 1)
{
structNlDate->iMonthDayNum = 30;
}
else
{
structNlDate->iMonthDayNum = 29;
}
}
intGetSepDayNum(MYDATE structYlDate0 ,
MYDATE structYlDate1 ){int iSepDayNum,i;/*规定iMonth0和iDay0为基准时间,如果要计算的日期比基准时间小,则返回无效值-1*/if(structYlDate1.iYear structYlDate0.iYear)/*不同年*/{
/*iYear0年不足月的天数*/
iSepDayNum=yl_MonthDays[structYlDate0.iMonth]-structYlDate0.iDay;
/*iYear0年足月的天数*/
for(i=structYlDate0.iMonth+1;i 2)
{
if ( (structYlDate1.iYear%4==0)||
((structYlDate1.iYear % 100== 0)&&(structYlDate1.iYear % 400 == 0)) )
{
iSepDayNum++;
}
}}else{
if(structYlDate1.iMonth >= structYlDate0.iMonth)
{
if(structYlDate1.iMonth == structYlDate0.iMonth)/*同年,同月,不同日*/
{
iSepDayNum = structYlDate1.iDay-structYlDate0.iDay;
}
else/*同年,不同月*/
{
/*不足月的天数*/
iSepDayNum=yl_MonthDays[structYlDate0.iMonth]-structYlDate0.iDay;
/*足月的天数*/
for(i=structYlDate0.iMonth+1;iiYear = preMonthInfo.iYear;
structMonthInfo->iMonth = preMonthInfo.iMonth;
structMonthInfo->iDay = preMonthInfo.iDay;
structMonthInfo->iMonthIsRun = 1;
memcpy(tmp,daList[structMonthInfo->iYear-1900]+13,1);/*闰月月份*/
if(atoi(tmp)==1)
{
structMonthInfo->iMonthDayNum = 30;
}
else
{
structMonthInfo->iMonthDayNum = 29;
}}else {
/*
是否跨年度
*/
if(preMonthInfo.iMonth iYear = preMonthInfo.iYear;
structMonthInfo->iMonth = preMonthInfo.iMonth+1;
}
else
{
/*年度加1,月份变为1月*/
structMonthInfo->iYear = preMonthInfo.iYear+1;
structMonthInfo->iMonth = 1;
}
structMonthInfo->iDay = preMonthInfo.iDay;
memcpy(tmp,daList[structMonthInfo->iYear-1900]+structMonthInfo->iMonth-1,1);
if(atoi(tmp) == 1)
{
structMonthInfo->iMonthDayNum = 30;
}
else
{
structMonthInfo->iMonthDayNum = 29;
}
structMonthInfo->iMonthIsRun = 0;}}头文件文件为: /*天干,地支,属象,农历月份名,农历日期名,阳历每月的天数*/unsigned char TianGanName[][3]={"甲","乙","丙",",丁","戊","已","庚","辛","壬","癸"};unsigned char DiZhiName[][3]={"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};unsigned char ShuXingName[][3]={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};unsigned char nl_MonthName[][7]={"",
"正月","二月","三月","四月","五月","六月","七月","八月","九月",
"十月","十一月","十二月"};unsigned char nl_DayName[][5]={"",
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};int yl_MonthDays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};/******************************************************1900 to 1909年的农历数据日期数据定义方法如下前12个字节代表1-12月为大月或是小月,1为大月30天,0为小月29天,第13位为闰月的情况,1为大月30天,0为小月29天,第14位为闰月的月份,如果不是闰月为0,否则给出月份,10、11、12分别用A、B、C来表示,即使用16进制。最后4位为当年家农历新年-即农历1月1日所在公历的日期,如0131代表1月31日。*******************************************************/unsigned char daList[][18] ={"010010110110180131","010010101110000219","101001010111000208","010100100110150129","110100100110000216","110110010101000204","011010101010140125","010101101010000213","100110101101000202","010010101110120122","010010101110000210","101001001101160130","101001001101000218","110100100101000206","110101010100150126","101101010101000214","010101101010000204","100101101101020123","100101011011000211","010010011011170201","010010011011000220","101001001011000208","101100100101150128","011010100101000216","011011010100000205","101011011010140124","001010110110000213","100101010111000202","010010010111120123","010010010111000210","011001001011060130","110101001010000217","111010100101000206","011011010100150126","010110101101000214","001010110110000204","100100110111030124","100100101110000211","110010010110170131","110010010101000219","110101001010000208","110110100101060127","101101010101000215","010101101010000205","101010101101140125","001001011101000213","100100101101000202","110010010101120122","101010010101000210","101101001010170129","011011001010000217","101101010101000206","010101011010150127","010011011010000214","101001011011000203","010100101011130124","010100101011000212","101010010101080131","111010010101000218","011010101010000208","101011010101060128","101010110101000215","010010110110000205","101001010111040125","101001010111000213","010100100110000202","111010010011030121","110110010101000209","010110101010170130","010101101010000217","100101101101000206","010010101110150127","010010101101000215","101001001101000203","110100100110140123","110100100101000211","110101010010180131","101101010100000218","101101011010000207","100101101101060128","100101011011000216","010010011011000205","101001001011140125","101001001011000213","1011001001011A0202","011010100101000220","011011010100000209","101011011010060129","101010110110000217","100100110111000206","010010010111150127","010010010111000215","011001001011000204","011010100101030123","111010100101000210","011010110010180131","010110101100000219","101010110110000207","100100110110150128","100100101110000216","110010010110000205","110101001010140124","110101001010000212","110110100101000201","010110101010120122","010101101010000209","101010101101170129","001001011101000218","100100101101000207","110010010101150126","101010010101000214","101101001010000214",
};
typedef struct _tagMyDate { int iYear; int iMonth; int iDay; int iMonthDayNum; /*本月天数,只有农历本字段才有用*/ int iMonthIsRun;/*如果所给的日期为润月,则该值为1,否则为0,只有农历本字段才有用*/}MYDATE;
void GetOfNongLiDate(MYDATE *nlDate,MYDATE ylDate);void printfDay(int iStartWeekDay,MYDATE structYlDate,
MYDATE structNlDate);intGetWeek(MYDATE structYlDate);void printfWeekName(); intGetSepDayNum(MYDATE structYlDate0 ,
MYDATE structYlDate1 );
void GetMonthInfo(MYDATE *structMonthInfo,MYDATE preMonthInfo);
回复

使用道具 举报

千问 | 2006-6-24 16:11:07 | 显示全部楼层
不用这么复杂,有现成的公式可套.http://www.sftw.umac.mo/~fstitl/primary/weekday.html你只要用c++把这个公式表述出来就好了 这么简单的程序不用帮写吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行