Advertisement
yahorrr

Untitled

Sep 22nd, 2022
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection.Metadata.Ecma335;
  4. using System.Text;
  5. using System.Xml;
  6.  
  7. namespace TransformerToWords
  8. {
  9.     /// <summary>
  10.     /// Implements transformer class.
  11.     /// </summary>
  12.     public class Transformer
  13.     {
  14.         /// <summary>
  15.         /// Transforms each element of source array into its 'word format'.
  16.         /// </summary>
  17.         /// <param name="source">Source array.</param>
  18.         /// <returns>Array of 'word format' of elements of source array.</returns>
  19.         /// <exception cref="ArgumentNullException">Thrown when array is null.</exception>
  20.         /// <exception cref="ArgumentException">Thrown when array is empty.</exception>
  21.         /// <example>
  22.         /// new[] { 2.345, -0.0d, 0.0d, 0.1d } => { "Two point three four five", "Minus zero", "Zero", "Zero point one" }.
  23.         /// </example>
  24.         public string[] Transform(double[]? source)
  25.         {
  26.             string[] result = new string[source.Length];
  27.  
  28.             for (int i = 0; i < source.Length; i++)
  29.             {
  30.                 result[i] = NumberToWord(source[i]);
  31.             }
  32.  
  33.             return result;
  34.         }
  35.  
  36.         private static string NumberToWord(double number)
  37.         {
  38.             switch (number)
  39.             {
  40.                 case double.NaN:
  41.                     return "Not a Number";
  42.                 case double.Epsilon:
  43.                     return "Double Epsilon";
  44.                 case double.NegativeInfinity:
  45.                     return "Negative Infinity";
  46.                 case double.PositiveInfinity:
  47.                     return "Positive Infinity";
  48.             }
  49.  
  50.             string stringNumber = number.ToString();
  51.             StringBuilder result = new StringBuilder();
  52.  
  53.             for (int i = 0; i < stringNumber.Length; i++)
  54.             {
  55.                 result.Append(CharToWord(stringNumber[i]));
  56.             }
  57.  
  58.             return result.Remove(0, 1).ToString().Substring(0, 1).ToUpper() + result.ToString().Substring(1);
  59.         }
  60.  
  61.         private static string CharToWord(char digit) =>
  62.             digit switch
  63.             {
  64.                 '-' => " minus",
  65.                 '+' => " plus",
  66.                 ',' => " point",
  67.                 'E' => " E",
  68.                 '0' => " zero",
  69.                 '1' => " one",
  70.                 '2' => " two",
  71.                 '3' => " three",
  72.                 '4' => " four",
  73.                 '5' => " five",
  74.                 '6' => " six",
  75.                 '7' => " seven",
  76.                 '8' => " eight",
  77.                 '9' => " nine",
  78.                 _ => "Error"
  79.             };
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement