星座类 (UserLib.World.Time.Constellation)

原创 2006年06月19日 13:25:00

以下是星座类,用于判断你属于哪个星座,也可以获取星座对应当日期范围

 

其中涉及到的类Segment将会在文章末尾解释

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace UserLib.World.Time

{

    /// <summary>

    /// 星座

    /// </summary>

    public class Constellation

     {

        private static Segment[] data=new Segment[12];         //保存星座数据

 

        /// <summary>

        /// 日期索引函数

        /// </summary>

        /// <param name="dt">日期</param>

        /// <returns>对应的索引</returns>

        private static int Index(DateTime dt)

        {

            //将年份统一为

            //因为年是闰年,当输入的日期为月日时,程序不会出错

            DateTime rdt = new DateTime(2000, dt.Month, dt.Day);

            for(int i=0;i<12;i++)

            {

                if (rdt>=data[i].StartDateTime && rdt<=data[i].EndDateTime)

                {

                    return i;

                }

            }

            return -1;

        }

 

        /// <summary>

        /// 中文名称索引函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <returns>对应的索引</returns>

        private static int IndexCN(string cnName)

        {

            for (int i = 0; i < 12; i++)

            {

                if (data[i].CNName == cnName)

                {

                    return i;

                }

            }

            return -1;

        }

 

        /// <summary>

        /// 英文名称索引函数

        /// </summary>

        /// <param name="enName">英文名称</param>

        /// <returns>对应的索引</returns>

        private static int IndexEN(string enName)

        {

            for (int i = 0; i < 12; i++)

            {

                if (data[i].ENName.ToLower() == enName.ToLower())

                {

                    return i;

                }

            }

            return -1;

        }

 

        /// <summary>

        /// 构造函数

        /// </summary>

        static Constellation()

        {

            data[0] = new Segment("水瓶座", "Aquarius", new DateTime(2000, 1, 21), new DateTime(2000, 2, 18));

            data[1] = new Segment("双鱼座", "Pisces", new DateTime(2000, 2, 19), new DateTime(2000, 3, 20));

            data[2] = new Segment("白羊座", "Aries", new DateTime(2000, 3, 21), new DateTime(2000, 4, 20));

            data[3] = new Segment("金牛座", "Taurus", new DateTime(2000, 4, 21), new DateTime(2000, 5, 21));

            data[4] = new Segment("双子座", "Gemini", new DateTime(2000, 5, 22), new DateTime(2000, 6, 21));

            data[5] = new Segment("巨蟹座", "Cancer", new DateTime(2000, 6, 22), new DateTime(2000, 7, 22));

            data[6] = new Segment("狮子座", "Leo", new DateTime(2000, 7, 23), new DateTime(2000, 8, 23));

            data[7] = new Segment("处女座", "Virgo", new DateTime(2000, 8, 24), new DateTime(2000, 9, 22));

            data[8] = new Segment("天秤座", "Libra", new DateTime(2000, 9, 23), new DateTime(2000, 10, 23));

            data[9] = new Segment("天蝎座", "Scorpio", new DateTime(2000, 10, 24), new DateTime(2000, 11, 22));

            data[10] = new Segment("射手座", "Sagittarius", new DateTime(2000, 11, 23), new DateTime(2000, 12, 21));

            data[11] = new Segment("摩羯座", "Capricorn", new DateTime(2000, 12, 22), new DateTime(2000, 1, 20));

        }

 

        /// <summary>

        /// 获取星座的中文名称

        /// </summary>

        /// <param name="dt">日期</param>

        /// <returns>星座的中文名称</returns>

        public static string GetCNName(DateTime dt)

        {

            int i = Index(dt);

            return data[i].CNName;

        }

 

        /// <summary>

        /// 获取星座的英文名称

        /// </summary>

        /// <param name="dt">日期</param>

        /// <returns>星座的英文名称</returns>

        public static string GetENName(DateTime dt)

        {

            int i = Index(dt);

            return data[i].ENName;

        }

 

        /// <summary>

        /// 获取星座的起始日期

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <returns>星座的起始日期</returns>

        public static DateTime GetStartDateByCNName(string cnName)

        {

            int i = IndexCN(cnName);

            if (i != -1)

            {

                return data[i].StartDateTime;

            }

            else

            {

                throw new IndexOutOfRangeException();

            }

        }

 

        /// <summary>

        /// 获取星座的终止日期

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <returns>星座的终止日期</returns>

        public static DateTime GetEndDateByCNName(string cnName)

        {

            int i = IndexCN(cnName);

            if (i != -1)

            {

                return data[i].EndDateTime;

            }

            else

            {

                throw new IndexOutOfRangeException();

            }

        }

 

        /// <summary>

        /// 获取星座的起始日期

        /// </summary>

        /// <param name="enName">英文名称</param>

        /// <returns>星座的起始日期</returns>

        public static DateTime GetStartDateByENName(string enName)

        {

            int i = IndexEN(enName);

            if (i != -1)

            {

                return data[i].StartDateTime;

            }

            else

            {

                throw new IndexOutOfRangeException();

            }

        }

 

        /// <summary>

        /// 获取星座的终止日期

        /// </summary>

        /// <param name="enName">英文名称</param>

        /// <returns>星座的终止日期</returns>

        public static DateTime GetEndDateByENName(string enName)

        {

            int i = IndexEN(enName);

            if (i != -1)

            {

                return data[i].EndDateTime;

            }

            else

            {

                throw new IndexOutOfRangeException();

            }

        }

 

        /// <summary>

        /// 获取星座的起始日期

        /// </summary>

        /// <param name="dt">日期</param>

        /// <returns>星座的起始日期</returns>

        public static DateTime GetStartDateByDate(DateTime dt)

        {

            int i = Index(dt);

            return data[i].StartDateTime;

        }

 

        /// <summary>

        /// 获取星座的终止日期

        /// </summary>

        /// <param name="dt">日期</param>

        /// <returns>星座的终止日期</returns>

        public static DateTime GetEndDateByDate(DateTime dt)

        {

            int i = Index(dt);

            return data[i].EndDateTime;

        }

           

     }

}

 

 

下面解释上文中提到的Segment

 

此类是用于保存每个时间对应当意义或节日名称,在星座类中用于保存各个星座的时间段。

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace UserLib.World.Time

{

    /// <summary>

    /// 用于存储一个时间断对应的意义

    /// </summary>

    public class Segment

    {

        private string cnName;          //中文名称

        private string cnShortName;     //中文名称缩写

        private string enName;          //英文名称

        private string enShortName;     //英文名称缩写

        private DateTime start;         //起始时间

        private DateTime end;           //终止时间

 

        /// <summary>

        /// 赋值函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <param name="cnShortName">中文名称缩写</param>

        /// <param name="enName">英文名称</param>

        /// <param name="enShortName">英文名称缩写</param>

        /// <param name="startDateTime">起始日期</param>

        /// <param name="endDateTime">终止日期</param>

        private void Evaluate(string cnName, string cnShortName, string enName, string enShortName, DateTime startDateTime, DateTime endDateTime)

        {

            this.cnName = cnName;

            this.cnShortName = cnShortName;

            this.enName = enName;

            this.enShortName = enShortName;

            start = startDateTime;

            end = endDateTime;

        }

 

        #region 构造函数

 

        /// <summary>

        /// 构造函数

        /// </summary>

        public Segment() { }

 

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <param name="dateTime">时间</param>

        public Segment(string cnName, DateTime dateTime)

        {

            Evaluate(cnName, null, null, null, dateTime, dateTime);

        }

 

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <param name="enName">英文名称</param>

        /// <param name="dateTime">时间</param>

        public Segment(string cnName, string enName, DateTime dateTime)

        {

            Evaluate(cnName, null, enName, null, dateTime, dateTime);

        }  

 

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <param name="cnShortName">中文名称缩写</param>

        /// <param name="enName">英文名称</param>

        /// <param name="enShortName">英文名称缩写</param>

        /// <param name="dateTime">时间</param>

        public Segment(string cnName, string cnShortName, string enName, string enShortName, DateTime dateTime)

        {

            Evaluate(cnName, cnShortName, enName, enShortName, dateTime, dateTime);

        }

 

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <param name="startDateTime">起始日期</param>

        /// <param name="endDateTime">终止日期</param>

        public Segment(string cnName, DateTime startDateTime, DateTime endDateTime)

        {

            Evaluate(cnName, null, null, null, startDateTime, endDateTime);

        }

 

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <param name="enName">英文名称</param>

        /// <param name="startDateTime">起始日期</param>

        /// <param name="endDateTime">终止日期</param>

        public Segment(string cnName, string enName, DateTime startDateTime, DateTime endDateTime)

        {

            Evaluate(cnName, null, enName, null, startDateTime, endDateTime);

        }

 

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="cnName">中文名称</param>

        /// <param name="cnShortName">中文名称缩写</param>

        /// <param name="enName">英文名称</param>

        /// <param name="enShortName">英文名称缩写</param>

        /// <param name="startDateTime">起始日期</param>

        /// <param name="endDateTime">终止日期</param>

        public Segment(string cnName, string cnShortName, string enName, string enShortName, DateTime startDateTime, DateTime endDateTime)

        {

            Evaluate(cnShortName, cnShortName, enName, enShortName, startDateTime, endDateTime);

        }

 

        #endregion

 

        #region 属性

        /// <summary>

        /// 获取或设置中文名称

        /// </summary>

        public string CNName

        {

            get

            {

                return cnName;

            }

            set

            {

                cnName = value;

            }

        }

 

        /// <summary>

        /// 获取或设置中文名称缩写

        /// </summary>

        public string CNShortName

        {

            get

            {

                return cnShortName;

            }

            set

            {

                cnShortName = value;

            }

        }

 

        /// <summary>

        /// 获取或设置英文名称

        /// </summary>

        public string ENName

        {

            get

            {

                return enName;

            }

            set

            {

                enName = value;

            }

        }

 

        /// <summary>

        /// 获取或设置英文名称缩写

        /// </summary>

        public string ENShortName

        {

            get

            {

                return enShortName;

            }

            set

            {

                enShortName = value;

            }

        }

 

        /// <summary>

        /// 获取或设置起始时间

        /// </summary>

        public DateTime StartDateTime

        {

            get

            {

                return start;

            }

            set

            {

                start = value;

            }

        }

 

        /// <summary>

        /// 获取或设置终止时间

        /// </summary>

        public DateTime EndDateTime

        {

            get

            {

                return end;

            }

            set

            {

                end = value;

            }

        }

 

        #endregion

 

    }

}

 

 

相关文章推荐

获取生肖、星座、年龄的工具类ConstellationHepler

获取生肖、星座、年龄的工具类ConstellationHeplerimport java.util.Calendar;public class ConstellationHepler { pri...

根据日期,转换成星座、生肖工具类

package com.ss.common.util; import java.text.ParseException; import java.text.SimpleDateFormat; imp...

CodeForces 618C Constellation

传送门:http://codeforces.com/problemset/problem/618/C 已知若干的点的坐标,并且没有两个点坐标相同,所有点不会均在一条线上,求三个顶点构成的...
  • LsFlyt
  • LsFlyt
  • 2016年03月18日 15:58
  • 232

constellation

  • 2013年02月18日 23:13
  • 219KB
  • 下载

Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)--C. Constellation

C. Constellation time limit per test 2 seconds memory limit per test 256 megabytes ...

codeforces 618C. Constellation

原题 排序选出最左下角的两个点,逐一和其他计算是否能组成三角形。 #define _CRT_SECURE_NO_WARNINGS #include #include #incl...

Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)-C. Constellation(贪心+向量平行判断)

C. Constellation time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) C. Constellation

昨晚的codeforces真的是惨,第三题WA了8次,然而并没有过。今天中午起来recode一下,AC掉之后,厚颜无耻的来写题解。献上这道平面几何,以此安慰昨夜脸上熬出的痘痘。传送门:http://c...

【CodeForces 618C】Constellation(几何水题)

DescriptionCat Noku has obtained a map of the night sky. On this map, he found a constellation with ...

Wunder Fund Round 2016 C. Constellation(贪心+计算几何)

题意: 给定N≤105个点,保证所有点不全部共线,找出一个三角形使得三角形内部没有其他点给定N\le 10^5个点, 保证所有点不全部共线, 找出一个三角形使得三角形内部没有其他点 分析: 随...
  • lwt36
  • lwt36
  • 2016年01月30日 05:24
  • 347
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:星座类 (UserLib.World.Time.Constellation)
举报原因:
原因补充:

(最多只允许输入30个字)