Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ConsoleApp1;
- using System.Text;
- public class Task22
- {
- private const string MinPostfix = "(min)";
- private const string MaxPostfix = "(max)";
- public void Run()
- {
- Console.InputEncoding = Encoding.Unicode;
- Console.OutputEncoding = Encoding.Unicode;
- int randomRangeIncludedLowBound = 1;
- int randomRangeExcludedHighBound = 10;
- Random random = new Random();
- int minimumArrayLength = 1;
- int minimumsMaximumsCalculationLength = 2;
- ConsoleColor defaultColor = ConsoleColor.White;
- ConsoleColor localMinimumColor = ConsoleColor.Red;
- ConsoleColor localMaximumColor = ConsoleColor.Green;
- bool canExit = false;
- while ( canExit == false )
- {
- Console.Clear();
- int arrayLength = 0;
- while ( arrayLength < minimumArrayLength )
- {
- Console.Write( $"Input array length equal or greater than {minimumArrayLength} : " );
- arrayLength = int.Parse( Console.ReadLine() );
- }
- int[] array = new int[arrayLength];
- for ( int i = 0; i < arrayLength; i++ )
- array[i] = random.Next( randomRangeIncludedLowBound, randomRangeExcludedHighBound );
- if ( arrayLength < minimumsMaximumsCalculationLength )
- {
- int firstElementIndex = 0;
- Console.WriteLine($"Array contains one element {array[firstElementIndex]} and can not have minimums/maximums");
- }
- else
- {
- for ( int i = 0; i < arrayLength; i++ )
- {
- Console.ForegroundColor = defaultColor;
- bool isFirst = i == 0;
- bool isLast = i == arrayLength - 1;
- bool isLocalMinimum = isFirst && array[i + 1] > array[i];
- isLocalMinimum |= isLast && array[i - 1] > array[i];
- isLocalMinimum |= isLast == false && isFirst == false && array[i + 1] > array[i] && array[i - 1] > array[i];
- bool isLocalMaximum = isFirst && array[i + 1] < array[i];
- isLocalMaximum |= isLast && array[i - 1] < array[i];
- isLocalMaximum |= isLast == false && isFirst == false && array[i + 1] < array[i] && array[i - 1] < array[i];
- ConsoleColor elementColor = isLocalMinimum
- ? localMinimumColor
- : isLocalMaximum
- ? localMaximumColor
- : defaultColor;
- string elementPostfix = isLocalMinimum
- ? MinPostfix
- : isLocalMaximum
- ? MaxPostfix
- : string.Empty;
- Console.ForegroundColor = elementColor;
- Console.Write( $"{array[i]}{elementPostfix} " );
- }
- Console.WriteLine();
- }
- Console.ForegroundColor = defaultColor;
- string properlyExitAnswer = "n";
- Console.WriteLine( $"Continue? Enter \'{properlyExitAnswer}\' for exit" );
- string continueAnswer = Console.ReadLine();
- canExit = string.Equals( continueAnswer, properlyExitAnswer );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement