prayer怎么读:一道VC++的题,会的请进

来源:百度文库 编辑:高考问答 时间:2024/05/05 15:56:41
输入日、月、年,判断星期几?知道请回答,呵呵,谢谢了
如果需要邮箱,请发到guofeini@126.com
我想要的是代码,别的我不懂啊

我有哦!
算法是程序员杂志01年的方法,查你邮箱吧

我认为如下:(知识思路)

先把固定一天的年月日星期放进去(一个对象)构造
然后设计 一个计算闰年的函数
一个计算某天至这个固定天的间隔多少天算出来,然后再除以7,余下来的再用一个函数判断.

同意楼上

******************************************************/
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include "wnl.h"

#define WEEK_STARTX 4
#define WEEK_STARTY 4

#define YL_DATE_STARTX WEEK_STARTX
#define YL_DATE_STARTY WEEK_STARTY+2

#define NL_DATE_STARTX WEEK_STARTX
#define NL_DATE_STARTY WEEK_STARTY+3

void 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;i<7;i++)
{
gotoxy(WEEK_STARTX+i*10,WEEK_STARTY);
printf("%s",WeekName[i]);
}
printf("\n");
printf("\n");
}
void printfDay(int iStartWeekDay,MYDATE structYlDate,
MYDATE structNlDate)
{
int i,j;
int iDayNum=1;
int offsetX=0,offsetY=0;
int iNlMonthDayNum,iMonthDay;
MYDATE nextMonthInfo;
iMonthDay = yl_MonthDays[structYlDate.iMonth];
if( structYlDate.iMonth == 2 )
{
if( (structYlDate.iYear%4==0)||
((structYlDate.iYear % 100== 0)&&(structYlDate.iYear % 400 == 0)) )
{
iMonthDay ++ ;
}
}
for(i=1;i<42;i++)
{
if(i>iStartWeekDay)
{
/*
打印阳历数据
*/
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 <= structYuanDanDate.iMonth && iFirstFlag ==1 )
{
if(structYlDate.iMonth == structYuanDanDate.iMonth)
{
if(structYlDate.iDay<structYuanDanDate.iDay && iFirstFlag ==1)
{
iFirstFlag=0;
structYuanDanDate.iYear--;
goto InitYear;
}
}
else
{
iFirstFlag=0;
structYuanDanDate.iYear--;
goto InitYear;
}
}
/*计算给定日期到元旦的天数*/
SepDayNum=GetSepDayNum(structYuanDanDate,structYlDate);
/*根据要计算的日期距新年的天数以及这一年的农历数据计算出所求日期是阳历的月分*/

nlMonth = 1;
nlDay = 1;
memcpy(tmp1,daList[structYuanDanDate.iYear-1900]+12, 1); /*闰月大小月*/
memcpy(runyue,daList[structYuanDanDate.iYear-1900]+13,1);/*闰月月份*/
for(i = 1;i<=SepDayNum;i++)
{
nlDay++;
memset(tmp,0,2);
memcpy(tmp,daList[structYuanDanDate.iYear-1900]+nlMonth-1, 1);
if( (nlDay == 30 +atoi(tmp) ) ||
(RunYue && (nlDay = 30 + atoi(tmp1)) ) )
{
if( (RunYue == 0) && (nlMonth == atoi(runyue)) )
{
RunYue = 1;
}
else
{
RunYue = 0;
nlMonth++;
}
nlDay=1;
}
}
/*
所对应的农历年份与春节年份相同,月份和日期为所计算的值
*/
structNlDate->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;
}

}

int GetSepDayNum(MYDATE structYlDate0 ,
MYDATE structYlDate1 )
{
int iSepDayNum,i;
/*
规定iMonth0和iDay0为基准时间,如果要计算的日期比基准时间小,
则返回无效值-1
*/
if(structYlDate1.iYear < structYlDate0.iYear)
{
return -1;
}
else
{
if(structYlDate1.iYear == structYlDate0.iYear)
{
if(structYlDate1.iMonth <= structYlDate0.iMonth)
{
if(structYlDate1.iMonth == structYlDate0.iMonth)
{
if(structYlDate1.iDay < structYlDate0.iDay )
{
return -1;
}
}
else
{
return -1;
}
}
}
}
/*
需要分3种情况进行分析:
1.要求的日期与春节不同年
2.要求的日期与春节同年,不同月
3.要求的日期与春节同年,同月,不同日
*/
if(structYlDate1.iYear>structYlDate0.iYear)/*不同年*/
{
/*iYear0年不足月的天数*/
iSepDayNum=yl_MonthDays[structYlDate0.iMonth]-structYlDate0.iDay;
/*iYear0年足月的天数*/
for(i=structYlDate0.iMonth+1;i<13;i++)
{
iSepDayNum += yl_MonthDays[i];
}
/*iYear1年足月的天数*/
for(i=1;i<structYlDate1.iMonth;i++)
{
iSepDayNum += yl_MonthDays[i];
}
/*iYear1年不足月的天数*/
iSepDayNum += structYlDate1.iDay;
/*如果iYear0年春节是2月或2月以前,并且该年为闰年,则天数加1*/
if(structYlDate0.iMonth <= 2)
{
if ( (structYlDate0.iYear%4==0)||
((structYlDate0.iYear % 100== 0)&&(structYlDate0.iYear % 400 == 0)) )
{
iSepDayNum++;
}
}
/*如果要求的日期是iYear1年2月以后,并且该年为闰年,则天数加1*/
if(structYlDate1.iMonth > 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;i<structYlDate1.iMonth;i++)
{
iSepDayNum += yl_MonthDays[i];
}
/*不足月的天数*/
iSepDayNum += structYlDate1.iDay;
if(structYlDate0.iMonth == 2)
{
if ( (structYlDate1.iYear%4==0)||
((structYlDate1.iYear % 100== 0)&&(structYlDate1.iYear % 400 == 0)) )
{
iSepDayNum++;
}
}
}
}
}
return iSepDayNum;
}
void GetMonthInfo(MYDATE *structMonthInfo,MYDATE preMonthInfo)
{
unsigned char tmp[2]={0};
int runyue;
memcpy(tmp,daList[preMonthInfo.iYear-1900]+13,1);/*闰月月份*/
runyue=atoi(tmp);
/*
判断某一个月是否为闰月的条件是:
1.上一个月一定不是闰月
2.该月的月份与闰月的月份值相同
否则,该月一定不是闰月

如果该月是闰月,则一定不会涉及到跨年度的问题,如果该月不是闰月,则可能
涉及到跨年度的问题
*/
if( (preMonthInfo.iMonthIsRun != 1) &&
(preMonthInfo.iMonth == runyue) )
{
structMonthInfo->iYear = 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 < 12)
{
/*年度不变,月份加1*/
structMonthInfo->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);
int GetWeek(MYDATE structYlDate);
void printfWeekName();
int GetSepDayNum(MYDATE structYlDate0 ,
MYDATE structYlDate1 );
void GetMonthInfo(MYDATE *structMonthInfo,MYDATE preMonthInfo);

不用这么复杂,有现成的公式可套.
http://www.sftw.umac.mo/~fstitl/primary/weekday.html
你只要用c++把这个公式表述出来就好了
这么简单的程序不用帮写吧