
ЛР1 / Лаб. 1 C# (Вариант 1)
.docxusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Person
{
string name;
string surname;
DateTime dateOfBirth;
public Person(string name, string surname, DateTime dateOfBirth)
{
this.name = name;
this.surname = surname;
this.dateOfBirth = dateOfBirth;
}
public Person()
{
name = "Name";
surname = "Surname";
dateOfBirth = DateTime.Today;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Surame
{
get
{
return surname;
}
set
{
surname = value;
}
}
public DateTime DateOfBirth
{
get
{
return dateOfBirth;
}
set
{
dateOfBirth = value;
}
}
public int YearOfBirth
{
get
{
return dateOfBirth.Year;
}
set
{
dateOfBirth = new DateTime(value, dateOfBirth.Month, dateOfBirth.Day);
}
}
public override string ToString()
{
return $"Фамилия, имя: {surname} {name},\nДата рождения: {dateOfBirth.ToShortDateString()}";
}
public virtual string ToShortString()
{
return $"Фамилия, имя: {surname} {name}";
}
}
//набор именнованных целочисленных констант
enum Education
{
Specialist,
Bachelor,
SecondEducation
}
class Exam
{
public string subject { get; set; }
public int grade { get; set; }
public DateTime examDate { get; set; }
public Exam(string subject, int grade, DateTime examDate)
{
this.subject = subject;
this.grade = grade;
this.examDate = examDate;
}
public Exam()
{
subject = "Subject";
grade = 0;
examDate = DateTime.Today;
}
public override string ToString()
{
return $"Предмет: {subject}, Оценка: {grade}, Дата экзамена: {examDate.ToShortDateString()}";
}
}
class Student
{
Person studentData;
Education formOfEducation;
int groupNumber;
Exam[] exams;
public Student(Person studentData, Education formOfEducation, int groupNumber)
{
this.studentData = studentData;
this.formOfEducation = formOfEducation;
this.groupNumber = groupNumber;
exams = new Exam[0];
}
public Student()
{
studentData = new Person();
formOfEducation = Education.Bachelor;
groupNumber = 0;
exams = new Exam[0];
}
public Person StudentData
{
get
{
return studentData;
}
set
{
studentData = value;
}
}
public Education FormOfEducation
{
get
{
return formOfEducation;
}
set
{
formOfEducation = value;
}
}
public int GroupNumber
{
get
{
return groupNumber;
}
set
{
groupNumber = value;
}
}
public Exam[] Exams
{
get
{
return exams;
}
set
{
exams = value;
}
}
public double AverageGrade
{
get
{
if (exams.Length == 0)
{
return 0;
}
double sum = 0;
foreach (Exam exam in exams)
{
sum += exam.grade;
}
return sum / exams.Length;
}
}
public bool this[Education index]
{
get
{
return formOfEducation == index;
}
}
public void AddExams(params Exam[] newExams)
{
int length = exams.Length;
Array.Resize(ref exams, exams.Length + newExams.Length); //изменяет размер одномерного массива(подлежащий измнению массив, новый размер)
Array.Copy(newExams, 0, exams, length, newExams.Length); //копирует элементы из одного массива в другой(массив из которого копируют, копирование с индекса, копирует в этот массив, начиная с этого индекса, количество копируемых элементов)
}
public override string ToString()
{
//string examsString = string.Concat(exams);
//for (int i=0; i<exams.Length; i++)
//string s = new string(exams);
//return $"Студент:\n{studentData},\nОбразование: {formOfEducation},\nНомер группы: {groupNumber},\nЭкзамены: {exams}\n";
Console.WriteLine($"Студент:\n{studentData},\nОбразование: {formOfEducation},\nНомер группы: {groupNumber},\nЭкзамены: ");
for (int i = 0; i < exams.Length; i++)
{
Console.WriteLine(exams[i]);
}
return "\n";
}
public virtual string ToShortString()
{
return $"Студент:\n{studentData},\nОбразование: {formOfEducation},\nНомер группы: {groupNumber},\nСредний балл: {AverageGrade}\n";
}
}
namespace LAB_1_C_SH
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---№1---");
Person human = new Person("Mark", "Astanaev", new DateTime(2004, 8, 5));
Education education = Education.Bachelor;
int groupNumber = 24;
Student student = new Student(human, education, groupNumber);
Console.WriteLine(student.ToShortString());
Console.WriteLine("---№2---");
Console.WriteLine("Значение индексатора:");
Console.WriteLine($"Education.Specialist: {student[Education.Specialist]}");
Console.WriteLine($"Education.Bachelor: {student[Education.Bachelor]}");
Console.WriteLine($"Education.SecondEducation: {student[Education.SecondEducation]}");
Console.WriteLine("---№3---");
student.StudentData.Name = "Peter";
student.StudentData.Surame = "Parker";
student.StudentData.DateOfBirth = new DateTime(2000, 3, 22);
student.FormOfEducation = Education.Specialist;
student.GroupNumber = 52;
Exam []exam = new Exam[2];
exam[0] = new Exam("C#", 4, new DateTime(2023, 12, 12));
exam[1] = new Exam("Физика", 5, new DateTime(2022, 7, 4));
student.Exams = exam;
Console.WriteLine(student.ToString());
Console.WriteLine("---№4---");
Exam examOne = new Exam("Алгем", 3, new DateTime(2021, 6, 29));
Exam examTwo = new Exam("Математика", 5, new DateTime(2024, 1, 14));
student.AddExams(examOne, examTwo);
Console.WriteLine(student.ToString());
Console.WriteLine(student.ToShortString());
Console.WriteLine("---№5---");
Console.WriteLine("Введите число строк и столбцов для массива(разделители: ' ', '.', ','): ");
string mass = Console.ReadLine();
string[] massList = mass.Split(' ', '.', ',');
int row = Convert.ToInt32(massList[0]);
int colomn = Convert.ToInt32(massList[1]);
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
Exam[] lineMass = new Exam[row * colomn];
watch.Start();
for (int i = 0; i<lineMass.Length; i++)
{
lineMass[i] = new Exam();
}
watch.Stop();
Console.WriteLine("Одномерный: " + watch.Elapsed);
/*
for (int i = 0; i < lineMass.Length; i++)
{
Console.WriteLine(lineMass[i]);
}
Console.WriteLine(lineMass);
*/
Exam[,] rectMass = new Exam[row, colomn];
watch.Restart();
for (int i = 0; i < row; i++)
{
for (int j = 0; j < colomn; j++)
{
rectMass[i, j] = new Exam();
}
}
watch.Stop();
Console.WriteLine("Двумерный прямоугольный: " + watch.Elapsed);
int k = 0;
int rowStep = 0;
int kStep = 1;
watch.Restart();
while (k < row * colomn)
{
k += kStep;
kStep++;
rowStep++;
}
k = 0;
Exam[][] stepMass = new Exam[rowStep][];
for (int i = 0; i < row * colomn; i++)
{
if (k < row * colomn - i)
{
stepMass[i] = new Exam[i + 1];
k += i + 1;
}
else
{
stepMass[i] = new Exam[row *colomn - k];
break;
}
if (k == row * colomn) break;
}
for (int i = 0; i < rowStep; i++)
{
for (int j = 0; j < stepMass[i].Length; j++)
{
stepMass[i][j] = new Exam();
}
}
watch.Stop();
Console.WriteLine("Двумерный ступенчатый: " + watch.Elapsed);
Console.ReadKey();
}
}
}