将月份转换为在Quicksort中使用的等效数字

时间:2016-07-22 22:12:14

标签: c# quicksort monthcalendar

我有几个月从外部文本文件读入一个数组,我需要将月份转换为一个数组,该数组保存相当于月份的值,例如1月= 1月,2月= 2等等,这样他们就可以通过Quicksort。

 public static void Main(string[] args)
    {
        //Read external files into arrays.
        string[] Month = File.ReadLines(@"Month.txt").ToArray();
        string[] Year = File.ReadLines(@"Year.txt").ToArray();

        //Convert arrays from string to double to be used in sort.
        double[] YearSort = Array.ConvertAll(Year, double.Parse);

        int UserInput1;

        //Create new array that will hold selected array to be used in sort. 
        double[] data = new double[1022];
        //Prompt user to select action. 
    Console.WriteLine("Press 1 to Sort by month or 2 to sort by year.");
     UserInput1 = Convert.ToInt32(Console.ReadLine());

    if(UserInput1 == 1)
    {
        Array.Copy(Month,data,1022);
        QuickSort(data); 
         for (int i = 0; i < 1022; i++)
  Console.WriteLine(data[i]);
  Console.WriteLine();
    }
    else if (UserInput1 == 2)
    {
        Array.Copy(YearSort,data,1022);
        QuickSort(data); 
         for (int i = 0; i < 1022; i++)
  Console.WriteLine(data[i]);
  Console.WriteLine();
    }
    else
    {
        Console.WriteLine("Please try again and select a valid option");
    }
    }

static int MonthToDouble( string Month )
        {
            int NewMonth = 0;

            switch(Month)
            {
                case "January":
                case "january":
                    NewMonth = 1;
                    break;
                case "February":
                case "february":
                    NewMonth = 2;
                    break;
                case "March":
                case "march":
                    NewMonth = 3;
                    break;
                case "April":
                case "april":
                    NewMonth = 4;
                    break;
                case "May":
                case "may":
                    NewMonth = 5;
                    break;
                case "June":
                case "june":
                    NewMonth = 6;
                    break;
                case "July":
                case "july":
                    NewMonth = 7;
                    break;
                case "August":
                case "august":
                    NewMonth = 8;
                    break;
                case "September":
                case "september":
                    NewMonth = 9;
                    break;
                case "October":
                case "october":
                    NewMonth = 10;
                    break;
                case "November":
                case "november":
                    NewMonth = 11;
                    break;
                case "December":
                case "december":
                    NewMonth = 12;
                    break;
            }

            return NewMonth;
        }

        static string DoubleToMonth(double Month)
        {
            string NewMonth = "";

            switch ((int)Month) 
            {
                case 1:
                    NewMonth = "January";
                    break;
                case 2:
                    NewMonth = "February";
                    break;
                case 3:
                    NewMonth = "March";
                    break;
                case 4:
                    NewMonth = "April";
                    break;
                case 5:
                    NewMonth = "May";
                    break;
                case 6:
                    NewMonth = "June";
                    break;
                case 7:
                    NewMonth = "July";
                    break;
                case 8:
                    NewMonth = "August";
                    break;
                case 9:
                    NewMonth = "September";
                    break;
                case 10:
                    NewMonth = "October";
                    break;
                case 11:
                    NewMonth = "November";
                    break;
                case 12:
                    NewMonth = "December";
                    break;
            }

            return NewMonth;
        }

 //QuickSort for double data values are in ascending order. 
 public static void QuickSort(double[] data)
 {
 Quick_Sort(data, 0, data.Length - 1);
 }
public static void Quick_Sort(double[] data, int left, int right)
 {
 int i, j;
 double pivot, temp;
 i = left;
 j = right;
 pivot = data[(left + right) / 2];
 do
 {
 while ((data[i] < pivot) && (i < right)) i++;
 while ((pivot < data[j]) && (j > left)) j--;
 if (i <= j)
 {
 temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
 }
 } while (i <= j);
 if (left < j) Quick_Sort(data, left, j);
 if (i < right) Quick_Sort(data, i, right);
 }          
}

2 个答案:

答案 0 :(得分:1)

DateTime对象的Month属性会根据您的需要为您提供从1开始的月份的整数值。因此,您可以使用DateTime.ParseExact()将字符串解析为完整的DateTime对象,然后获取Month属性:

int monthNumber = DateTime.ParseExact("January", "MMMM", CultureInfo.CurrentCulture).Month;

您只需将"January"替换为月份字符串,然后将"MMMM" Custom Format String保留为&#34;月份的全名&#34;。

以上所有代码都会简化您的MonthToDouble()方法,因为某些原因您甚至不使用该方法(也应该返回double,而不是int)。与你的头衔相反,你已经有了一种方法来将月份转换为等价数字#34;你只是没有使用它。

所以,我认为你唯一缺少的就是替换它:

    Array.Copy(Month,data,1022);
    QuickSort(data);

有了这个:

        double[] monthsAsDoubles = new double[Month.Length];
        for (int i = 0; i < monthsAsDoubles.Length; i++)
        {
            monthsAsDoubles[i] = MonthToDouble(Month[i]);
        }
        QuickSort(monthsAsDoubles);

同时将MonthToDouble()的返回值从int更改为double(如果需要,可以投射)。

答案 1 :(得分:0)

编辑:第二个想法,Quantic的答案更简单。我只是把它留在这里作为替代。

使用DateTimeFormatInfo.MonthNames属性以及不区分大小写的字符串比较可以大大简化您的代码。这也有一个好处,就是更容易移植以使用月份名称的不同语言。

这是一个片段:

using System;
using System.Globalization;
using System.Linq;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {

        var InputMonths = new List<string> { "January","march","sepTEmber","smarch" };

        var MonthNames = new DateTimeFormatInfo().MonthNames.ToList();

        var InputMonthNumbers = new List<int>();

        foreach (var m in InputMonths)
        {
            //Find index of the month name, ignoring case
            //Note if the input month name is invalid, FindIndex will return 0
            int month_num = 1 + MonthNames.FindIndex(name => name.Equals(m, StringComparison.OrdinalIgnoreCase));

            if (month_num > 0)
            {
                InputMonthNumbers.Add(month_num);
            }
        }

        foreach (var n in InputMonthNumbers)
        {
            Console.WriteLine(n.ToString());
        }

    }
}

输出:

1
3
9