Перепиши данную программу так, чтобы Student был не типом Struct, а типом Class: using…
- User
Перепиши данную программу так, чтобы Student был не типом Struct, а типом Class: using System; using System.Collections.Generic; using System.Diagnostics;
enum Position { Senior, Junior, Preschooler }
struct Student : IComparable { public int Number; public int CountPhone; public int CountLunch; public Position Position;
public int CompareTo(Student other) { if (this.CountPhone != other.CountPhone) { return this.CountPhone.CompareTo(other.CountPhone); } else { return this.Position.CompareTo(other.Position); } }}
class School { List students;
public School() { students = new List<Student>(); } public void AddStudent(Student student) { students.Add(student); } public Tuple<Student, Student> FindMinMaxEmployee() { students.Sort(); Student minStudent = students[0]; Student maxStudent = students[students.Count - 1]; return Tuple.Create(minStudent, maxStudent); } public void Reward(ref Tuple<Student, Student> studentsTuple) { Student minStudent = studentsTuple.Item1; Student maxStudent = studentsTuple.Item2; minStudent.CountLunch--; maxStudent.CountLunch++; }}
class Program { static void Main(string[] args) { School school = new School();
Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 1; i <= 50000000; i++) { Student student = new Student(); student.Number = i; student.CountPhone = i % 10; student.CountLunch = i % 5; student.Position = (Position)(i % 3); school.AddStudent(student); } stopwatch.Stop(); Console.WriteLine("Время работы программы со структурами: " + stopwatch.ElapsedMilliseconds + " мс"); stopwatch.Reset(); stopwatch.Start(); for (int i = 1; i <= 50000000; i++) { Student student = new Student(); student.Number = i; student.CountPhone = i % 10; student.CountLunch = i % 5; student.Position = (Position)(i % 3); school.AddStudent(student); } stopwatch.Stop(); Console.WriteLine("Время работы программы с классами: " + stopwatch.ElapsedMilliseconds + " мс"); }}
- Assistant
using System; using System.Collections.Generic; using System.Diagnostics;
enum Position { Senior, Junior, Preschooler }
class Student : IComparable { public int Number; public int CountPhone; public int CountLunch; public Position Position;
public int CompareTo(Student other) { if (this.CountPhone != other.CountPhone) { return this.CountPhone.CompareTo(other.CountPhone); } else { return this.Position.CompareTo(other.Position); } }}
class School { List students;
public School() { students = new List<Student>(); } public void AddStudent(Student student) { students.Add(student); } public Tuple<Student, Student> FindMinMaxEmployee() { students.Sort(); Student minStudent = students[0]; Student maxStudent = students[students.Count - 1]; return Tuple.Create(minStudent, maxStudent); } public void Reward(ref Tuple<Student, Student> studentsTuple) { Student minStudent = studentsTuple.Item1; Student maxStudent = studentsTuple.Item2; minStudent.CountLunch--; maxStudent.CountLunch++; }}
class Program { static void Main(string[] args) { School school = new School();
Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 1; i <= 50000000; i++) { Student student = new Student(); student.Number = i; student.CountPhone = i % 10; student.CountLunch = i % 5; student.Position = (Position)(i % 3); school.AddStudent(student); } stopwatch.Stop(); Console.WriteLine("Время работы программы со структурами: " + stopwatch.ElapsedMilliseconds + " мс"); stopwatch.Reset(); stopwatch.Start(); for (int i = 1; i <= 50000000; i++) { Student student = new Student(); student.Number = i; student.CountPhone = i % 10; student.CountLunch = i % 5; student.Position = (Position)(i % 3); school.AddStudent(student); } stopwatch.Stop(); Console.WriteLine("Время работы программы с классами: " + stopwatch.ElapsedMilliseconds + " мс"); }}