CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

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

楼主yongbo(山高水低风格上)2004-05-02 23:11:43 在 VB / 基础类 提问

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 问题点数:20、回复次数:11Top

1 楼yongbo(山高水低风格上)回复于 2004-05-06 17:19:58 得分 0

请批评一下这段代码!Top

2 楼KiteGirl(小仙妹)回复于 2004-05-06 17:26:24 得分 0

这么写有点麻烦,一会我给你一个……Top

3 楼yongbo(山高水低风格上)回复于 2004-05-06 17:54:04 得分 0

好Top

4 楼northwolves(狼行天下)回复于 2004-05-06 18:32:00 得分 10

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  
  Top

5 楼northwolves(狼行天下)回复于 2004-05-06 18:34:37 得分 0

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   FunctionTop

6 楼KiteGirl(小仙妹)回复于 2004-05-06 18:42:26 得分 5

这是我的代码:  
  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  
   
  Top

7 楼KiteGirl(小仙妹)回复于 2004-05-06 18:49:22 得分 0

其实我这么写也很麻烦,有时候直接用Select语句更直观。不过,我个人习惯是不喜欢用Select语句和If语句。上述代码的优势仅仅在于:如果星座的日期设置有频繁变化,可以非常容易修改。不过,这种情况似乎不大可能出现。Top

8 楼KiteGirl(小仙妹)回复于 2004-05-06 19:01:47 得分 0

下面是另一种思路,是我平时写程序经常用的一种“缓冲法”。当然,用于这个程序有点不是地方。  
   
  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  
   
  Top

9 楼yongbo(山高水低风格上)回复于 2004-05-06 19:29:24 得分 0

northwolves(野性的呼唤)   比较高,但是同样没有注释的话,我的似乎要好明白一些。  
  KiteGirl(小仙妹)   用绝对日期算的话,好像没有必要呢。毕竟星座日期是固定的。不过提供了另一种思路。至于“缓冲表”法,我还没有看明白。一定仔细研究。Top

10 楼BlueBeer(1win)回复于 2004-05-08 00:25:08 得分 5

根据   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)Top

11 楼BlueBeer(1win)回复于 2004-05-08 00:28:30 得分 0

个人认为,northwolves(野性的呼唤)的算法比较棒,眼毒啊Top

相关问题

  • 求计算日期的源代码(简单简单,给分多多)
  • 在cs代码中 怎样根据出生日期计算出他的年龄? 出生日期是DateTime的.???????????????????????????????????????????????????????????????
  • 日期计算
  • 日期计算。。。
  • 日期计算
  • 哪位大虾能给段代码计算一个日期在这一年中的第几周
  • 要计算两个日期之间相隔的天数,应该怎样写代码?
  • About日期计算
  • 日期的计算
  • 给我一计算器的源代码

关键词

  • 代码
  • idaynum
  • imonthnum
  • 星座
  • 日期
  • as integer
  • case

得分解答快速导航

  • 帖主:yongbo
  • northwolves
  • KiteGirl
  • BlueBeer

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo