用vb编写万年历小程序

用vb编写万年历小程序,第1张

用VB做万年历,非常关键点就是农历写法,参考代码如下:

#Region " 返回农历 "

'返回农历

'cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28

'cCalendar.MinSupportedDateTime 返回支持的最小日期,即190-2-19

Private cCalendar As New System.Globalization.ChineseLunisolarCalendar

Public Function PubFunGet_CNDate(ByVal sDateTime As Date) As String

cCalendar = New System.Globalization.ChineseLunisolarCalendar

Dim lyear As Integer = cCalendar.GetYear(sDateTime)

Dim lmonth As Integer = cCalendar.GetMonth(sDateTime)

Dim lday As Integer = cCalendar.GetDayOfMonth(sDateTime)

Dim lweek As Integer = cCalendar.GetDayOfWeek(sDateTime)

'获取闰月, 0 则表示没有闰月

Dim leapMonth As Integer = cCalendar.GetLeapMonth(lyear)

Dim isleap As Boolean = False

If (leapMonth >0) Then

If (leapMonth = lmonth) Then

'闰月

isleap = True

lmonth = lmonth - 1

ElseIf (lmonth >leapMonth) Then

lmonth = lmonth - 1

End If

End If

Return String.Concat(GetLunisolarYear(lyear), IIf(isleap = True, "闰年", "年"), GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday))

End Function

'十天干

Private tiangan As String() = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}

'十二地支

Private dizhi As String() = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}

'十二升哪生肖

Private shengxiao As String() = {"鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "枯笑轿猴", "鸡", "狗", "猪"}

'农历月

Private months As String() = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)"}

'农历日

Private days1 As String() = {"初", "十", "廿", "三"}

Private days As String() = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"}

'返回农历年(天干 地支 生肖)

Private Function GetLunisolarYear(ByVal year As Integer) As String

GetLunisolarYear = ""

If (year >3) Then

Dim tgIndex As Integer = (year - 4) Mod 10

Dim dzIndex As Integer = (year - 4) Mod 12

Return tiangan(tgIndex) &dizhi(dzIndex) &"[" &shengxiao(dzIndex) &"]"

End If

'无效的年份!

End Function

'没肆返回生肖

Private Function GetShengXiao(ByVal sDateTime As Date) As String

Return shengxiao(cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(sDateTime)) - 1)

End Function

'返回农历月

Private Function GetLunisolarMonth(ByVal month As Integer) As String

GetLunisolarMonth = ""

If (month <13 AndAlso month >0) Then

Return months(month - 1)

End If

'无效的月份!

End Function

'返回农历日

Private Function GetLunisolarDay(ByVal day As Integer) As String

GetLunisolarDay = ""

If (day >0 AndAlso day <32) Then

If (day <>20 AndAlso day <>30) Then

Return String.Concat(days1((day - 1) \ 10), days((day - 1) Mod 10))

Else

Return String.Concat(days((day - 1) \ 10), days1(1))

End If

End If

'无效的日!

End Function

#End Region

网上找的部分带我,我又修改了一下,圆镇现在基本满足lz的要求。

手头没有Wintc用vc测的,不过没用到什么vc特有的东西,应该可以的。

#include <郑腔升stdio.h>

#include <stdlib.h>

#include <conio.h>

void DayOfWeek()

{

int rtnDay

int y,m,d

char ch

while (1)

{

printf("请输入年月日(xxxx,xx,xx): ")

scanf("%d,%d,%d", &y, &m, &d)

fflush(stdin)

rtnDay = (3*y-(7*(y+(m+9)/12))/4+(23*m)/9+d+17-((y+(m<3?-1:0))/100+1)*3/4) % 7

printf("%04d年%02d月%02d日 是 星期%d\n", y, m, d, rtnDay?rtnDay:7)

printf("继续查询(y/n)")

ch = getchar()

if(ch != 'y' &&ch != 'Y')

break

}

}

void GetMonth(int D, int md, int n)

{

int N ,m,d,y,c,Wd

int i,maxday,i1

if(md>=13||md<=0)

printf("HAVE WRONG!")

else

{

for(i1=0md++,i1++)

{

if(md==0)

{

md=12

D-=1

}

else

if(md==13)

{

md=1

D+=1

}

m=md

printf("\n============%d,%2d============\喊老n",D,md)

if(0<m&&m<=12)

{

switch(m)

{

case 1:maxday=31break

case 2:if(D%4==0&&D%100!=0||D%400==0)

maxday=29

else

maxday=28

break

case 3:maxday=31break

case 4:maxday=30break

case 5:maxday=31break

case 6:maxday=30break

case 7:maxday=31break

case 8:maxday=31break

case 9:maxday=30break

case 10:maxday=31break

case 11:maxday=30break

case 12:maxday=31break

default:printf("HAVE WRONG")

}

if(0<m&&m<=2)

{

N=D-1

m=m+10

}

else

{

N=D

m=m-2

}

c=N/100

y=N%100

Wd=((1+(13*m-1)/5+y+y/4+c/4-2*c)%7+7)%7

printf(" SUN MOU TUE WED TUR FRI SAT\n")

for(i=0i<Wdi++)

printf("%4c",' ')

for(d=1d<=maxdayd++)

{

printf("%4d",d)

Wd=(Wd+1)%7

if(Wd==0)

printf("\n")

}

}

else

printf("HAVE WRONG!")

if(i1==n)

break

}

}

}

void MonthOfYear()

{

char ch

int y, m

while (1)

{

printf("请输入年月(xxxx,xx): ")

scanf("%d,%d", &y, &m)

fflush(stdin)

GetMonth(y, m, 0)

printf("\n继续查询(y/n)")

ch = getchar()

if(ch != 'y' &&ch != 'Y')

break

}

}

void GetYear()

{

char ch

int y

while (1)

{

printf("请输入年: ")

scanf("%d", &y)

fflush(stdin)

GetMonth(y, 1, 11)

printf("\n继续查询(y/n)")

ch = getchar()

if(ch != 'y' &&ch != 'Y')

break

}

}

int main()

{

char ch

while(1)

{

system("cls")

printf("1.查询星期\n")

printf("2.查询月份\n")

printf("3.显示一年\n")

printf("4.退出程序\n")

ch = getchar()

fflush(stdin)

switch(ch)

{

case '1':

DayOfWeek()

break

case '2':

MonthOfYear()

break

case '3':

GetYear()

break

case '4':

printf("是否退出(y/n)")

ch = getchar()

fflush(stdin)

if(ch == 'y' || ch == 'Y')

exit(0)

break

}

}

}

另外,虚机团上产品团购,超级便宜

除了天数差计算,基本上是完成了你所提出的功能(天数差计算其实已经提供了年内序数计算方方法,可以在此基础上完善即可)。包含星期计算、公历转农历、农历转公历、节气查询。数据与算法均为原创,绝非网上复制而来。附消态瞎件中有源代码,同时附带农历数据提取工具,可提取1600到7000年之间的任意年份区间,提取后的数据为压缩数据,占用空间小,同时数据包含数九、三伏、入梅和出梅,非常适合写一份完整的万年历。

部分核心算法函数如下:

//判断闰年,参数:年份,返回值:0-平年,1-闰年

int IsLeapYear(int iYear)

//计算日期在年内的序数,参数:年,月,日,年闭世内序数,返回值:0-失败,1-成功

int GetDaysNumInYear(int iYear, int iMonth, int iDay,int *nDays)

//从年内序数计算月、日,参数:年,年内序数,月,日,返回值:0-失败,1-成功

int GetDateFromDays(int iYear, int nDays, int *iMonth, int *iDay)

//检验年、月、日的合法性,参数:年,月,日,返回值:0-失败,1-成功

int DateCheck(int iYear,int iMonth,int iDay)

//获取农历新年的公历年内序数,参数:农历年,返回值:农历新年的公历年内序数

int LunarGetNewYearOrdinal(int iLunarYear)

//获取农历月的天数,参数:农历年,农历月,是否为闰月,返回值:该农历月的天数,为0代表参数无效

int LunarGetDaysofMonth(int iLunarYear,int iLunarMonth,int IsLeapMonth)

//展开大小月数据表(某一年的),参数:农历年,从上一拿空年十一月开始到当前年份(闰)十二月的每月天数,返回值:0-失败,1-成功

int LunarExpandDX(int iLunarYear,int iDayOfMonth[15])

//获取农历某一年的闰月情况,参数:农历年,返回值,该年的闰月月份,0表示无闰月

int LunarGetLeapMonth(int iLunarYear)

//公历转农历,参数:公历年、月、日,农历年、月、日,是否为闰月,返回值:0-失败,1-成功

int Gongli2Nongli(int iYear,int iMonth,int iDay,int *iLunarYear,int *iLunarMonth,int *iLunarDay,int *iIsLeapMonth)

//农历转公历,参数:家历年、月、日,是否为闰月,公历年、月、日,返回值:0-失败,1-成功

int Nongli2Gongli(int iLunarYear,int iLunarMonth,int iLunarDay,int iIsLeapMonth,int *iYear,int *iMonth,int *iDay)

//得到指定年份的节气信息,首个是小寒

int GetJieQi(int iYear,int uMonth,unsigned int uJieQi[2])

//计算星期

unsigned int GetDayOfWeek(int iYear,int uMonth,int uDay)

源代码请看

吧。提取码:ooj7


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12522274.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存