redSkywalker

задача 2 полиморф

Mar 6th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace задача_полиморф_2
  8. {
  9.     class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             string[] data = { "Hello", "World", "Sometime", "Brotherhood" };
  14.             foreach (string s in data)
  15.             {
  16.                 Console.Write(s + " ");
  17.             }
  18.             Console.WriteLine();
  19.  
  20.             IStringComparer comparer1 = new LengthComparer();
  21.             IStringComparer comparer2 = new FirstCharComparer();
  22.  
  23.             InsertionSort(data, comparer1);
  24.             foreach(string s in data)
  25.             {
  26.                 Console.Write(s + " ");
  27.             }
  28.             Console.WriteLine();
  29.  
  30.             InsertionSort(data, comparer2);
  31.             foreach (string s in data)
  32.             {
  33.                 Console.Write(s + " ");
  34.             }
  35.             Console.ReadKey();
  36.  
  37.  
  38.             //
  39.         }
  40.  
  41.        
  42.        
  43.  
  44.  
  45.  
  46.         public static void InsertionSort(string[] array, IStringComparer comparer)
  47.         {
  48.             for (int i = 1; i < array.Length; i++)
  49.             {
  50.                 string cur = array[i];
  51.                 int j = i;
  52.                 while (j > 0 && comparer.Compare(cur, array[j - 1]) == -1)
  53.                 {
  54.                     array[j] = array[j - 1];
  55.                     j--;
  56.                 }
  57.                 array[j] = cur;
  58.             }
  59.         }
  60.     }
  61.  
  62.     interface IStringComparer
  63.     {
  64.         /// <summary>
  65.         /// Возвращает 1 если первая строка больше второй
  66.         /// Возвращает 0 если строки равны
  67.         /// Возвращает -1 если первая строка меньше второй
  68.         /// </summary>
  69.         /// <param name="a">Первая строка</param>
  70.         /// <param name="b">Вторая строка</param>
  71.         /// <returns></returns>
  72.         int Compare(string a, string b);
  73.     }
  74.  
  75.     class LengthComparer:IStringComparer
  76.     {
  77.  
  78.         public int Compare(string a, string b)
  79.         {
  80.             int result=0;
  81.  
  82.             if (a.Length > b.Length)
  83.             {
  84.                 result = 1;
  85.             }
  86.             else if (a.Length < b.Length)
  87.             {
  88.                 result = -1;
  89.             }
  90.            
  91.             return result;                
  92.         }
  93.     }
  94.  
  95.     class FirstCharComparer : IStringComparer
  96.  
  97.     {
  98.         public int Compare(string a, string b)
  99.         {
  100.             int result = String.Compare(a, b, true); ;
  101.            
  102.             return result;
  103.         }
  104.  
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment