Advertisement
myname0

12_5

Nov 19th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1.  abstract public class Person: IComparable
  2.     {
  3.         abstract public void Print();
  4.         abstract public int Age();
  5.         public int CompareTo(object obj)
  6.         {
  7.             Person a = (Person) obj;
  8.             if (this.Age() == a.Age()) return 0;
  9.             else if (this.Age() < a.Age()) return -1;
  10.             else return 1;
  11.         }
  12.     }
  13.  
  14.  class Enrollee: Person
  15.     {
  16.         private string name;
  17.         private string DOB;
  18.         private string faculty;
  19.  
  20.         public Enrollee (string _name, string _DOB, string _faculty)
  21.         {
  22.             name = _name;
  23.             DOB = _DOB;
  24.             faculty = _faculty;
  25.         }
  26.  
  27.         public override void Print()
  28.         {
  29.             Console.Write("{0}, {1}, {2}, ", name, DOB, faculty);
  30.         }
  31.  
  32.         public override int Age()
  33.         {
  34.             string time = DateTime.Now.ToShortDateString();
  35.             int day1 = int.Parse(DOB.Substring(0, 2));
  36.             int month1 = int.Parse(DOB.Substring(3, 2));
  37.             int year1 = int.Parse(DOB.Substring(6));
  38.             int day2 = int.Parse(time.Substring(0, 2));
  39.             int month2 = int.Parse(time.Substring(3, 2));
  40.             int year2 = int.Parse(time.Substring(6));
  41.             if (month1 < month2) return year2 - year1;
  42.             else if (month1 == month2)
  43.             {
  44.                 if (day1 <= day2)
  45.                     return year2 - year1;
  46.                 else return year2 - year1 - 1;
  47.             }
  48.             else return year2 - year1 - 1;
  49.         }
  50.     }
  51.  
  52. class Student: Person
  53.     {
  54.         private string name;
  55.         private string DOB;
  56.         private string faculty;
  57.         private int course;
  58.  
  59.         public Student(string _name, string _DOB, string _faculty, int _course)
  60.         {
  61.             name = _name;
  62.             DOB = _DOB;
  63.             faculty = _faculty;
  64.             course = _course;
  65.         }
  66.  
  67.         public override void Print ()
  68.         {
  69.             Console.Write("{0}, {1}, {2}, {3}, ", name, DOB, faculty, course);
  70.         }
  71.  
  72.         public override int Age()
  73.         {
  74.             string time = DateTime.Now.ToShortDateString();
  75.             int day1 = int.Parse(DOB.Substring(0, 2));
  76.             int month1 = int.Parse(DOB.Substring(3, 2));
  77.             int year1 = int.Parse(DOB.Substring(6));
  78.             int day2 = int.Parse(time.Substring(0, 2));
  79.             int month2 = int.Parse(time.Substring(3, 2));
  80.             int year2 = int.Parse(time.Substring(6));
  81.             if (month1 < month2) return year2 - year1;
  82.             else if (month1 == month2)
  83.             {
  84.                 if (day1 <= day2)
  85.                     return year2 - year1;
  86.                 else return year2 - year1 - 1;
  87.             }
  88.             else return year2 - year1 - 1;
  89.         }
  90.     }
  91.  
  92.  class Teacher: Person
  93.     {
  94.         private string name;
  95.         private string DOB;
  96.         private string faculty;
  97.         private string post;
  98.         private int experience;
  99.  
  100.         public Teacher (string _name, string _DOB, string _faculty, string _post, int _experience)
  101.         {
  102.             name = _name;
  103.             DOB = _DOB;
  104.             faculty = _faculty;
  105.             post = _post;
  106.             experience = _experience;
  107.         }
  108.  
  109.         public override void Print ()
  110.         {
  111.             Console.Write("{0}, {1}, {2}, {3}, {4}, ",name, DOB, faculty, post, experience);
  112.         }
  113.  
  114.         public override int Age()
  115.         {
  116.             string time = DateTime.Now.ToShortDateString();
  117.             int day1 = int.Parse(DOB.Substring(0, 2));
  118.             int month1 = int.Parse(DOB.Substring(3, 2));
  119.             int year1 = int.Parse(DOB.Substring(6));
  120.             int day2 = int.Parse(time.Substring(0, 2));
  121.             int month2 = int.Parse(time.Substring(3, 2));
  122.             int year2 = int.Parse(time.Substring(6));
  123.             if (month1 < month2) return year2 - year1;
  124.             else if (month1 == month2)
  125.             {
  126.                 if (day1 <= day2)
  127.                     return year2 - year1;
  128.                 else return year2 - year1 - 1;
  129.             }
  130.             else return year2 - year1 - 1;
  131.         }
  132.     }
  133.  
  134.  class Program
  135.     {
  136.         static void Print(List<Person> list)
  137.         {
  138.                 foreach (Person item in list)
  139.                 {
  140.                     item.Print();
  141.                     Console.WriteLine("{0}", item.Age());
  142.                 }
  143.         }
  144.  
  145.         static List<Person> Read()
  146.         {
  147.             using (StreamReader fin = new StreamReader("D:/input.txt"))
  148.             {
  149.                 int n = int.Parse(fin.ReadLine());
  150.                 List<Person> list = new List<Person>();
  151.                 for (int i = 0; i < n; i++)
  152.                 {
  153.                     string[] text = fin.ReadLine().Split(' ');
  154.                     if (text.Length == 3)
  155.                         list.Add(new Enrollee(text[0], text[1], text[2]));
  156.                     else if (text.Length == 4)
  157.                         list.Add(new Student(text[0], text[1], text[2], int.Parse(text[3])));
  158.                     else if (text.Length == 5)
  159.                         list.Add(new Teacher(text[0], text[1], text[2], text[3], int.Parse(text[4])));
  160.                     else Console.WriteLine("В {0}-ой строчке ошибка в данных", i+1);
  161.                 }
  162.                 return list;
  163.             }  
  164.         }
  165.  
  166.         static void Search(List<Person> list)
  167.         {
  168.             Console.WriteLine("Enter range og the age a: ");
  169.             int a = int.Parse(Console.ReadLine());
  170.             Console.WriteLine("b: ");
  171.             int b = int.Parse(Console.ReadLine());
  172.             foreach (Person item in list)
  173.             {
  174.                 if ((item.Age() >= a) && (item.Age() <= b))
  175.                 {
  176.                     item.Print();
  177.                     Console.WriteLine(item.Age());
  178.                 }
  179.             }
  180.         }
  181.  
  182.         static void Main(string[] args)
  183.        
  184.        {
  185.             List<Person> list = Read();
  186.             Print(list);
  187.             Search(list);
  188.             Console.WriteLine();
  189.             list.Sort();
  190.             Print(list);
  191.         }
  192.  
  193. 10
  194. Akunin 15.12.1996 Biofac 4
  195. Fedorov 21.12.2001 Himfac
  196. Chernov 30.04.1947 Fisfac 3
  197. Gorina 26.01.1982 IFIG assistent 5
  198. Kuzmina 15.03.1973 KNIIT
  199. Leshin 01.09.1976 NLP dotzent 12
  200. Kudrin 03.11.1994 Fisfac
  201. Rodnin 30.11.1973 Himfac
  202. Voevodina 01.12.1969 KNIIT prepodavatel' 23
  203. Burkina 29.11.1996 KNIIT 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement