Advertisement
Guest User

Говнокод

a guest
Jan 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace Lab_16
  6. {
  7.     class MainClass
  8.     {
  9.         // Объявление переменных
  10.         static int[] numbers;
  11.         static int[] qSortNumbers;
  12.         static int[] bubbleSort;
  13.         static int size;
  14.         // Сортировка пузырьком
  15.         public static int[] BubbleSort(int[] arr)
  16.         {
  17.             int[] res = arr;
  18.             int temp;
  19.             for (int i = 0; i < arr.Length; i++)
  20.             {
  21.                 for (int j = i + 1; j < arr.Length; j++)
  22.                 {
  23.                     if (res[i] > res[j])
  24.                     {
  25.                         temp = res[i];
  26.                         res[i] = res[j];
  27.                         res[j] = temp;
  28.                     }
  29.                 }
  30.             }
  31.             return res;
  32.         }
  33.         // Сортировка QSort
  34.         static void QuickSort(int[] arr, int l, int r)
  35.         {
  36.             if (l >= r)
  37.             {
  38.                 return;
  39.             }
  40.             int temp;
  41.             int m = l;
  42.             for (int i = m; i < r; i++)
  43.             {
  44.                 if (arr[i] <= arr[r])
  45.                 {
  46.                     temp = arr[m];
  47.                     arr[m] = arr[i];
  48.                     arr[i] = temp;
  49.                     m++;
  50.                 }
  51.             }
  52.             temp = arr[m];
  53.             arr[m] = arr[r];
  54.             arr[r] = temp;
  55.             QuickSort(arr, l, m - 1);
  56.             QuickSort(arr, m + 1, r);
  57.         }
  58.         // Удаление повторяющихся элементов из массива
  59.         public static int[] deleteRepetititve(int[] arr)
  60.         {
  61.             ArrayList list = new ArrayList();
  62.             for (int i = 0; i < arr.Length; i++)
  63.             {
  64.                 if (!list.Contains(arr[i]))
  65.                 {
  66.                     list.Add(arr[i]);
  67.                 }
  68.             }
  69.             int[] res = (Int32[])list.ToArray(typeof(int));
  70.             return res;
  71.         }
  72.         // Отображение массива и его описания
  73.         public static void DisplayArray(int[] arr, String arrOperation)
  74.         {
  75.             Console.WriteLine(arrOperation);
  76.             for (int i = 0; i < arr.Length; i++)
  77.             {
  78.                 Console.Write(arr[i] + " ");
  79.             }
  80.             Console.WriteLine();
  81.         }
  82.         // Отображение указанных пользователем минимума и максимума
  83.         public static void DisplayMinAndMax(int[] arr)
  84.         {
  85.             int m, n;
  86.             int max = Int32.MinValue;
  87.             int maxCounter = 0;
  88.             int min = Int32.MaxValue;
  89.             int minCounter = 0;
  90.             try
  91.             {
  92.                 List<int> listmax = new List<int>();
  93.                 List<int> listmin = new List<int>();
  94.                 Console.WriteLine("Enter necessary min");
  95.                 m = EnterInt(3);
  96.                 Console.WriteLine("Enter necessary high");
  97.                 n = EnterInt(3);
  98.                 listmax = arr.ToList();
  99.                 for (int i = 0; i < n; i++)
  100.                 {
  101.                     max = Int32.MinValue;
  102.                     for (int j = 0; j < listmax.Count(); j++)
  103.                     {
  104.                         if (listmax[j] > max)
  105.                         {
  106.                             max = listmax[j];
  107.                             maxCounter = j;
  108.                         }
  109.                     }
  110.                     if (i != n - 1)
  111.                     {
  112.                         listmax.Remove(max);
  113.                     }
  114.                 }
  115.                 Console.WriteLine(n + "-й максимум " + listmax[maxCounter]);
  116.                 listmin = arr.ToList();
  117.                 for (int i = 0; i < m; i++)
  118.                 {
  119.                     min = Int32.MaxValue;
  120.                     for (int j = 0; j < listmin.Count(); j++)
  121.                     {
  122.                         if (listmin[j] < min)
  123.                         {
  124.                             min = listmin[j];
  125.                             minCounter = j;
  126.                         }
  127.                     }
  128.                     if (i != m - 1)
  129.                     {
  130.                         listmin.Remove(min);
  131.                     }
  132.                 }
  133.                 Console.WriteLine(m + "-й минимум " + listmin[minCounter]);
  134.  
  135.             }
  136.             catch (Exception e)
  137.             {
  138.                 Console.WriteLine("Sorry, an error was occured: " + e);
  139.                 return;
  140.             }
  141.         }
  142.         // Основная часть программы и ее дополнительные компоненты
  143.         public static void ProcessingArray()
  144.         {
  145.             DisplayArray(BubbleSort(bubbleSort), "Sorted array (Bubble sort)");
  146.             QuickSort(qSortNumbers, 0, qSortNumbers.Length - 1);
  147.             DisplayArray(qSortNumbers, "Sorted array (QSort)");
  148.             DisplayArray(deleteRepetititve(numbers), "Deleted repetitives");
  149.             DisplayMinAndMax(deleteRepetititve(numbers));
  150.             Console.WriteLine("Press any key to continue...");
  151.         }
  152.  
  153.         public static int EnterInt(int type)
  154.         {
  155.             int a;
  156.             switch (type)
  157.             {
  158.                 case 1:
  159.                     {
  160.                         try
  161.                         {
  162.                             a = Int32.Parse(Console.ReadLine());
  163.                             return a;
  164.                         }
  165.                         catch (Exception)
  166.                         {
  167.                             Console.WriteLine("Sorry, an error was occured, please try again: ");
  168.                             a = EnterInt(1);
  169.                             return a;
  170.                         }
  171.                     }
  172.                     break;
  173.                 case 2:
  174.                     {
  175.                         try
  176.                         {
  177.                             a = Int32.Parse(Console.ReadLine());
  178.                             if (a > 0 & a <= 100)
  179.                             {
  180.                                 return a;
  181.                             } else
  182.                             {
  183.                                 Console.WriteLine("Sorry, an error was occured, please try again: ");
  184.                                 a = EnterInt(2);
  185.                                 return a;
  186.                             }
  187.                            
  188.                         }
  189.                         catch (Exception)
  190.                         {
  191.                             Console.WriteLine("Sorry, an error was occured, please try again: ");
  192.                             a = EnterInt(2);
  193.                             return a;
  194.                         }
  195.                     }
  196.                     break;
  197.                 case 3:
  198.                     {
  199.                         try
  200.                         {
  201.                             a = Int32.Parse(Console.ReadLine());
  202.                             if (a > 0 & a < size)
  203.                             {
  204.                                 return a;
  205.                             }
  206.                             else
  207.                             {
  208.                                 Console.WriteLine("Sorry, an error was occured, please try again: ");
  209.                                 a = EnterInt(3);
  210.                                 return a;
  211.                             }
  212.                         }
  213.                         catch (Exception)
  214.                         {
  215.                             Console.WriteLine("Sorry, an error was occured, please try again: ");
  216.                             a = EnterInt(3);
  217.                             return a;
  218.                         }
  219.                     }
  220.                     break;
  221.                 case 4:
  222.                     try
  223.                     {
  224.                         a = Int32.Parse(Console.ReadLine());
  225.                         if (a == 1 | a == 2)
  226.                         {
  227.                             return a;
  228.                         }
  229.                         else
  230.                         {
  231.                             Console.WriteLine("Sorry, an error was occured, please try again: ");
  232.                             a = EnterInt(4);
  233.                             return a;
  234.                         }
  235.  
  236.                     }
  237.                     catch (Exception)
  238.                     {
  239.                         Console.WriteLine("Sorry, an error was occured, please try again: ");
  240.                         a = EnterInt(4);
  241.                         return a;
  242.                     }
  243.                     break;
  244.                 default:
  245.                     {
  246.                         return 0;
  247.                     }
  248.                     break;
  249.             }
  250.         }
  251.        
  252.         public static void Main(string[] args)
  253.         {
  254.             Console.WriteLine("Enter array size (max is 100)");
  255.             size = EnterInt(2);
  256.             numbers = new int[size];
  257.             qSortNumbers = new int[size];
  258.             bubbleSort = new int[size];
  259.             Random random = new Random();
  260.             Console.WriteLine("Choose input type: 1 for auto, 2 for manual");
  261.             switch (EnterInt(4))
  262.             {
  263.                 case 1:
  264.                     Console.WriteLine("Your array:");
  265.                     for (int i = 0; i < numbers.Length; i++)
  266.                     {
  267.                         numbers[i] = random.Next(-100, 101);
  268.                         qSortNumbers[i] = numbers[i];
  269.                         bubbleSort[i] = numbers[i];
  270.                         Console.Write(numbers[i] + " ");
  271.                     }
  272.                     Console.WriteLine();
  273.                     ProcessingArray();
  274.                     break;
  275.                 case 2:
  276.                     for (int i = 0; i < numbers.Length; i++)
  277.                         {
  278.                             numbers[i] = EnterInt(1);
  279.                             qSortNumbers[i] = numbers[i];
  280.                             bubbleSort[i] = numbers[i];
  281.                            
  282.                         }
  283.                     ProcessingArray();
  284.                     break;
  285.                 default:
  286.                     Console.WriteLine("Sorry, an unknown error was occured");
  287.                     break;
  288.             }
  289.             Console.ReadLine();
  290.         }
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement