Advertisement
Death420

Length_Conversion.cs

Jan 17th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1.     class Program
  2.     {
  3.         static Dictionary<string,double> conv = new Dictionary<string, double>() { { "mm", 1000 }, { "cm", 100 }, { "miles", 0.000621371192 }, { "inches", 39.3700767 }, { "feet", 3.2808399 }, { "yards", 1.0936133 } };
  4.         static string ConvertLength(string message)
  5.         {
  6.             var arr = message.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  7.             if (arr.Length < 3)
  8.                 return "Not enough arguments.";
  9.            
  10.             var notFound = string.Join("\" and \"", arr.Skip(1).Where(w => !conv.ContainsKey(w)));
  11.  
  12.             if (!double.TryParse(arr[0], out double val))
  13.                 return $"The first argument \"{arr[0]}\" is not a number.";
  14.             else if (notFound.Length != 0)
  15.                 return $"I don't understand \"{notFound}\".";
  16.             else
  17.                 return $"{val} {arr[1]} is {((val / conv[arr[1]]) * conv[arr[2]]).ToString("N0")} {arr[2]}";
  18.         }
  19.         static void Main(string[] args)
  20.         {            
  21.             Console.WriteLine("The options are:\n[{0}].", string.Join(", ", (conv.Select(kvp => kvp.Key))));
  22.            
  23.             while (true)
  24.             {
  25.                 Console.WriteLine("\nNext input: ");
  26.                 var input = Console.ReadLine().Trim();
  27.                 if (input.ToLower() == "exit") break;
  28.                 Console.WriteLine(ConvertLength(input));
  29.             }            
  30.         }
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement