Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Kuraxin_LINQ
- {
- class Student
- {
- public string Name { get; set; }
- public string SurName { get; set; }
- public string TestResult { get; set; }
- }
- class Menu
- {
- public static void PrintMenu()
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- Console.WriteLine("");
- Console.Write("--------------------------------------------------------------------------------");
- Console.WriteLine(" МЕНЮ ");
- Console.Write("--------------------------------------------------------------------------------");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 1. ");
- Console.ResetColor();
- Console.WriteLine("Вывести список всех студентов");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 2. ");
- Console.ResetColor();
- Console.WriteLine("Вывести список студентов, которые сдали тест на 5");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 3. ");
- Console.ResetColor();
- Console.WriteLine("Вывести рейтинг студентов, начиная с отличников");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 4. ");
- Console.ResetColor();
- Console.WriteLine("Определить кого больше: отличников или двоечников");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 5. ");
- Console.ResetColor();
- Console.WriteLine("Разбить студентов на группы по первой букве");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 6. ");
- Console.ResetColor();
- Console.WriteLine("Найти разность между max и min баллом");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 7. ");
- Console.ResetColor();
- Console.WriteLine("Добавить в список вторую попытку сдачи теста");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 8. ");
- Console.ResetColor();
- Console.WriteLine("Создать список результатов студентов по тесту");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" 9. ");
- Console.ResetColor();
- Console.WriteLine("Выход");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("");
- }
- }
- class Queries
- {
- static public void ListOfStudents(List<Student> students)
- {
- Console.Clear();
- Console.WriteLine(" СПИСОК ВСЕХ СТУДЕНТОВ");
- Console.WriteLine("");
- Console.ResetColor();
- Console.WriteLine(" Фамилия | Имя | Баллы");
- Console.WriteLine("--------------------------------------------------------");
- var AllStudents = from a in students select a; // Выбрать все
- foreach (Student stud in AllStudents)
- {
- Console.WriteLine(" {0,-15} | {1, -15} | {2, -3}", stud.SurName, stud.Name, stud.TestResult);
- }
- }
- static public void ListOfBestStudents(List<Student> students) // Вывести список студентов, которые сдали тест на 5
- {
- //IEnumerable<Student> BestStudents = from a in students select a;
- Console.Clear();
- Console.WriteLine(" СПИСОК СТУДЕНТОВ ОТЛИЧНИКОВ");
- Console.WriteLine("");
- Console.ResetColor();
- Console.WriteLine(" Фамилия | Имя | Баллы");
- Console.WriteLine("--------------------------------------------------------");
- var BestStudents = from a in students where Convert.ToInt32(a.TestResult) >= 86 select a;
- foreach (Student stud in BestStudents)
- {
- Console.WriteLine(" {0,-15} | {1, -15} | {2, -3}", stud.SurName, stud.Name, stud.TestResult);
- }
- }
- static public void TopList(List<Student> students) // Вывести рейтинг студентов, начиная с отличников
- {
- Console.Clear();
- Console.WriteLine(" РЕЙТИНГ СТУДЕНТОВ");
- Console.WriteLine("");
- Console.ResetColor();
- Console.WriteLine(" Фамилия | Имя | Баллы");
- Console.WriteLine("--------------------------------------------------------");
- var TopOfStudents = from a in students orderby a.TestResult descending select a;
- foreach (Student stud in TopOfStudents)
- {
- Console.WriteLine(" {0,-15} | {1, -15} | {2, -3}", stud.SurName, stud.Name, stud.TestResult);
- }
- }
- static public void DifferenceOfBest(List<Student> students) // Определить кого больше: отличников или двоечников
- {
- int BestStudents = (from a in students where Convert.ToInt32(a.TestResult) >= 86 select a).Count();
- int BadStudents = (from a in students where Convert.ToInt32(a.TestResult) <= 50 select a).Count();
- Console.Clear();
- Console.WriteLine("Количество отличников: {0} ", BestStudents);
- Console.WriteLine("Количество двоечников: {0} ", BadStudents);
- if (BestStudents > BadStudents)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Отличников больше");
- Console.ResetColor();
- }
- if (BestStudents < BadStudents)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Двоечников больше");
- Console.ResetColor();
- }
- if (BestStudents == BadStudents)
- {
- Console.WriteLine("Равное количество отличников и двоечников");
- }
- }
- static public void GroupByLetters(List<Student> students) // Разбить студентов на группы по первой букве
- {
- Console.Clear();
- Console.WriteLine(" СПИСОК ВСЕХ СТУДЕНТОВ ПО АЛФАВИТУ");
- Console.WriteLine("");
- Console.ResetColor();
- Console.WriteLine(" Фамилия | Имя | Баллы");
- var groups = from student in students group student by student.SurName[0] into letter orderby letter.Key select letter;
- foreach (var letter in groups)
- {
- Console.WriteLine("-{0}-------------------------------------------------------", letter.Key);
- foreach (var stud in letter)
- {
- Console.WriteLine(" {0,-15} | {1, -15} | {2, -3}", stud.SurName, stud.Name, stud.TestResult);
- }
- }
- }
- static public void ResultDifference(List<Student> students) // Найти разность между max и min баллом
- {
- int MaxResult = students.Max(point => Convert.ToInt32(point.TestResult));
- int MinResult = students.Min(point => Convert.ToInt32(point.TestResult));
- Console.Clear();
- Console.WriteLine("Максимальный балл: {0} ", MaxResult);
- Console.WriteLine("Минимальный балл: {0} ", MinResult);
- Console.Write("");
- Console.WriteLine("Разность: {0} ", MaxResult - MinResult);
- }
- static public void NewResult(List<Student> students, int index) // Добавить в список вторую попытку сдачи теста
- {
- Console.Write("Введите баллы студента за вторую попытку: ");
- string NewTestResult = Console.ReadLine();
- students.Add(new Student { Name = students[index - 1].Name, SurName = students[index - 1].SurName, TestResult = NewTestResult });
- }
- static public void ResultOfTest(List<Student> students) // Создать список результатов студентов по тесту
- {
- Console.Clear();
- Console.WriteLine(" СПИСОК РЕЗУЛЬТАТОВ ПО ТЕСТУ");
- Console.WriteLine("");
- Console.ResetColor();
- Console.WriteLine(" Фамилия | Имя | Оценка");
- Console.WriteLine("--------------------------------------------------------");
- List<Student> NewList = new List<Student> { };
- var groups = from surg in students group surg by surg.SurName;
- foreach (var gr in groups)
- {
- foreach (Student stud in gr)
- {
- if (stud.TestResult == gr.Max(tr => tr.TestResult))
- {
- Console.WriteLine(" {0,-15} | {1, -15} | {2, -2}", stud.SurName, stud.Name, stud.TestResult);
- }
- }
- }
- }
- static public void NumberListOfStudents(List<Student> students)
- {
- Console.Clear();
- Console.WriteLine(" СПИСОК ВСЕХ СТУДЕНТОВ");
- Console.WriteLine("");
- Console.ResetColor();
- Console.WriteLine(" № | Фамилия | Имя | Баллы");
- Console.WriteLine("-------------------------------------------------------------------");
- var AllStudents = from a in students select a; // Выбрать все
- foreach (Student stud in AllStudents)
- {
- Console.WriteLine(" {0,-3} | {1,-15} | {2, -15} | {3, -3}", students.IndexOf(stud) + 1, stud.SurName, stud.Name, stud.TestResult);
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- int choose;
- List<Student> students = new List<Student>() {
- new Student {Name = "Андрей", SurName = "Иванов", TestResult = "96"},
- new Student {Name = "Вера", SurName = "Котова", TestResult = "90"},
- new Student {Name = "Виталий", SurName = "Собакин", TestResult = "85"},
- new Student {Name = "Зинаида", SurName = "Герасимова", TestResult = "81"},
- new Student {Name = "Алексей", SurName = "Навальный", TestResult = "44"},
- new Student {Name = "Пётр", SurName = "Лужин", TestResult = "80"},
- new Student {Name = "Анна", SurName = "Каренина", TestResult = "72"},
- new Student {Name = "Михаил", SurName = "Сидоров", TestResult = "72"},
- new Student {Name = "Виктор", SurName = "Петров", TestResult = "27"},
- new Student {Name = "Екатерина", SurName = "Романова", TestResult = "92"},
- new Student {Name = "Аркадий", SurName = "Попов", TestResult = "57"},
- new Student {Name = "Алексей", SurName = "Патрушев", TestResult = "7"},
- new Student {Name = "Елизавета", SurName = "Алиева", TestResult = "64"},
- new Student {Name = "Анна", SurName = "Веселова", TestResult = "53"},
- new Student {Name = "Дмитрий", SurName = "Бариев", TestResult = "47"},
- new Student {Name = "Иван", SurName = "Карамазов", TestResult = "98"}
- };
- void MenuInput()
- {
- Console.ResetColor();
- try
- {
- Console.Write(" Введите номер запроса: ");
- choose = Convert.ToInt32(Console.ReadLine());
- }
- catch (FormatException)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("");
- Console.WriteLine(" Ошибка ввода. Нажмите 0, чтобы вывести список запросов.");
- Console.WriteLine("");
- MenuInput();
- }
- switch (choose)
- {
- case 0:
- {
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 1:
- {
- // Вывести список всех студентов
- Queries.ListOfStudents(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 2:
- {
- // Вывести список студентов, которые сдали тест на 5
- Queries.ListOfBestStudents(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 3:
- {
- // Вывести рейтинг студентов, начиная с отличников
- Queries.TopList(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 4:
- {
- // Определить кого больше: отличников или двоечников
- Queries.DifferenceOfBest(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 5:
- {
- // Разбить студентов на группы по первой букве
- Queries.GroupByLetters(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 6:
- {
- // Найти разность между max и min баллом
- Queries.ResultDifference(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 7:
- {
- // Добавить в список вторую попытку сдачи теста
- Queries.NumberListOfStudents(students);
- Console.WriteLine("");
- Console.Write("Выберите номер студента: ");
- Queries.NewResult(students, Convert.ToInt32(Console.ReadLine()));
- Console.Write("Вывести новый список?");
- Console.WriteLine("");
- Console.ReadKey(true);
- Queries.ListOfStudents(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 8:
- {
- // Создать список результатов студентов по тесту
- Queries.ResultOfTest(students);
- Console.WriteLine("");
- Console.Write("Вернуться в меню?");
- Console.ReadKey(true);
- Menu.PrintMenu();
- MenuInput();
- break;
- }
- case 9:
- {
- Environment.Exit(0);
- break;
- }
- default:
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("");
- Console.WriteLine(" Ошибка ввода. Нажмите 0, чтобы вывести список запросов.");
- Console.WriteLine("");
- MenuInput();
- }
- break;
- }
- }
- Menu.PrintMenu();
- MenuInput();
- Console.ReadKey(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement