2.5.29

上次更新:2019-04-17
发现了题解错误/代码缺陷/排版问题?请点这里:如何:提交反馈

解答

首先定义一系列比较器,分别根据文件大小、文件名和最后修改日期比较。
然后修改 Less 的实现,接受一个比较器数组,使用数组中的比较器依次比较,直到比较结果为两者不相同。
最后使用插入排序作为稳定排序,传入比较器数组用于 Less 函数。

代码

using System;
using System.IO;
using System.Collections.Generic;

namespace _2._5._29
{
    class Program
    {
        class FileSizeComparer : Comparer<FileInfo>
        {
            public override int Compare(FileInfo x, FileInfo y)
            {
                return x.Length.CompareTo(y.Length);
            }
        }

        class FileNameComparer : Comparer<FileInfo>
        {
            public override int Compare(FileInfo x, FileInfo y)
            {
                return x.FullName.CompareTo(y.FullName);
            }
        }

        class FileTimeStampComparer : Comparer<FileInfo>
        {
            public override int Compare(FileInfo x, FileInfo y)
            {
                return x.LastWriteTime.CompareTo(y.LastWriteTime);
            }
        }

        static void InsertionSort<T>(T[] keys, Comparer<T>[] comparers)
        {
            for (int i = 0; i < keys.Length; i++)
                for (int j = i; j > 0 && Less(keys, j, j - 1, comparers); j--)
                {
                    T temp = keys[j];
                    keys[j] = keys[j - 1];
                    keys[j - 1] = temp;
                }
        }

        static bool Less<T>(T[] keys, int x, int y, Comparer<T>[] comparables)
        {
            int cmp = 0;
            for (int i = 0; i < comparables.Length && cmp == 0; i++)
            {
                cmp = comparables[i].Compare(keys[x], keys[y]);
            }
            return cmp < 0;
        }

        static void Main(string[] args)
        {
            string[] arguments = Console.ReadLine().Split(' ');
            string directoryPath = arguments[0];
            string[] filenames = Directory.GetFiles(directoryPath);
            FileInfo[] fileInfos = new FileInfo[filenames.Length];
            for (int i = 0; i < filenames.Length; i++)
                fileInfos[i] = new FileInfo(filenames[i]);

            List<Comparer<FileInfo>> comparers = new List<Comparer<FileInfo>>();
            for (int i = 1; i < arguments.Length; i++)
            {
                string command = arguments[i];
                switch (command)
                {
                    case "-t":
                        comparers.Add(new FileTimeStampComparer());
                        break;
                    case "-s":
                        comparers.Add(new FileSizeComparer());
                        break;
                    case "-n":
                        comparers.Add(new FileNameComparer());
                        break;
                }
            }
            InsertionSort(fileInfos, comparers.ToArray());
            for (int i = 0; i < fileInfos.Length; i++)
            {
                Console.WriteLine(fileInfos[i].Name + "\t" + fileInfos[i].Length + "\t" + fileInfos[i].LastWriteTime);
            }
        }
    }
}
上一题 下一题