Advertisement
Maxim_Kuraksin

LINQ

Nov 1st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Kuraxin_LINQ
  8. {
  9.     class Student
  10.     {
  11.         public string Name { get; set; }
  12.         public string SurName { get; set; }
  13.         public string TestResult { get; set; }
  14.     }
  15.  
  16.     class Menu
  17.     {
  18.         public static void PrintMenu()
  19.         {
  20.             Console.Clear();
  21.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  22.             Console.WriteLine("");
  23.             Console.Write("--------------------------------------------------------------------------------");
  24.             Console.WriteLine("                                 МЕНЮ         ");
  25.             Console.Write("--------------------------------------------------------------------------------");
  26.             Console.ForegroundColor = ConsoleColor.White;
  27.             Console.Write("           1. ");
  28.             Console.ResetColor();
  29.             Console.WriteLine("Вывести список всех студентов");
  30.             Console.ForegroundColor = ConsoleColor.White;
  31.             Console.Write("           2. ");
  32.             Console.ResetColor();
  33.             Console.WriteLine("Вывести список студентов, которые сдали тест на 5");
  34.             Console.ForegroundColor = ConsoleColor.White;
  35.             Console.Write("           3. ");
  36.             Console.ResetColor();
  37.             Console.WriteLine("Вывести рейтинг студентов, начиная с отличников");
  38.             Console.ForegroundColor = ConsoleColor.White;
  39.             Console.Write("           4. ");
  40.             Console.ResetColor();
  41.             Console.WriteLine("Определить кого больше: отличников или двоечников");
  42.             Console.ForegroundColor = ConsoleColor.White;
  43.             Console.Write("           5. ");
  44.             Console.ResetColor();
  45.             Console.WriteLine("Разбить студентов на группы по первой букве");
  46.             Console.ForegroundColor = ConsoleColor.White;
  47.             Console.Write("           6. ");
  48.             Console.ResetColor();
  49.             Console.WriteLine("Найти разность между max и min баллом");
  50.             Console.ForegroundColor = ConsoleColor.White;
  51.             Console.Write("           7. ");
  52.             Console.ResetColor();
  53.             Console.WriteLine("Добавить в список вторую попытку сдачи теста");
  54.             Console.ForegroundColor = ConsoleColor.White;
  55.             Console.Write("           8. ");
  56.             Console.ResetColor();
  57.             Console.WriteLine("Создать список результатов студентов по тесту");
  58.             Console.ForegroundColor = ConsoleColor.White;
  59.             Console.Write("           9. ");
  60.             Console.ResetColor();
  61.             Console.WriteLine("Выход");
  62.  
  63.  
  64.             Console.ForegroundColor = ConsoleColor.White;
  65.             Console.WriteLine("");
  66.         }
  67.     }
  68.  
  69.  
  70.     class Queries
  71.     {
  72.         static public void ListOfStudents(List<Student> students)
  73.         {
  74.             Console.Clear();
  75.             Console.WriteLine("                    СПИСОК ВСЕХ СТУДЕНТОВ");
  76.             Console.WriteLine("");
  77.             Console.ResetColor();
  78.             Console.WriteLine("    Фамилия         |        Имя           |    Баллы");
  79.             Console.WriteLine("--------------------------------------------------------");
  80.  
  81.  
  82.             var AllStudents = from a in students select a; // Выбрать все
  83.  
  84.             foreach (Student stud in AllStudents)
  85.             {
  86.                 Console.WriteLine("    {0,-15} |      {1, -15} |     {2, -3}", stud.SurName, stud.Name, stud.TestResult);
  87.             }
  88.         }
  89.  
  90.         static public void ListOfBestStudents(List<Student> students) // Вывести список студентов, которые сдали тест на 5
  91.         {
  92.             //IEnumerable<Student> BestStudents = from a in students select a;
  93.  
  94.             Console.Clear();
  95.             Console.WriteLine("                    СПИСОК СТУДЕНТОВ ОТЛИЧНИКОВ");
  96.             Console.WriteLine("");
  97.             Console.ResetColor();
  98.             Console.WriteLine("    Фамилия         |        Имя           |    Баллы");
  99.             Console.WriteLine("--------------------------------------------------------");
  100.  
  101.  
  102.             var BestStudents = from a in students where Convert.ToInt32(a.TestResult) >= 86 select a;
  103.  
  104.             foreach (Student stud in BestStudents)
  105.             {
  106.                 Console.WriteLine("    {0,-15} |      {1, -15} |     {2, -3}", stud.SurName, stud.Name, stud.TestResult);
  107.             }
  108.         }
  109.  
  110.         static public void TopList(List<Student> students) // Вывести рейтинг студентов, начиная с отличников
  111.         {
  112.             Console.Clear();
  113.             Console.WriteLine("                    РЕЙТИНГ СТУДЕНТОВ");
  114.             Console.WriteLine("");
  115.             Console.ResetColor();
  116.             Console.WriteLine("    Фамилия         |        Имя           |    Баллы");
  117.             Console.WriteLine("--------------------------------------------------------");
  118.  
  119.             var TopOfStudents = from a in students orderby a.TestResult descending select a;
  120.  
  121.             foreach (Student stud in TopOfStudents)
  122.             {
  123.                 Console.WriteLine("    {0,-15} |      {1, -15} |     {2, -3}", stud.SurName, stud.Name, stud.TestResult);
  124.             }
  125.         }
  126.  
  127.         static public void DifferenceOfBest(List<Student> students) // Определить кого больше: отличников или двоечников
  128.         {
  129.             int BestStudents = (from a in students where Convert.ToInt32(a.TestResult) >= 86 select a).Count();
  130.             int BadStudents = (from a in students where Convert.ToInt32(a.TestResult) <= 50 select a).Count();
  131.  
  132.             Console.Clear();
  133.             Console.WriteLine("Количество отличников: {0} ", BestStudents);
  134.             Console.WriteLine("Количество двоечников: {0} ", BadStudents);
  135.  
  136.             if (BestStudents > BadStudents)
  137.             {
  138.                 Console.ForegroundColor = ConsoleColor.Green;
  139.                 Console.WriteLine("Отличников больше");
  140.                 Console.ResetColor();
  141.             }
  142.  
  143.             if (BestStudents < BadStudents)
  144.             {
  145.                 Console.ForegroundColor = ConsoleColor.Red;
  146.                 Console.WriteLine("Двоечников больше");
  147.                 Console.ResetColor();
  148.             }
  149.  
  150.             if (BestStudents == BadStudents)
  151.             {
  152.                 Console.WriteLine("Равное количество отличников и двоечников");
  153.             }
  154.  
  155.         }
  156.  
  157.         static public void GroupByLetters(List<Student> students) // Разбить студентов на группы по первой букве
  158.         {
  159.             Console.Clear();
  160.             Console.WriteLine("             СПИСОК ВСЕХ СТУДЕНТОВ ПО АЛФАВИТУ");
  161.             Console.WriteLine("");
  162.             Console.ResetColor();
  163.             Console.WriteLine("    Фамилия         |        Имя           |    Баллы");
  164.  
  165.             var groups = from student in students group student by student.SurName[0] into letter orderby letter.Key select letter;
  166.  
  167.             foreach (var letter in groups)
  168.             {
  169.                 Console.WriteLine("-{0}-------------------------------------------------------", letter.Key);
  170.  
  171.                 foreach (var stud in letter)
  172.                 {
  173.                     Console.WriteLine("    {0,-15} |      {1, -15} |     {2, -3}", stud.SurName, stud.Name, stud.TestResult);
  174.                 }
  175.             }
  176.         }
  177.  
  178.         static public void ResultDifference(List<Student> students) // Найти разность между max и min баллом
  179.         {
  180.             int MaxResult = students.Max(point => Convert.ToInt32(point.TestResult));
  181.             int MinResult = students.Min(point => Convert.ToInt32(point.TestResult));
  182.  
  183.             Console.Clear();
  184.             Console.WriteLine("Максимальный балл: {0} ", MaxResult);
  185.             Console.WriteLine("Минимальный балл: {0} ", MinResult);
  186.             Console.Write("");
  187.             Console.WriteLine("Разность: {0} ", MaxResult - MinResult);
  188.         }
  189.  
  190.         static public void NewResult(List<Student> students, int index) // Добавить в список вторую попытку сдачи теста
  191.         {
  192.  
  193.             Console.Write("Введите баллы студента за вторую попытку: ");
  194.             string NewTestResult = Console.ReadLine();
  195.  
  196.             students.Add(new Student { Name = students[index - 1].Name, SurName = students[index - 1].SurName, TestResult = NewTestResult });
  197.         }
  198.  
  199.         static public void ResultOfTest(List<Student> students) // Создать список результатов студентов по тесту
  200.         {
  201.             Console.Clear();
  202.             Console.WriteLine("                    СПИСОК РЕЗУЛЬТАТОВ ПО ТЕСТУ");
  203.             Console.WriteLine("");
  204.             Console.ResetColor();
  205.             Console.WriteLine("    Фамилия         |        Имя           |    Оценка");
  206.             Console.WriteLine("--------------------------------------------------------");
  207.  
  208.             List<Student> NewList = new List<Student> { };
  209.  
  210.             var groups = from surg in students group surg by surg.SurName;
  211.  
  212.             foreach (var gr in groups)
  213.             {
  214.                 foreach (Student stud in gr)
  215.                 {
  216.                     if (stud.TestResult == gr.Max(tr => tr.TestResult))
  217.                     {
  218.                         Console.WriteLine("    {0,-15} |      {1, -15} |       {2, -2}", stud.SurName, stud.Name, stud.TestResult);
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.  
  224.         static public void NumberListOfStudents(List<Student> students)
  225.         {
  226.             Console.Clear();
  227.             Console.WriteLine("                    СПИСОК ВСЕХ СТУДЕНТОВ");
  228.             Console.WriteLine("");
  229.             Console.ResetColor();
  230.             Console.WriteLine("     №    |     Фамилия        |         Имя          |    Баллы");
  231.             Console.WriteLine("-------------------------------------------------------------------");
  232.  
  233.  
  234.             var AllStudents = from a in students select a; // Выбрать все
  235.  
  236.             foreach (Student stud in AllStudents)
  237.             {
  238.                 Console.WriteLine("    {0,-3}   |    {1,-15} |      {2, -15} |     {3, -3}", students.IndexOf(stud) + 1, stud.SurName, stud.Name, stud.TestResult);
  239.             }
  240.         }
  241.  
  242.     }
  243.  
  244.     class Program
  245.     {
  246.         static void Main(string[] args)
  247.         {
  248.             int choose;
  249.             List<Student> students = new List<Student>() {
  250.                     new Student {Name = "Андрей", SurName = "Иванов", TestResult = "96"},
  251.                     new Student {Name = "Вера", SurName = "Котова", TestResult = "90"},
  252.                     new Student {Name = "Виталий", SurName = "Собакин", TestResult = "85"},
  253.                     new Student {Name = "Зинаида", SurName = "Герасимова", TestResult = "81"},
  254.                     new Student {Name = "Алексей", SurName = "Навальный", TestResult = "44"},
  255.                     new Student {Name = "Пётр", SurName = "Лужин", TestResult = "80"},
  256.                     new Student {Name = "Анна", SurName = "Каренина", TestResult = "72"},
  257.                     new Student {Name = "Михаил", SurName = "Сидоров", TestResult = "72"},
  258.                     new Student {Name = "Виктор", SurName = "Петров", TestResult = "27"},
  259.                     new Student {Name = "Екатерина", SurName = "Романова", TestResult = "92"},
  260.                     new Student {Name = "Аркадий", SurName = "Попов", TestResult = "57"},
  261.                     new Student {Name = "Алексей", SurName = "Патрушев", TestResult = "7"},
  262.                     new Student {Name = "Елизавета", SurName = "Алиева", TestResult = "64"},
  263.                     new Student {Name = "Анна", SurName = "Веселова", TestResult = "53"},
  264.                     new Student {Name = "Дмитрий", SurName = "Бариев", TestResult = "47"},
  265.                     new Student {Name = "Иван", SurName = "Карамазов", TestResult = "98"}
  266.             };
  267.  
  268.             void MenuInput()
  269.             {
  270.                 Console.ResetColor();
  271.                 try
  272.                 {
  273.                     Console.Write("           Введите номер запроса: ");
  274.                     choose = Convert.ToInt32(Console.ReadLine());
  275.                 }
  276.                 catch (FormatException)
  277.                 {
  278.                     Console.ForegroundColor = ConsoleColor.Red;
  279.                     Console.WriteLine("");
  280.                     Console.WriteLine("                  Ошибка ввода. Нажмите 0, чтобы вывести список запросов.");
  281.                     Console.WriteLine("");
  282.                     MenuInput();
  283.                 }
  284.  
  285.                 switch (choose)
  286.                 {
  287.                     case 0:
  288.                         {
  289.                             Menu.PrintMenu();
  290.                             MenuInput();
  291.                             break;
  292.                         }
  293.                     case 1:
  294.                         {
  295.                             // Вывести список всех студентов
  296.                             Queries.ListOfStudents(students);
  297.                             Console.WriteLine("");
  298.                             Console.Write("Вернуться в меню?");
  299.                             Console.ReadKey(true);
  300.                             Menu.PrintMenu();
  301.                             MenuInput();
  302.                             break;
  303.                         }
  304.                     case 2:
  305.                         {
  306.                             // Вывести список студентов, которые сдали тест на 5
  307.                             Queries.ListOfBestStudents(students);
  308.                             Console.WriteLine("");
  309.                             Console.Write("Вернуться в меню?");
  310.                             Console.ReadKey(true);
  311.                             Menu.PrintMenu();
  312.                             MenuInput();
  313.                             break;
  314.                         }
  315.                     case 3:
  316.                         {
  317.                             // Вывести рейтинг студентов, начиная с отличников
  318.                             Queries.TopList(students);
  319.                             Console.WriteLine("");
  320.                             Console.Write("Вернуться в меню?");
  321.                             Console.ReadKey(true);
  322.                             Menu.PrintMenu();
  323.                             MenuInput();
  324.                             break;
  325.                         }
  326.                     case 4:
  327.                         {
  328.                             // Определить кого больше: отличников или двоечников
  329.                             Queries.DifferenceOfBest(students);
  330.                             Console.WriteLine("");
  331.                             Console.Write("Вернуться в меню?");
  332.                             Console.ReadKey(true);
  333.                             Menu.PrintMenu();
  334.                             MenuInput();
  335.                             break;
  336.                         }
  337.                     case 5:
  338.                         {
  339.                             // Разбить студентов на группы по первой букве
  340.                             Queries.GroupByLetters(students);
  341.                             Console.WriteLine("");
  342.                             Console.Write("Вернуться в меню?");
  343.                             Console.ReadKey(true);
  344.                             Menu.PrintMenu();
  345.                             MenuInput();
  346.                             break;
  347.                         }
  348.                     case 6:
  349.                         {
  350.                             // Найти разность между max и min баллом
  351.                             Queries.ResultDifference(students);
  352.                             Console.WriteLine("");
  353.                             Console.Write("Вернуться в меню?");
  354.                             Console.ReadKey(true);
  355.                             Menu.PrintMenu();
  356.                             MenuInput();
  357.                             break;
  358.                         }
  359.                     case 7:
  360.                         {
  361.                             // Добавить в список вторую попытку сдачи теста
  362.                             Queries.NumberListOfStudents(students);
  363.                             Console.WriteLine("");
  364.                             Console.Write("Выберите номер студента: ");
  365.                             Queries.NewResult(students, Convert.ToInt32(Console.ReadLine()));
  366.                             Console.Write("Вывести новый список?");
  367.                             Console.WriteLine("");
  368.                             Console.ReadKey(true);
  369.                             Queries.ListOfStudents(students);
  370.                             Console.WriteLine("");
  371.                             Console.Write("Вернуться в меню?");
  372.                             Console.ReadKey(true);
  373.                             Menu.PrintMenu();
  374.                             MenuInput();
  375.                             break;
  376.                         }
  377.                     case 8:
  378.                         {
  379.                             // Создать список результатов студентов по тесту
  380.                             Queries.ResultOfTest(students);
  381.                             Console.WriteLine("");
  382.                             Console.Write("Вернуться в меню?");
  383.                             Console.ReadKey(true);
  384.                             Menu.PrintMenu();
  385.                             MenuInput();
  386.                             break;
  387.                         }
  388.                     case 9:
  389.                         {
  390.                             Environment.Exit(0);
  391.                             break;
  392.                         }
  393.                     default:
  394.                         {
  395.                             Console.ForegroundColor = ConsoleColor.Red;
  396.                             Console.WriteLine("");
  397.                             Console.WriteLine("                  Ошибка ввода. Нажмите 0, чтобы вывести список запросов.");
  398.                             Console.WriteLine("");
  399.                             MenuInput();
  400.                         }
  401.                         break;
  402.                 }
  403.             }
  404.  
  405.  
  406.             Menu.PrintMenu();
  407.             MenuInput();
  408.             Console.ReadKey(true);
  409.         }
  410.     }
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement