Advertisement
wikail

Student

Dec 2nd, 2020
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. //Написать программу ,которая хранит и рассчитывает сред. арифметическую оценку студента.
  6. //Реализовать поиск студентов по этому среднему баллу .
  7. //Выберите корректные структуры данных и типы для решения задачи.
  8.  
  9. namespace Student
  10. {
  11.     class Program
  12.     {
  13.         public struct Student
  14.         {
  15.             string Name;
  16.             string Surname;
  17.             public List<int> Balls;
  18.            
  19.  
  20.             public Student(string name, string surnamr)
  21.             {
  22.                 Name = name;
  23.                 Surname = surnamr;
  24.                 Balls = new List<int>();
  25.             }
  26.  
  27.             public double GetMidBall()
  28.             {
  29.                 double balls = 0;
  30.  
  31.                 if (Balls.Count == 0) throw new Exception("Нет оценок");
  32.  
  33.                 foreach (int ball in Balls)
  34.                 {
  35.                     balls += ball;
  36.                 }
  37.                 return balls/Balls.Count;
  38.             }
  39.  
  40.             static void Main(string[] args)
  41.             {
  42.  
  43.                 List<Student> students = new List<Student>();
  44.                
  45.                 Student Vasiljev = new Student("Василий", "Васильев");
  46.                 Vasiljev.Balls.AddRange(new int[] { 3, 4, 4, 5, 5});
  47.  
  48.                 Student Ivanov = new Student("Иван", "Иванов");
  49.                 Ivanov.Balls.AddRange(new int[] { 3, 4, 4, 3, 5 });
  50.  
  51.                 Student Petrov = new Student("Петр", "Петров");
  52.                 Petrov.Balls.AddRange(new int[] { 5, 5, 4, 5, 5 });
  53.  
  54.                 students.Add(Vasiljev);
  55.                 students.Add(Ivanov);
  56.                 students.Add(Petrov);
  57.  
  58.                 Console.WriteLine("Средний бал Васильева: " + Vasiljev.GetMidBall());
  59.                 Console.WriteLine("Средний бал Иванова: " + Ivanov.GetMidBall());
  60.                 Console.WriteLine("Средний бал Петрова: " + Petrov.GetMidBall());
  61.  
  62.                 double rightBall = 4.8;
  63.  
  64.                 Console.WriteLine("Cреднй балл " + rightBall + " у студента: " + Search (students,rightBall).Surname );
  65.  
  66.             }
  67.            
  68.             //Ищем студента с указанным баллом
  69.             public static Student Search (List<Student> students, double rightBall)
  70.             {
  71.                 for (int i = 0; i < students.Count; i++)
  72.                 {
  73.                     if (students[i].GetMidBall() == rightBall)
  74.                         return students[i];
  75.                 }
  76.                
  77.                 return new Student(null, "студента с таким баллом нет");
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement