Advertisement
sergezhu

Untitled

Apr 30th, 2023
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. namespace ConsoleApp1;
  2.  
  3. using System.Text;
  4.  
  5. public class Task22
  6. {
  7.     private const string MinPostfix = "(min)";
  8.     private const string MaxPostfix = "(max)";
  9.    
  10.     public void Run()
  11.     {
  12.         Console.InputEncoding = Encoding.Unicode;
  13.         Console.OutputEncoding = Encoding.Unicode;
  14.  
  15.         int randomRangeIncludedLowBound = 1;
  16.         int randomRangeExcludedHighBound = 10;
  17.         Random random = new Random();
  18.  
  19.         int minimumArrayLength = 1;
  20.         int minimumsMaximumsCalculationLength = 2;
  21.  
  22.         ConsoleColor defaultColor = ConsoleColor.White;
  23.         ConsoleColor localMinimumColor = ConsoleColor.Red;
  24.         ConsoleColor localMaximumColor = ConsoleColor.Green;
  25.  
  26.         bool canExit = false;
  27.  
  28.         while ( canExit == false )
  29.         {
  30.             Console.Clear();
  31.  
  32.             int arrayLength = 0;
  33.  
  34.             while ( arrayLength < minimumArrayLength )
  35.             {
  36.                 Console.Write( $"Input array length equal or greater than {minimumArrayLength} : " );
  37.                 arrayLength = int.Parse( Console.ReadLine() );
  38.             }
  39.  
  40.             int[] array = new int[arrayLength];
  41.  
  42.             for ( int i = 0; i < arrayLength; i++ )
  43.                 array[i] = random.Next( randomRangeIncludedLowBound, randomRangeExcludedHighBound );
  44.  
  45.             if ( arrayLength < minimumsMaximumsCalculationLength )
  46.             {
  47.                 int firstElementIndex = 0;
  48.                 Console.WriteLine($"Array contains one element {array[firstElementIndex]} and can not have minimums/maximums");
  49.             }
  50.             else
  51.             {
  52.                 for ( int i = 0; i < arrayLength; i++ )
  53.                 {
  54.                     Console.ForegroundColor = defaultColor;
  55.  
  56.                     bool isFirst = i == 0;
  57.                     bool isLast = i == arrayLength - 1;
  58.  
  59.                     bool isLocalMinimum = isFirst && array[i + 1] > array[i];
  60.                     isLocalMinimum |= isLast && array[i - 1] > array[i];
  61.                     isLocalMinimum |= isLast == false && isFirst == false && array[i + 1] > array[i] && array[i - 1] > array[i];
  62.  
  63.                     bool isLocalMaximum = isFirst && array[i + 1] < array[i];
  64.                     isLocalMaximum |= isLast && array[i - 1] < array[i];
  65.                     isLocalMaximum |= isLast == false && isFirst == false && array[i + 1] < array[i] && array[i - 1] < array[i];
  66.  
  67.                     ConsoleColor elementColor = isLocalMinimum
  68.                         ? localMinimumColor
  69.                         : isLocalMaximum
  70.                             ? localMaximumColor
  71.                             : defaultColor;
  72.  
  73.                     string elementPostfix = isLocalMinimum
  74.                         ? MinPostfix
  75.                         : isLocalMaximum
  76.                             ? MaxPostfix
  77.                             : string.Empty;
  78.  
  79.                     Console.ForegroundColor = elementColor;
  80.                     Console.Write( $"{array[i]}{elementPostfix} " );
  81.                 }
  82.                
  83.                 Console.WriteLine();
  84.             }
  85.  
  86.             Console.ForegroundColor = defaultColor;
  87.  
  88.             string properlyExitAnswer = "n";
  89.             Console.WriteLine( $"Continue? Enter \'{properlyExitAnswer}\' for exit" );
  90.             string continueAnswer = Console.ReadLine();
  91.  
  92.             canExit = string.Equals( continueAnswer, properlyExitAnswer );
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement