Вывести список студентов в порядке убывания - C#

Узнай цену своей работы

Формулировка задачи:

Добрый вечер. Ребята, помогите отсортировать считанный список. В файл нужно вывести фамилию и имя студента в порядке убывания их среднего балла. Очень нужно. Спасибо за внимание) Пример файла IN:
Листинг программы
  1. 4
  2. Ivanov Vasiliy 5 3 4
  3. Petrov Sergey 4 3 5
  4. Konstantinov Nikolay 5 5 5
  5. Kuznetsov Ivan 5 4 4
Листинг программы
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace New_3
  8. {
  9. class Student
  10. {
  11. public string surname;
  12. public string name;
  13. public byte phys;
  14. public byte mat;
  15. public byte info;
  16.  
  17. public Student()
  18. {
  19. surname = null;
  20. name = null;
  21. phys = 0;
  22. mat = 0;
  23. info = 0;
  24. }
  25. public Student(string surname, string name, byte mat, byte phys, byte info)
  26. {
  27. this.surname = surname;
  28. this.name = name;
  29. this.mat = mat;
  30. this.phys = phys;
  31. this.info = info;
  32. }
  33. public string Output()
  34. {
  35. return string.Format("{0} {1} {3} {4}", surname, name, mat, phys, info);
  36. }
  37. }
  38. class Program
  39. {
  40. static void Main(string[] args)
  41. {
  42. StreamReader input = new StreamReader("C:/IN.txt");
  43. byte n = byte.Parse(input.ReadLine());
  44. Console.WriteLine(n);
  45. Student[] students = new Student[n];
  46. string buf = null;
  47. string[] bufsplit;
  48. for (int i = 0; i < n; i++)
  49. {
  50. buf = input.ReadLine();
  51. bufsplit = buf.Split(' ');
  52. students[i] = new Student(bufsplit[0], bufsplit[1], byte.Parse(bufsplit[2]), byte.Parse(bufsplit[3]), byte.Parse(bufsplit[4]));
  53. Console.WriteLine("{0} {1}", i + 1, students[i].Output());
  54. }
  55. Console.WriteLine("Файл считан");
  56. StreamWriter output = new StreamWriter("C:/OUT.txt");
  57. //блок обработки считанных данных
  58. output.Close();
  59. input.Close();
  60. }
  61. }
  62. }

Решение задачи: «Вывести список студентов в порядке убывания»

textual
Листинг программы
  1. using System.IO;
  2. using System.Linq;
  3.  
  4. class Class13
  5. {
  6.     static void Main()
  7.     {
  8.         var t = File.ReadLines("in.txt").Skip(1).Select(n =>
  9.         {
  10.             var strings = n.Split();
  11.             int phys = int.Parse(strings[2]),
  12.                 mat = int.Parse(strings[3]),
  13.                 info = int.Parse(strings[4]);
  14.             return
  15.                 new
  16.                 {
  17.                     surname = strings[0],
  18.                     name = strings[1],
  19.                     average = ((double)phys + mat + info) / 3
  20.                 };
  21.         }).OrderByDescending(n => n.average).Select(n => n.surname + " " + n.name);
  22.  
  23.  
  24.         File.WriteAllLines("result.txt", t);
  25.     }
  26. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы