liartie

Untitled

Sep 11th, 2022
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 KB | None | 0 0
  1. using System.Text.RegularExpressions;
  2.  
  3. namespace Sort
  4. {
  5.     public class Sort
  6.     {
  7.         public static bool CheckCommand(string command)
  8.         {
  9.             switch (command)
  10.             {
  11.                 case "sort":
  12.                     return true;
  13.                 case "Sort":
  14.                     Console.WriteLine("I don't know this command, sorry to say that. Is there a chance you meant 'sort'?");
  15.                     return false;
  16.                 default:
  17.                     Console.WriteLine("I don't know this command, sorry to say that. And I have no idea what you meant...");
  18.                     return false;
  19.             }
  20.         }
  21.  
  22.         public static List<String> ReadFile(string name)
  23.         {
  24.             List<String> lines = new List<String>();
  25.             try
  26.             {
  27.                 foreach (string line in File.ReadLines(name))
  28.                 {
  29.                     lines.Add(line);
  30.                 }
  31.             }
  32.             catch (FileNotFoundException)
  33.             {
  34.                 Console.WriteLine("I am so sorry to say, but I cannot find such file (" + name + "). I know this might be very difficult," +
  35.                                   " but please check if you have entered the name correctly. I believe in you.");
  36.             }
  37.  
  38.             return lines;
  39.         }
  40.  
  41.         public static async Task Print(bool toFile, List<String> linesArray, string fileName)
  42.         {
  43.             if (toFile)
  44.             {
  45.                 await File.WriteAllLinesAsync(fileName, linesArray);
  46.             }
  47.             else
  48.             {
  49.                 foreach (var line in linesArray)
  50.                 {
  51.                     Console.WriteLine(line);
  52.                 }
  53.             }
  54.         }
  55.  
  56.         public static List<String> SortLines(List<String> lines, bool reverse, bool unique, bool numericSort)
  57.         {
  58.             if (numericSort)
  59.             {
  60.                 List<String> nums = new List<string>();
  61.                 List<String> other = new List<string>();
  62.                 foreach (var line in lines)
  63.                 {
  64.                     if (new Regex("^[0-9]*$").IsMatch(line)) nums.Add(line);
  65.                     else other.Add(line);
  66.                 }
  67.                 nums.Sort();
  68.                 if (reverse) nums.Reverse();
  69.                 nums.AddRange(other);
  70.                 if (unique) nums = nums.Distinct().ToList();
  71.                 return nums;
  72.             }
  73.             lines.Sort();
  74.             if (reverse) lines.Reverse();
  75.             if (unique) lines = lines.Distinct().ToList();
  76.            
  77.             return lines;
  78.         }
  79.  
  80.         public static void Main(string[] args)
  81.         {
  82.             var command = Console.ReadLine();
  83.             String[] flags = command!.Split(" ");
  84.             if(!CheckCommand(flags[0])) return;
  85.  
  86.             var fileName = flags[^1];
  87.             var linesArray = ReadFile(fileName);
  88.  
  89.             bool ignoreCase = false,
  90.                 reverse = false,
  91.                 unique = false,
  92.                 numericSort = false,
  93.                 versionSort = false,
  94.                 fileOutput = false;
  95.             string outputFileName = "output.txt";
  96.             for (int i = 1; i < flags.Length - 1; i++)
  97.             {
  98.                 var flag = flags[i];
  99.                 if (flag == "-f" || flag == "--ignore-case") ignoreCase = true;
  100.                 else if (flag == "-r" || flag == "--reverse") reverse = true;
  101.                 else if (flag == "-u" || flag == "--unique") unique = true;
  102.                 else if (flag == "-n" || flag == "--numeric-sort") numericSort = true;
  103.                 else if (flag == "-o" || Regex.Match(flag , @"--output=.*").Success)
  104.                 {
  105.                     fileOutput = true;
  106.                     if (flag == "-o")
  107.                     {
  108.  
  109.                             outputFileName = flags[i + 1];
  110.                             i++;
  111.                             if (i == flags.Length - 1)
  112.                             {
  113.                                 Console.WriteLine("Hmmm... Seems like you didn't specify the output file name.");
  114.                                 return;
  115.                             }
  116.  
  117.                             Console.WriteLine("Hmmm... Seems like you didn't specify the output file name.");
  118.                             return;
  119.                     }
  120.                     else
  121.                     {
  122.                         outputFileName = flag.Substring(9);
  123.                         if(outputFileName == "")
  124.                         {
  125.                             Console.Write("Hmmm... Seems like you didn't specify the file name.");
  126.                             return;
  127.                         }
  128.                     }
  129.                 }
  130.                 else
  131.                 {
  132.                     Console.WriteLine("Unknown flag: " + flag + ". Maybe try reading the documentation? (just an advise)");
  133.                     return;
  134.                 }
  135.             }
  136.  
  137.             linesArray = SortLines(linesArray, reverse, unique, numericSort);
  138.             Print(fileOutput, linesArray, outputFileName);
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment