Advertisement
VictoriaLodochkina

lab 6 z2

Oct 21st, 2021
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace lab_6_z2_csharp
  9. {
  10.     public class Cars : IComparable
  11.     {
  12.         private string mark;
  13.         private string surname;
  14.         public int year;
  15.         public int len;
  16.         public Cars() { }
  17.         public Cars(string m, string s, int y, int l)
  18.         {
  19.             this.mark = m;
  20.             this.surname = s;
  21.             this.year = y;
  22.             this.len = l;
  23.         }
  24.  
  25.         public override string ToString()   //Преобразование ToString()
  26.         {
  27.             return "" + this.mark + "  " + this.surname + "  " + "  " + year.ToString() + "  " + len.ToString() + "";
  28.         }
  29.  
  30.         public int CompareTo(object obj)
  31.         {
  32.             Cars temp = obj as Cars;
  33.             if (temp != null)
  34.             {
  35.                 if (this.year >= temp.year)
  36.                 {
  37.                     return 1;
  38.                 }
  39.                 if (this.year < temp.year)
  40.                 {
  41.                     return -1;
  42.                 }
  43.             }
  44.             else
  45.             {
  46.                 throw new ArgumentException("Not a Car");
  47.                
  48.             }
  49.             return -100;
  50.         }
  51.  
  52.         public static bool operator <(Cars c1, Cars c2)
  53.         {
  54.             bool flag = false;
  55.             int i = c1.CompareTo(c2);
  56.             if (i < 0)
  57.             {
  58.                 flag = true;
  59.             }
  60.             return flag;
  61.         }
  62.  
  63.         public static bool operator >(Cars c1, Cars c2)
  64.         {
  65.             bool flag = false;
  66.             int i = c1.CompareTo(c2);
  67.             if (i >= 0)
  68.             {
  69.                 flag = true;
  70.             }
  71.             return flag;
  72.         }
  73.     }
  74.  
  75.     public class AutoLenComparer : IComparer
  76.     {
  77.         int IComparer.Compare(object o1, object o2)
  78.         {
  79.             Cars t1 = o1 as Cars;
  80.             Cars t2 = o2 as Cars;
  81.             int flag = -1;
  82.             if (t1 != null && t2 != null)
  83.             {
  84.                 if (t1.len >= t2.len)
  85.                 {
  86.                     flag = 1;
  87.                 }
  88.                 return flag;
  89.             }
  90.             else
  91.             {
  92.                 throw new ArgumentException("Not a Cars");
  93.             }
  94.         }
  95.     }
  96.     class Program
  97.     {
  98.         static void Main(string[] args)
  99.         {
  100.             ArrayList list = new ArrayList();
  101.             list.Add(new Cars("Audi R8", "Петров", 2010, 2000));
  102.             list.Add(new Cars("Lambargini", "Сидоров", 2009, 100000));
  103.             list.Add(new Cars("VAZ 2101", "Иванов", 1970, 1000));
  104.             list.Add(new Cars("Chevrolet Lacetti", "Колесников", 2007, 80000));
  105.             list.Add(new Cars("Suzuki Grand Vitara", "Щербакова", 2013, 70000));
  106.             list.Add(new Cars("Renault Logan", "Дыбенко", 2015, 3000));
  107.  
  108.             Console.WriteLine("Before sort:");
  109.             foreach (Cars i in list)
  110.             {
  111.                 Console.WriteLine(i.ToString());
  112.             }
  113.  
  114.             Cars c = new Cars("*", "*", 2011, 0);
  115.             list.Sort(new AutoLenComparer());
  116.             Console.WriteLine("After sort:");
  117.             foreach (Cars i in list)
  118.             {
  119.                 if (i<c)
  120.                 Console.WriteLine(i.ToString());
  121.             }
  122.  
  123.             Console.ReadLine();
  124.         }
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement