一个更好的办法,以填补日期和星座二维数组 [英] A better way to fill a two-dimensional array with date and zodiac sign

查看:119
本文介绍了一个更好的办法,以填补日期和星座二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以下问题:

欲填充的二维[365,2]数组。第一个值是应该保存日期:首先是1月1日和十二月31日结束。第二个值应该是对应的十二生肖坚持每个日期:

I want to fill a two-dimensional [365,2] Array. The first value is supposed to hold the date: starting with January 1st and ending with December the 31st. The second value is supposed to hold the corresponding Zodiac Sign to each date:

例如。 数组[0,0] 持有101和数组[0,1] 持有白羊等。

e.g. array[0, 0] holds 101 and array[0, 1] holds Aries and so on.

我写了一个功能:

public static void fill_array(string[,] year_zodiac, int days, string zodiac, string startdate, int starting_day) 
{
    //function to fill array with date and zodiac
    int startdate_int = 0;
    for (int i = starting_day; i <= days; i++)
    {
        year_zodiac[i, 0] = startdate;
        year_zodiac[i, 1] = zodiac;

        startdate_int = Int32.Parse(startdate);
        startdate_int++;
        startdate = startdate_int.ToString();
    }

和调用它是这样的:

fill_array(main_array, 18, "Aries", "101", 0);

这对每个星座做。我通过简单地调用规避月份破题 fill_array 的两倍(即我曾经把它叫做白羊座在12月的一部分,一旦白羊座1月份部分)。

This has to be done for every Zodiac sign. I circumvented the month break problem by simply calling fill_array twice (i.e. I call it once for the part of Aries in December and once for the part of aries in January).

这个解决方案的工作原理,但它似乎令人难以置信的原油给我。

This Solution works, but it seems incredibly crude to me.

任何人都可以给我一些指点朝着更优雅的解决方案?

Can anybody give me some pointers towards a more elegant solution?

推荐答案

下面是一个类,你想要做什么,它已经过测试。我没有填写在第一个例子中的所有迹象,但是当我重新分解它我做到了。我建议你​​测试这口井,因为我只测试少数情况下,可能已经错过了一些边缘情况。

Here is a class that does what you want, it has been tested. I did not fill out all signs on the first example, but when I re-factored it I did. I suggest you test this well as I only tested a few cases and might have missed some edge cases.

正如已经向我指出由@ClickRick,我的确开始了他的阵列/枚举的设计,却发现使用LINQ并移动到列表时缺乏。我还必须解决他的数据数组所以它会编译。我给信誉为到期。

As has been pointed out to me by @ClickRick, I did start with his array/enum design, but found that lacking when using Linq and moved to a List. I also had to fix his data array so it would compile. I'm giving credit as is due.

public class Signs
{
   static List<string> SignList = new List<string>() {  "Aquarius", "Pisces", "Aries", "Taurus", "Not Found"};

   static DateTime[] startDates =  {
        new DateTime(DateTime.Now.Year,1,21),
        new DateTime(DateTime.Now.Year,2,20),
        new DateTime(DateTime.Now.Year,3,21),
        new DateTime(DateTime.Now.Year,4,21),
        new DateTime(DateTime.Now.Year,12,31)  };

    static public string Sign(DateTime inDate)
    {
       return SignList.Zip(startDates, (s, d) => new { sign = s, date = d })
                      .Where(x => (inDate.Month*100)+inDate.Day <= (x.date.Month*100)+x.date.Day)
                      .Select(x => x.sign)
                      .First();
    }
}

重新分解(这是更清晰上面第一个例子)

re-factored (this is clearer with the example above first)

public class Signs
{
  static List<string> SignList = new List<string>() {
    "Capricorn", "Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Not Found" };

  static List<int> startDates = new List<int>() {
    // month * 100 + day of month
     120, 219, 320, 420, 521, 621, 722, 821, 923, 1023, 1122, 1222, 1232, 9999 // edge marker
  };

  static public string Sign(DateTime inDate)
  {
    return SignList[startDates.TakeWhile(d => (inDate.Month*100)+inDate.Day > d).Count()];
  }
}

这篇关于一个更好的办法,以填补日期和星座二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆