Advertisement
yahorrr

Untitled

Sep 14th, 2022
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Numbers
  4. {
  5.     public static class IntegerExtensions
  6.     {
  7.         /// <summary>
  8.         /// Obtains formalized information in the form of an enum <see cref="ComparisonSigns"/>
  9.         /// about the relationship of the order of two adjacent digits for all digits of a given number.
  10.         /// </summary>
  11.         /// <param name="number">Source number.</param>
  12.         /// <returns>Information in the form of an enum <see cref="ComparisonSigns"/>
  13.         /// about the relationship of the order of two adjacent digits for all digits of a given number
  14.         /// or null if the information is not defined.</returns>
  15.         public static ComparisonSigns? GetTypeComparisonSigns(this long number)
  16.         {
  17.             ComparisonSigns? result = 0;
  18.  
  19.             if (number == long.MinValue)
  20.             {
  21.                 return ComparisonSigns.MoreThan | ComparisonSigns.Equals | ComparisonSigns.LessThan;
  22.             }
  23.  
  24.             if (number < 0)
  25.             {
  26.                 number = -number;
  27.             }
  28.  
  29.             if (number < 10)
  30.             {
  31.                 return null;
  32.             }
  33.  
  34.             do
  35.             {
  36.                 if ((number / 10) % 10 < number % 10)
  37.                 {
  38.                     result |= ComparisonSigns.LessThan;
  39.                 }
  40.                 else if ((number / 10) % 10 > number % 10)
  41.                 {
  42.                     result |= ComparisonSigns.MoreThan;
  43.                 }
  44.                 else
  45.                 {
  46.                     result |= ComparisonSigns.Equals;
  47.                 }
  48.  
  49.                 number /= 10;
  50.             }
  51.             while (number < -10 || number > 10);
  52.  
  53.             return result;
  54.             }
  55.  
  56.         /// <summary>
  57.         /// Gets information in the form of a string about the type of sequence that the digit of a given number represents.
  58.         /// </summary>
  59.         /// <param name="number">Source number.</param>
  60.         /// <returns>The information in the form of a string about the type of sequence that the digit of a given number represents.</returns>
  61.         public static string GetTypeOfDigitsSequence(this long number)
  62.             {
  63.                 return GetTypeComparisonSigns(number) switch
  64.                 {
  65.                     ComparisonSigns.LessThan => "Strictly Increasing.",
  66.                     ComparisonSigns.LessThan | ComparisonSigns.Equals => "Increasing.",
  67.                     ComparisonSigns.MoreThan => "Strictly Decreasing.",
  68.                     ComparisonSigns.MoreThan | ComparisonSigns.Equals => "Decreasing.",
  69.                     ComparisonSigns.Equals => "Monotonous.",
  70.                     null => "One digit number.",
  71.                     _ => "Unordered."
  72.                 };
  73.             }
  74.         }
  75.     }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement