星座类(UserLib.World.Time.Constellation)
作者: 不详
编辑: 海韵 发布日期: 2006-11-16 00:00:00 点击量: 6643
来源: haiyun.net
以下是星座类,用于判断你属于哪个星座,也可以获取星座对应当日期范围
其中涉及到的类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
}
}