Professional Documents
Culture Documents
ЛБ№3 - Єсипенко - Єгор - КН - 222В
ЛБ№3 - Єсипенко - Єгор - КН - 222В
ЗВІТ
по лабораторній роботі №3
з дисципліни «Об'єктно-орієнтоване програмування. Ознайомча
практика»
Виконав:
ст. гр. КН-
222в Єсипенко
Є. С.
Перевірив:
асист. Ковальчук Д.
Харків 2023
Тема: Узагальнене програмування.
Завдання:
1. Індивідуальне завдання
Розширити програму, яка була створена у попередній лабораторній роботі,
ієрархією сутностей відповідно до наведеної таблиці:
Варіант:
Program.cs
using CreativeTeamLib;
using System;
namespace BookshelfApp
{
class Program
{
static void Main(string[] args)
{
// Створюємо порожню полицю:
CreativeTeam<Country> bookshelf = new();
// Додаємо книжки
bookshelf += new Album<Country>("Nirvana", 1999,
new Country() {});
try
{
// Зберігаємо дані про книжки:
bookshelf.WriteAlbums("Bookshelf.xml");
CreativeTeamLib.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
namespace CreativeTeamLib
{
// Структура для опису
автора public class Country
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string NameOfCountry { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Title { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Year { get; set; }
public Country()
{
// Перевизначення еквівалентності
public override bool Equals(object? obj)
{
if (obj == null)
{
return false;
}
Country author = (Country)obj;
return author.NameOfCountry == NameOfCountry; // && author.Name
== Name;
}
// Книжка
public class Album<TCountry>
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Title { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public int Year { get; set; }
// public List<TCountry> Authors { get; set; }
// Конструктори
public Album(string title, int year, Country country)
{
Title = title;
Year =
year;
}
public Album()
{
// Перевизначення еквівалентності
public override bool Equals(object? obj)
{
if (obj is Album<TCountry> b)
{
if (b.Year.Equals(Year))
{
return false;
}
return b.Title == Title && b.Year == Year;
}
return false;
}
}
// Книжкова полиця
public class CreativeTeam<TCountry>
{
public List<Album<TCountry>> arraysOfSongs { get; set; }
// Конструктор
public CreativeTeam(params Album<TCountry>[] objects)
{
arraysOfSongs = new List<Album<TCountry>>(objects);
}
// Додавання книжки
public void Add(Album<TCountry> album)
{
arraysOfSongs.Add(album);
}
// Індексатор
public Album<TCountry> this[int index]
{
get { return arraysOfSongs[index]; }
set { arraysOfSongs[index] = value; }
}
Program.cs
using System;
using
System.IO;
using System.Text;
namespace Lab_3_2
{
class Program
{
static void Main(string[] args)
{
int length = 8;
Program.cs
using System.Xml.Serialization;
using System.IO;
using System;
namespace Lab3_3
{
public class Student
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Surname { get; set; }
public string[] Grades { get; set; }
}
class Test
{
static void Main(string[] args)
{
AcademicGroup academicGroup = new AcademicGroup()
{
student = new Student[]
{
new Student {
Name =
"Frodo",
Surname = "Baggins",
Grades = new string[] { "B", "D", "C" }
},
new Student {
Name = "Oleksandr",
Surname =
"Trufanov",
Grades = new string[] { "A", "A", "B+" }
},
new Student {
Name = "Maksim",
Surname = "Petrovicjev",
Grades = new string[] { "A+", "B", "B+" }
},
new Student {
Name = "Andrew",
Surname =
"Brown",
Grades = new string[] { "C", "A", "C" }
}
}
};
XmlSerializer deserializer =
new
XmlSerializer(typeof(AcademicGroup));
using (TextReader textReader = new StreamReader("XT.xml"))
{
academicGroup =
(AcademicGroup)deserializer.Deserialize(textReader);
}
}
}
}
Program.cs
using System;
namespace Lab3_4
{
public static class ArrayUtils
{
public static void SwapElements<TElem>(TElem[] arr, int iStart, int
iFinish, int jStart, int jFinish)
{
for (int i = 0; i <= iFinish - iStart; i++)
{
// Копіювання одного з елементів у тимчасову комірку:
TElem elem = arr[iStart];
arr[iStart] = arr[jStart];
arr[jStart] = elem;
iStart++;
jStart++;
}
}
public static void Insert<TElem>(ref TElem[] arr, ref TElem[] arr1, int
index)
{
// Зміна розміру масиву:
Array.Resize(ref arr, arr1.Length + arr.Length - 1);
public static void Insert2<TElem>(ref TElem[] arr, int index1, int index2,
TElem c)
{
for (int i = index1; i < index2; i++)
{
arr[i] = c;
}
}
class Program
{
static void Main(string[] args)
{
int[] a = { 112, 23, 32, 45, 34, 57, 70 };
int[] a1 = { 10, 20, 30, };
ArrayUtils.Insert(ref a, ref a1, 2);
ArrayUtils.Print(a);
ArrayUtils.SwapElements2(a, 3);
ArrayUtils.Print(a);
ArrayUtils.Insert2(ref a, 3, 5, 4);
ArrayUtils.Print(a);
ArrayUtils.SwapElements(a, 1, 3, 4, 6);
ArrayUtils.Print(a);
}
}
}
Рисунок 6 - Робота програми
Program.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace CustomContainer
{
public class CustomArray<T> : IEnumerable<T>
{
private T[] array; // Закрите поле для зберігання масиву елементів
public int From { get; private set; } // Властивість "From" для зберігання
початкового індексу
public int To { get; private set; } // Властивість "To" для зберігання
кінцевого індексу
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
class Program
{
static void Main(string[] args)
{
// Варіант на базі масиву
CustomArray<int> arrayContainer = new CustomArray<int>(0, 4); //
Створення контейнера з індексами від 0 до 4
arrayContainer[0] = 10; // Присвоєння значення елементу за індексом
arrayContainer[3] = 20;
Console.WriteLine($"Elements in arrayContainer:
{(string)arrayContainer}"); // Виведення елементів контейнера
listContainer[0] = "orange";
Console.WriteLine($"Elements in listContainer after modifying:
{(string)listContainer}");
listContainer.Add("grape");
Console.WriteLine($"Elements in listContainer after adding:
{(string)listContainer}");
listContainer.RemoveLast();
Console.WriteLine($"Elements in listContainer after removing:
{(string)listContainer}");
6. Робота з множиною
Увести кількість елементів майбутньої множини цілих чисел та діапазон
чисел. Сформувати цю множину з випадкових значень. Вивести елементи
множини, відсортовані за збільшенням.
Program.cs
using System;
using System.Collections.Generic;
namespace Lab3_6
{
class Program
{
static void Main(string[] args)
{
SortedSet<int> set = new SortedSet<int>();
Program.cs
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.Write("Введіть речення: ");
string sentence = Console.ReadLine();