看看这段根据日期计算星座的代码有没有问题。

yongbo 2004-05-02 11:11:43
Public Function funcWhatConstellation(iMonthNum As Integer, iDayNum As Integer) As Integer
''以下是各星座对应函数返回代码和星座日期
'' 1魔羯座(12/22-01/19) 2水瓶座(01/20-02/18) 3双鱼座(02/19-03/20)
'' 4白羊座(03/21-04/19) 5金牛座(04/20-05/20) 6双子座(05/21-06/21)
'' 7巨蟹座(06/22-07/22) 8狮子座(07/23-08/22) 9处女座(08/23-09/22)
''10天秤座(09/23-10/23) 11天蝎座(10/24-11/22) 12射手座(11/23-12/21)

Select Case iMonthNum
Case 1
If iDayNum <= 19 Then
funcWhatConstellation = 1
Else
funcWhatConstellation = 2
End If
Case 2
If iDayNum <= 18 Then
funcWhatConstellation = 2
Else
funcWhatConstellation = 3
End If
Case 3
If iDayNum <= 20 Then
funcWhatConstellation = 3
Else
funcWhatConstellation = 4
End If
Case 4
If iDayNum <= 19 Then
funcWhatConstellation = 4
Else
funcWhatConstellation = 5
End If
Case 5
If iDayNum <= 20 Then
funcWhatConstellation = 5
Else
funcWhatConstellation = 6
End If
Case 6
If iDayNum <= 21 Then
funcWhatConstellation = 6
Else
funcWhatConstellation = 7
End If
Case 7
If iDayNum <= 22 Then
funcWhatConstellation = 7
Else
funcWhatConstellation = 8
End If
Case 8
If iDayNum <= 22 Then
funcWhatConstellation = 8
Else
funcWhatConstellation = 9
End If
Case 9
If iDayNum <= 22 Then
funcWhatConstellation = 9
Else
funcWhatConstellation = 10
End If
Case 10
If iDayNum <= 23 Then
funcWhatConstellation = 10
Else
funcWhatConstellation = 11
End If
Case 11
If iDayNum <= 22 Then
funcWhatConstellation = 11
Else
funcWhatConstellation = 12
End If
Case 12
If iDayNum <= 21 Then
funcWhatConstellation = 12
Else
funcWhatConstellation = 1
End If
End Select

End Function
...全文
140 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlueBeer 2004-05-08
  • 打赏
  • 举报
回复
个人认为,northwolves(野性的呼唤)的算法比较棒,眼毒啊
BlueBeer 2004-05-08
  • 打赏
  • 举报
回复
根据 northwolves(野性的呼唤)的代码修改的:

Function XingZuo(M As Long, D As Long) As String
Dim XZ As String, RQ As String, P As Long
XZ = "魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手"
RQ = "191820192021222222232221"
P = (IIf(D <= Val(Mid(RQ, M * 2 - 1, 2)), M, M + 1)) Mod 12
XingZuo = Mid(XZ, P * 2 - 1, 2) & "座"
End Function

MsgBox XingZuo(12, 25)
yongbo 2004-05-06
  • 打赏
  • 举报
回复
northwolves(野性的呼唤) 比较高,但是同样没有注释的话,我的似乎要好明白一些。
KiteGirl(小仙妹) 用绝对日期算的话,好像没有必要呢。毕竟星座日期是固定的。不过提供了另一种思路。至于“缓冲表”法,我还没有看明白。一定仔细研究。
KiteGirl 2004-05-06
  • 打赏
  • 举报
回复
下面是另一种思路,是我平时写程序经常用的一种“缓冲法”。当然,用于这个程序有点不是地方。

Private pri缓冲表(12, 31) As String
Private pri缓冲表_就绪 As Boolean

Private Sub Command1_Click()
Dim t日期 As Date
Dim t星座名表() As String
Dim t星座 As Integer
t星座名表() = Split("魔羯座,水瓶座,双鱼座,白羊座,金牛座,双子座,巨蟹座,狮子座,处女座,天秤座,天蝎座,射手座", ",")

t日期 = "2004-1-1"
For tIndex = 1 To 366
t星座 = 星座_获得从日期_缓冲表法(t日期)
Text1.Text = Text1.Text & t日期 & ":" & t星座名表(t星座) & vbCrLf
t日期 = t日期 + 1
Next
End Sub

Function 星座_获得从日期_缓冲表法(ByVal p日期 As Date) As Integer
Dim t输出星座 As Integer
Dim t序 As Integer
Dim t日期 As Date
t日期 = "2004-1-1"
If pri缓冲表_就绪 Then
t输出星座 = pri缓冲表(Month(p日期), Day(p日期))
Else
For t序 = 1 To 366
pri缓冲表(Month(t日期), Day(t日期)) = 星座_获得从日期(t日期)
t日期 = t日期 + 1
Next
pri缓冲表_就绪 = True
End If
星座_获得从日期_缓冲表法 = t输出星座
End Function

Function 绝对日_获得从日期(ByVal p日期 As Date, Optional ByVal p偏移量 As Integer) As Integer
Dim t输出日 As Integer

Dim t月日表 As String
Dim t月 As Integer
Dim t月_序 As Integer
Dim t日 As Integer

t月日表 = "313232332323"

t月 = Month(p日期) - 1
t日 = Day(p日期) - 1

For t月_序 = 1 To t月
t输出日 = t输出日 + (Asc(Mid(t月日表, t月_序, 1)) - 20)
Next

t输出日 = (t输出日 + t日 + p偏移量) Mod 366

绝对日_获得从日期 = t输出日
End Function

Function 星座_获得从日期(ByVal p日期 As Date) As Integer
Dim t输出星座 As Integer
Dim t星座日 As Integer
Dim t星座_序 As Integer
Dim t月日表 As String

t月日表 = "123234333321"
t星座日 = 绝对日_获得从日期(p日期, 10)

For t星座_序 = 0 To 11
t星座日 = t星座日 - (Asc(Mid(t月日表, t星座_序 + 1, 1)) - 20)
If t星座日 < 0 Then t输出星座 = t星座_序: Exit For
Next

星座_获得从日期 = t输出星座
End Function

KiteGirl 2004-05-06
  • 打赏
  • 举报
回复
其实我这么写也很麻烦,有时候直接用Select语句更直观。不过,我个人习惯是不喜欢用Select语句和If语句。上述代码的优势仅仅在于:如果星座的日期设置有频繁变化,可以非常容易修改。不过,这种情况似乎不大可能出现。
KiteGirl 2004-05-06
  • 打赏
  • 举报
回复
这是我的代码:
Private Sub Command1_Click()
Dim t日期 As Date
Dim t星座名表() As String
Dim t星座 As Integer
t星座名表() = Split("魔羯座,水瓶座,双鱼座,白羊座,金牛座,双子座,巨蟹座,狮子座,处女座,天秤座,天蝎座,射手座", ",")

t日期 = "2004-1-1"
For tIndex = 1 To 366
t星座 = 星座_获得从日期(t日期)
Text1.Text = Text1.Text & t日期 & ":" & t星座名表(t星座) & vbCrLf
t日期 = t日期 + 1
Next
End Sub

Function 绝对日_获得从日期(ByVal p日期 As Date, Optional ByVal p偏移量 As Integer) As Integer
Dim t输出日 As Integer

Dim t月日表 As String
Dim t月 As Integer
Dim t月_序 As Integer
Dim t日 As Integer

t月日表 = "313232332323"

t月 = Month(p日期) - 1
t日 = Day(p日期) - 1

For t月_序 = 1 To t月
t输出日 = t输出日 + (Asc(Mid(t月日表, t月_序, 1)) - 20)
Next

t输出日 = (t输出日 + t日 + p偏移量) Mod 366

绝对日_获得从日期 = t输出日
End Function

Function 星座_获得从日期(ByVal p日期 As Date) As Integer
Dim t输出星座 As Integer
Dim t星座日 As Integer
Dim t星座_序 As Integer
Dim t月日表 As String

t月日表 = "123234333321"
t星座日 = 绝对日_获得从日期(p日期, 10)

For t星座_序 = 0 To 11
t星座日 = t星座日 - (Asc(Mid(t月日表, t星座_序 + 1, 1)) - 20)
If t星座日 < 0 Then t输出星座 = t星座_序: Exit For
Next

星座_获得从日期 = t输出星座
End Function

northwolves 2004-05-06
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
MsgBox funcWhatConstellation(12, 25)
End Sub
Function funcWhatConstellation(ByVal iMonthNum As Integer, ByVal iDayNum As Integer) As String
Dim temp As Integer
temp = iMonthNum + IIf(iDayNum <= Choose(iMonthNum, 19, 18, 20, 19, 20, 21, 22, 22, 22, 23, 22, 21), 0, 1)
If temp = 13 Then temp = 1
funcWhatConstellation = Choose(temp, "魔羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座")
End Function
northwolves 2004-05-06
  • 打赏
  • 举报
回复
Function funcWhatConstellation(ByVal iMonthNum As Integer, ByVal iDayNum As Integer) As Integer
funcWhatConstellation = iMonthNum + IIf(iDayNum <= Choose(iMonthNum, 19, 18, 20, 19, 20, 21, 22, 22, 22, 23, 22, 21), 0, 1)
If funcWhatConstellation = 13 Then funcWhatConstellation = 1
End Function
yongbo 2004-05-06
  • 打赏
  • 举报
回复
KiteGirl 2004-05-06
  • 打赏
  • 举报
回复
这么写有点麻烦,一会我给你一个……
yongbo 2004-05-06
  • 打赏
  • 举报
回复
请批评一下这段代码!

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧