Bob103

C#_Struct

Dec 1st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. class Program
  2.     {
  3.         private static void Main()
  4.         {
  5.              // Читаем из файла
  6.             string[] allLines = File.ReadAllLines("input.txt");
  7.             // Преобразуем в массив студентов
  8.             Student[] students = new Student[allLines.Length];
  9.             for (int index = 0; index < allLines.Length; index++)
  10.             {
  11.                 string line = allLines[index];
  12.                 string[] fields = line.Split(';');
  13.                 Student student = new Student(fields[0], fields[1], Convert.ToInt32(fields[2]),fields[3]);
  14.                 students[index] = student;
  15.             }
  16.             //Сортируем
  17.             Array.Sort(students);
  18.             // Выводим данные
  19.            for (int i=0;i<students.Length;i++)
  20.             {
  21.                 if (students[i].Mas.Contains("2"))
  22.                 {
  23.                     Console.WriteLine(students[i]);
  24.                 }
  25.             }
  26.             // Преобразуем в удобный для записи вид
  27.             string[] linesToSave = new string[students.Length];
  28.             for (int i = 0; i < students.Length; i++)
  29.             {
  30.                 Student student = students[i];
  31.                 if (students[i].Mas.Contains("2"))
  32.                 {
  33.                     linesToSave[i] = student.ToString();
  34.                 }
  35.  
  36.             }
  37.             // Сохраняем в файл
  38.             File.WriteAllLines("output.txt", linesToSave);
  39.         }      
  40.     }
  41.  
  42.  
  43.     public struct Student : IComparable<Student>
  44.     {
  45.         public int CompareTo(Student obj)
  46.         {
  47.             if (Course > obj.Course)
  48.                 return 1;
  49.             if (Course < obj.Course)
  50.                 return -1;
  51.             else return 0;
  52.         }
  53.  
  54.         public Student(string surname, string fac, int course, string mas)
  55.             : this()
  56.         {
  57.             Surname = surname;
  58.             Fac = fac;
  59.             Course = course;
  60.             Mas = mas;
  61.         }
  62.  
  63.         // Фамилия
  64.         public string Surname { get; private set; }
  65.         // Факультет
  66.         public string Fac { get; private set; }
  67.         // Курс
  68.         public int Course { get; private set; }
  69.         // Оценки
  70.         public string Mas { get ; private set; }
  71.    
  72.  
  73.          
  74.  
  75.         public override string ToString()
  76.         {
  77.             return string.Format("{0}; {1}; {2}; {3}",
  78.                                  Surname, Fac, Course, Mas);
  79.         }
  80.     }
  81. Петров ; Фак2; 211; 3 3 3 3 3;
  82. Сидоров; Фак3; 311; 2 4 5 2 4;
  83. Пупкин ; Фак4; 411; 4 3 4 3 4;
  84. Таратайкин ;Фак5; 511; 5 5 2 5 5;
  85. Иванов ; Фак1; 111; 2 3 4 4 5;
Advertisement
Add Comment
Please, Sign In to add comment