Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace lab_6_z2_csharp
- {
- public class Cars : IComparable
- {
- private string mark;
- private string surname;
- public int year;
- public int len;
- public Cars() { }
- public Cars(string m, string s, int y, int l)
- {
- this.mark = m;
- this.surname = s;
- this.year = y;
- this.len = l;
- }
- public override string ToString() //Преобразование ToString()
- {
- return "" + this.mark + " " + this.surname + " " + " " + year.ToString() + " " + len.ToString() + "";
- }
- public int CompareTo(object obj)
- {
- Cars temp = obj as Cars;
- if (temp != null)
- {
- if (this.year >= temp.year)
- {
- return 1;
- }
- if (this.year < temp.year)
- {
- return -1;
- }
- }
- else
- {
- throw new ArgumentException("Not a Car");
- }
- return -100;
- }
- public static bool operator <(Cars c1, Cars c2)
- {
- bool flag = false;
- int i = c1.CompareTo(c2);
- if (i < 0)
- {
- flag = true;
- }
- return flag;
- }
- public static bool operator >(Cars c1, Cars c2)
- {
- bool flag = false;
- int i = c1.CompareTo(c2);
- if (i >= 0)
- {
- flag = true;
- }
- return flag;
- }
- }
- public class AutoLenComparer : IComparer
- {
- int IComparer.Compare(object o1, object o2)
- {
- Cars t1 = o1 as Cars;
- Cars t2 = o2 as Cars;
- int flag = -1;
- if (t1 != null && t2 != null)
- {
- if (t1.len >= t2.len)
- {
- flag = 1;
- }
- return flag;
- }
- else
- {
- throw new ArgumentException("Not a Cars");
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList list = new ArrayList();
- list.Add(new Cars("Audi R8", "Петров", 2010, 2000));
- list.Add(new Cars("Lambargini", "Сидоров", 2009, 100000));
- list.Add(new Cars("VAZ 2101", "Иванов", 1970, 1000));
- list.Add(new Cars("Chevrolet Lacetti", "Колесников", 2007, 80000));
- list.Add(new Cars("Suzuki Grand Vitara", "Щербакова", 2013, 70000));
- list.Add(new Cars("Renault Logan", "Дыбенко", 2015, 3000));
- Console.WriteLine("Before sort:");
- foreach (Cars i in list)
- {
- Console.WriteLine(i.ToString());
- }
- Cars c = new Cars("*", "*", 2011, 0);
- list.Sort(new AutoLenComparer());
- Console.WriteLine("After sort:");
- foreach (Cars i in list)
- {
- if (i<c)
- Console.WriteLine(i.ToString());
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement