diff --git a/Tank/Tank/DirectionType.cs b/Tank/Tank/DirectionType.cs new file mode 100644 index 0000000..8923629 --- /dev/null +++ b/Tank/Tank/DirectionType.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank; + +public enum DirectionType +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} diff --git a/Tank/Tank/DrawningTank.cs b/Tank/Tank/DrawningTank.cs new file mode 100644 index 0000000..48820a5 --- /dev/null +++ b/Tank/Tank/DrawningTank.cs @@ -0,0 +1,193 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank; + +public class DrawningTank +{ + /// + /// Класс-сущность + /// + public EntityTank? EntityTank { get; private set; } + /// + /// Ширина окна + /// + private int? _pictureWidth; + /// + /// Высота окна + /// + private int? _pictureHeight; + /// + /// Левая координата прорисовки автомобиля + /// + private int? _startPosX; + /// + /// Верхняя кооридната прорисовки автомобиля + /// + private int? _startPosY; + /// + /// Ширина прорисовки автомобиля + /// + private readonly int _drawningTankWidth = 218; + /// + /// Высота прорисовки автомобиля + /// + private readonly int _drawningTankHeight = 105; + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pushka, bool pulemet) + { + EntityTank = new EntityTank(); + EntityTank.Init(speed, weight, bodyColor, additionalColor, pushka, pulemet); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + /// + /// Установка границ поля + /// + /// Ширина поля + /// Высота поля + /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах + /// + /// Установка границ поля + /// + /// Ширина поля + /// Высота поля + /// true - границы заданы, false - проверка не пройдена, нельзя + public bool SetPictureSize(int width, int height) + { + if (_drawningTankWidth <= width && _drawningTankHeight <= height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX.HasValue && _startPosY.HasValue) + { + if (_startPosX + _drawningTankWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningTankWidth; + } + + if (_startPosY + _drawningTankHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningTankHeight; + } + } + return true; + } + return false; + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + if (x < 0) x = 0; + else if (x + _drawningTankWidth > _pictureWidth) x = _pictureWidth.Value - _drawningTankWidth; + + if (y < 0) y = 0; + else if (y + _drawningTankHeight > _pictureHeight) y = _pictureHeight.Value - _drawningTankHeight; + _startPosX = x; + _startPosY = y; + } + /// + /// Изменение направления перемещения + /// + /// Направление + /// true - перемещене выполнено, false - перемещение невозможно + public bool MoveTransport(DirectionType direction) + { + if (EntityTank == null || !_startPosX.HasValue || + !_startPosY.HasValue) + { + return false; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX.Value - EntityTank.Step > 0) + _startPosX -= (int)EntityTank.Step; + return true; + //вверх + case DirectionType.Up: + if (_startPosY.Value - EntityTank.Step > 0) + _startPosY -= (int)EntityTank.Step; + return true; + // вправо + case DirectionType.Right: + if (_startPosX.Value + _drawningTankWidth + EntityTank.Step < _pictureWidth) + _startPosX += (int)EntityTank.Step; + return true; + //вниз + case DirectionType.Down: + if (_startPosY.Value + _drawningTankHeight + EntityTank.Step < _pictureHeight) + _startPosY += (int)EntityTank.Step; + return true; + default: + return false; + } + } + /// + /// Прорисовка объекта + /// + /// + public void DrawTransport(Graphics g) + { + if (EntityTank == null || !_startPosX.HasValue || + !_startPosY.HasValue) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(EntityTank.AdditionalColor); + Brush bodyBrush = new SolidBrush(EntityTank.BodyColor); + + g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 45, 150, 60); + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 60, 33, 33); + g.DrawEllipse(pen, _startPosX.Value + 110, _startPosY.Value + 60, 33, 33); + + g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 80, 16, 16); + g.DrawEllipse(pen, _startPosX.Value + 55, _startPosY.Value + 83, 16, 16); + g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 83, 16, 16); + g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 80, 16, 16); + + g.FillEllipse(bodyBrush, _startPosX.Value + 45, _startPosY.Value + 55, 15, 15); + g.FillEllipse(bodyBrush, _startPosX.Value + 65, _startPosY.Value + 55, 15, 15); + g.FillEllipse(bodyBrush, _startPosX.Value + 85, _startPosY.Value + 55, 15, 15); + + g.FillRectangle(bodyBrush, _startPosX.Value + 30, _startPosY.Value + 10, 100, 30); + g.FillRectangle(bodyBrush, _startPosX.Value + 5, _startPosY.Value + 40, 140, 25); + + if (EntityTank.Pushka) + g.FillRectangle(additionalBrush, _startPosX.Value + 120, _startPosY.Value + 20, 100, 15); + + if (EntityTank.Pulemet) + { + Point p = new Point(_startPosX.Value + 75, _startPosY.Value + 10); + Point p1 = new Point(_startPosX.Value + 80, _startPosY.Value + 1); + Point p2 = new Point(_startPosX.Value + 87, _startPosY.Value + 2); + Point p3 = new Point(_startPosX.Value + 80, _startPosY.Value + 10); + Point[] p_pulemet = { p, p1, p2, p3 }; + g.FillPolygon(additionalBrush, p_pulemet); + } + } +} diff --git a/Tank/Tank/EntityTank.cs b/Tank/Tank/EntityTank.cs new file mode 100644 index 0000000..b8303da --- /dev/null +++ b/Tank/Tank/EntityTank.cs @@ -0,0 +1,54 @@ +namespace Tank; + +public class EntityTank +{ + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public double Weight { get; private set; } + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + /// + /// Дополнительный цвет (для опциональных элементов) + /// + public Color AdditionalColor { get; private set; } + /// + /// Признак (опция) наличия обвеса + /// + public bool Pushka { get; private set; } + /// + /// Признак (опция) наличия антикрыла + /// + public bool Pulemet { get; private set; } + public bool Gusenica { get; private set; } + /// + /// Шаг перемещения автомобиля + /// + public double Step => Speed * 100 / Weight; + /// + /// Инициализация полей объекта-класса спортивного автомобиля + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + /// Признак наличия гоночной полосы + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pushka, bool pulemet) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Pushka = pushka; + Pulemet = pulemet; + + } +} \ No newline at end of file diff --git a/Tank/Tank/FormTank.Designer.cs b/Tank/Tank/FormTank.Designer.cs index 5a22010..ef5db72 100644 --- a/Tank/Tank/FormTank.Designer.cs +++ b/Tank/Tank/FormTank.Designer.cs @@ -3,12 +3,12 @@ partial class FormTank { /// - /// Required designer variable. + /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// - /// Clean up any resources being used. + /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) @@ -23,17 +23,115 @@ #region Windows Form Designer generated code /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + pictureBoxTank = new PictureBox(); + buttonCreate = new Button(); + buttonLeft = new Button(); + buttonUp = new Button(); + buttonRight = new Button(); + buttonDown = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit(); + SuspendLayout(); + // + // pictureBoxTank + // + pictureBoxTank.AccessibleRole = AccessibleRole.None; + pictureBoxTank.Dock = DockStyle.Fill; + pictureBoxTank.Location = new Point(0, 0); + pictureBoxTank.Name = "pictureBoxTank"; + pictureBoxTank.Size = new Size(1235, 854); + pictureBoxTank.SizeMode = PictureBoxSizeMode.AutoSize; + pictureBoxTank.TabIndex = 1; + pictureBoxTank.TabStop = false; + pictureBoxTank.Click += ButtonMove_Click; + // + // buttonCreate + // + buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreate.Location = new Point(12, 796); + buttonCreate.Name = "buttonCreate"; + buttonCreate.Size = new Size(150, 46); + buttonCreate.TabIndex = 2; + buttonCreate.Text = "Создать"; + buttonCreate.UseVisualStyleBackColor = true; + buttonCreate.Click += ButtonCreateTank_Click; + // + // buttonLeft + // + buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonLeft.BackgroundImage = Properties.Resources.left; + buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; + buttonLeft.Location = new Point(1021, 786); + buttonLeft.Name = "buttonLeft"; + buttonLeft.Size = new Size(45, 45); + buttonLeft.TabIndex = 3; + buttonLeft.UseVisualStyleBackColor = true; + buttonLeft.Click += ButtonMove_Click; + // + // buttonUp + // + buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonUp.BackgroundImage = Properties.Resources.up; + buttonUp.BackgroundImageLayout = ImageLayout.Stretch; + buttonUp.Location = new Point(1072, 735); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new Size(45, 45); + buttonUp.TabIndex = 4; + buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += ButtonMove_Click; + // + // buttonRight + // + buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonRight.BackgroundImage = Properties.Resources.right; + buttonRight.BackgroundImageLayout = ImageLayout.Stretch; + buttonRight.Location = new Point(1123, 786); + buttonRight.Name = "buttonRight"; + buttonRight.Size = new Size(45, 45); + buttonRight.TabIndex = 5; + buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += ButtonMove_Click; + // + // buttonDown + // + buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonDown.BackgroundImage = Properties.Resources.down; + buttonDown.BackgroundImageLayout = ImageLayout.Stretch; + buttonDown.Location = new Point(1072, 786); + buttonDown.Name = "buttonDown"; + buttonDown.Size = new Size(45, 45); + buttonDown.TabIndex = 6; + buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += ButtonMove_Click; + // + // FormTank + // + AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1235, 854); + Controls.Add(buttonDown); + Controls.Add(buttonRight); + Controls.Add(buttonUp); + Controls.Add(buttonLeft); + Controls.Add(buttonCreate); + Controls.Add(pictureBoxTank); + Name = "FormTank"; + Text = "Танк"; + ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); + ResumeLayout(false); + PerformLayout(); } #endregion + private PictureBox pictureBoxTank; + private Button buttonCreate; + private Button buttonLeft; + private Button buttonUp; + private Button buttonRight; + private Button buttonDown; } } \ No newline at end of file diff --git a/Tank/Tank/FormTank.cs b/Tank/Tank/FormTank.cs index 19f5b27..46566fd 100644 --- a/Tank/Tank/FormTank.cs +++ b/Tank/Tank/FormTank.cs @@ -1,10 +1,102 @@ -namespace Tank +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Tank; + +public partial class FormTank : Form { - public partial class FormTank : Form + /// + /// Поле-объект для прорисовки объекта + /// + private DrawningTank? _drawningTank; + /// + /// Конструктор формы + /// + public FormTank() { - public FormTank() + InitializeComponent(); + } + /// + /// Метод прорисовки машины + /// + private void Draw() + { + if (_drawningTank == null) { - InitializeComponent(); + return; + } + Bitmap bmp = new(pictureBoxTank.Width, + pictureBoxTank.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningTank.DrawTransport(gr); + pictureBoxTank.Image = bmp; + } + /// + /// Обработка нажатия кнопки "Создать" + /// + /// + /// + private void ButtonCreateTank_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningTank = new DrawningTank(); + + _drawningTank.Init(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2))); + _drawningTank.SetPictureSize(pictureBoxTank.Width, + pictureBoxTank.Height); + _drawningTank.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + + /// + /// Перемещение объекта по форме (нажатие кнопок навигации) + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningTank == null) + { + return; + } + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonUp": + result = + _drawningTank.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = + _drawningTank.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + result = + _drawningTank.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + result = + _drawningTank.MoveTransport(DirectionType.Right); + break; + } + if (result) + { + Draw(); } } } \ No newline at end of file diff --git a/Tank/Tank/FormTank.resx b/Tank/Tank/FormTank.resx index 1af7de1..af32865 100644 --- a/Tank/Tank/FormTank.resx +++ b/Tank/Tank/FormTank.resx @@ -1,17 +1,17 @@  - diff --git a/Tank/Tank/Properties/Resources.Designer.cs b/Tank/Tank/Properties/Resources.Designer.cs new file mode 100644 index 0000000..adfd17a --- /dev/null +++ b/Tank/Tank/Properties/Resources.Designer.cs @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// Этот код создан программой. +// Исполняемая версия:4.0.30319.42000 +// +// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +// повторной генерации кода. +// +//------------------------------------------------------------------------------ + +namespace Tank.Properties { + using System; + + + /// + /// Класс ресурса со строгой типизацией для поиска локализованных строк и т.д. + /// + // Этот класс создан автоматически классом StronglyTypedResourceBuilder + // с помощью такого средства, как ResGen или Visual Studio. + // Чтобы добавить или удалить член, измените файл .ResX и снова запустите ResGen + // с параметром /str или перестройте свой проект VS. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Возвращает кэшированный экземпляр ResourceManager, использованный этим классом. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Tank.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Перезаписывает свойство CurrentUICulture текущего потока для всех + /// обращений к ресурсу с помощью этого класса ресурса со строгой типизацией. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Поиск локализованного ресурса типа System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap down { + get { + object obj = ResourceManager.GetObject("down", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// Поиск локализованного ресурса типа System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap left { + get { + object obj = ResourceManager.GetObject("left", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// Поиск локализованного ресурса типа System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap right { + get { + object obj = ResourceManager.GetObject("right", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// Поиск локализованного ресурса типа System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap up { + get { + object obj = ResourceManager.GetObject("up", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + } +} diff --git a/Tank/Tank/Properties/Resources.resx b/Tank/Tank/Properties/Resources.resx new file mode 100644 index 0000000..02616cd --- /dev/null +++ b/Tank/Tank/Properties/Resources.resx @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\Resources\left.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\down.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\right.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\up.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + \ No newline at end of file diff --git a/Tank/Tank/Resources/down.png b/Tank/Tank/Resources/down.png new file mode 100644 index 0000000..2a4910d Binary files /dev/null and b/Tank/Tank/Resources/down.png differ diff --git a/Tank/Tank/Resources/left.png b/Tank/Tank/Resources/left.png new file mode 100644 index 0000000..5489fb2 Binary files /dev/null and b/Tank/Tank/Resources/left.png differ diff --git a/Tank/Tank/Resources/right.png b/Tank/Tank/Resources/right.png new file mode 100644 index 0000000..980696d Binary files /dev/null and b/Tank/Tank/Resources/right.png differ diff --git a/Tank/Tank/Resources/up.png b/Tank/Tank/Resources/up.png new file mode 100644 index 0000000..b1f4345 Binary files /dev/null and b/Tank/Tank/Resources/up.png differ diff --git a/Tank/Tank/Tank.csproj b/Tank/Tank/Tank.csproj index b57c89e..13ee123 100644 --- a/Tank/Tank/Tank.csproj +++ b/Tank/Tank/Tank.csproj @@ -8,4 +8,19 @@ enable + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + \ No newline at end of file diff --git a/labalaba2/lab2/SourceZhadny.cpp b/labalaba2/lab2/SourceZhadny.cpp new file mode 100644 index 0000000..cf22f5b --- /dev/null +++ b/labalaba2/lab2/SourceZhadny.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +class Item { +private: + std::string name; + int weight; + int price; +public: + + int GetItemWeight() { + return weight; + } + int GetItemPrice() { + return price; + } + double GetPriceOfWeight() { + return price / (double)weight; + } + void PrintParametres() { + std::cout << "\tName item: " << name << "\n\t\tWeight: " << weight << "\n\t\tPrice:" << price << std::endl; + } + void SetParametresOfItem() { + std::cout << "Enter name of item: "; + std::cin >> name; + std::cout << std::endl; + std::cout << "Enter weight of item: "; + std::cin >> weight; + std::cout << std::endl; + std::cout << "Enter price of item: "; + std::cin >> price; + std::cout << std::endl; + } +}; + +class ItemsHeap { +private: + bool sorted = false; + std::vector itemsHeap; + void m_swap(int i_1, int i_2) { + Item tmp = itemsHeap[i_1]; + itemsHeap[i_1] = itemsHeap[i_2]; + itemsHeap[i_2] = tmp; + } + void sort_items() { + int i_mx = 0; + for (int i = 0; i < itemsHeap.size(); ++i) { + i_mx = i; + for (int j = i + 1; j < itemsHeap.size(); ++j) { + if (itemsHeap[j].GetPriceOfWeight() > itemsHeap[i_mx].GetPriceOfWeight()) { + i_mx = j; + } + } + if (i != i_mx) + m_swap(i, i_mx); + } + } +public: + int GetSizeHeap() { + return itemsHeap.size(); + } + void AddItemInHeap() { + Item newItem; + newItem.SetParametresOfItem(); + itemsHeap.push_back(newItem); + } + Item getItem() { + if (!sorted) { + sort_items(); + sorted = true; + } + Item temp = itemsHeap[0]; + itemsHeap.erase(itemsHeap.begin()); + return temp; + } +}; + +class Knapsack { +private: + int max_weight; + int weight = 0; + int price = 0; + ItemsHeap heap; + std::vector items; + void SumOfItems(Item temp) { + price += temp.GetItemPrice(); + } + void WeightOfItems(Item temp) { + weight += temp.GetItemWeight(); + } +public: + int GetSizeOfHeapOnFloor() { + return heap.GetSizeHeap(); + } + void SetMaxWeight() { + std::cout << "Enter max weight: "; + std::cin >> max_weight; + std::cout << std::endl; + } + void PrintItems() { + std::cout << "Items:\n"; + for (int i = 0; i < items.size(); ++i) { + items[i].PrintParametres(); + } + } + void PrintParametresKnapsack() { + std::cout << "Backpack parametres:\n" << "\tPrice: " << price << " " << "\n\tWeight: " << weight << " " << "\n\tMax weight: " << max_weight << std::endl; + PrintItems(); + } + void AddItemOnFloor() { + heap.AddItemInHeap(); + } + void AddItemInKnapsack() { + while (heap.GetSizeHeap() > 0) { + Item temp = heap.getItem(); + if (temp.GetItemWeight() + weight <= max_weight) + { + items.push_back(temp); + SumOfItems(temp); + WeightOfItems(temp); + } + else + break; + } + std::cout << "Complete task" << std::endl; + } +}; + +void main() { + Knapsack backpack; + backpack.SetMaxWeight(); + backpack.AddItemOnFloor(); + backpack.AddItemInKnapsack(); + backpack.PrintParametresKnapsack(); +} \ No newline at end of file diff --git a/labalaba2/lab2/lab2.vcxproj b/labalaba2/lab2/lab2.vcxproj new file mode 100644 index 0000000..d5d79ac --- /dev/null +++ b/labalaba2/lab2/lab2.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {94371f76-b2cc-4212-8f44-31c220e94e22} + lab2 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/labalaba2/lab2/lab2.vcxproj.filters b/labalaba2/lab2/lab2.vcxproj.filters new file mode 100644 index 0000000..01dcf8d --- /dev/null +++ b/labalaba2/lab2/lab2.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/labalaba2/lab2z3/Zadanie3.cpp b/labalaba2/lab2z3/Zadanie3.cpp new file mode 100644 index 0000000..de4a8d8 --- /dev/null +++ b/labalaba2/lab2z3/Zadanie3.cpp @@ -0,0 +1,63 @@ +#include + + +void m_swap(int* arr, int i_1, int i_2) { + int tmp = arr[i_1]; + arr[i_1] = arr[i_2]; + arr[i_2] = tmp; +} +int fib(int n) { //O(2**n) + if (n < 2) + return 1; + return fib(n - 1) + fib(n - 2); +} + +void sort_choice(int arr[], int size) { //n*n + int i_mn = 0; + for (int i = 0; i < size; ++i) { + i_mn = i; + for (int j = i + 1; j < size; ++j) { + if (arr[j] < arr[i_mn]) { + i_mn = j; + } + } + if (i != i_mn) + m_swap(arr, i, i_mn); + } +} + + +void quickSort(int* arr, int left, int right) //n logN +{ + int piv; + int index; + int _left = left; + int _right = right; + piv = arr[left]; + while (left < right) + { + while ((arr[right] > piv) && (left < right)) + right--; + if (left != right) + { + arr[left] = arr[right]; + left++; + } + while ((arr[left] < piv) && (left < right)) + left++; + if (left != right) + { + arr[right] = arr[left]; + right--; + } + } + arr[left] = piv; + index = left; + left = _left; + right = _right; + if (left < index) + quickSort(arr, left, index - 1); + if (right > index) + quickSort(arr, index + 1, right); +} + diff --git a/labalaba2/lab2z3/lab2z3.vcxproj b/labalaba2/lab2z3/lab2z3.vcxproj new file mode 100644 index 0000000..d6b7dd8 --- /dev/null +++ b/labalaba2/lab2z3/lab2z3.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {5e968207-3ace-4f80-968b-65cd0841b6de} + lab2z3 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/labalaba2/lab2z3/lab2z3.vcxproj.filters b/labalaba2/lab2z3/lab2z3.vcxproj.filters new file mode 100644 index 0000000..b691508 --- /dev/null +++ b/labalaba2/lab2z3/lab2z3.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/labalaba2/labalaba2.sln b/labalaba2/labalaba2.sln new file mode 100644 index 0000000..726c7c0 --- /dev/null +++ b/labalaba2/labalaba2.sln @@ -0,0 +1,51 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34221.43 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "labalaba2", "labalaba2\labalaba2.vcxproj", "{78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab2", "lab2\lab2.vcxproj", "{94371F76-B2CC-4212-8F44-31C220E94E22}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab2z3", "lab2z3\lab2z3.vcxproj", "{5E968207-3ACE-4F80-968B-65CD0841B6DE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Debug|x64.ActiveCfg = Debug|x64 + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Debug|x64.Build.0 = Debug|x64 + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Debug|x86.ActiveCfg = Debug|Win32 + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Debug|x86.Build.0 = Debug|Win32 + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Release|x64.ActiveCfg = Release|x64 + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Release|x64.Build.0 = Release|x64 + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Release|x86.ActiveCfg = Release|Win32 + {78A2C6D7-BAB1-4CE3-B6B6-B52B3BDCBE5E}.Release|x86.Build.0 = Release|Win32 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Debug|x64.ActiveCfg = Debug|x64 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Debug|x64.Build.0 = Debug|x64 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Debug|x86.ActiveCfg = Debug|Win32 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Debug|x86.Build.0 = Debug|Win32 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Release|x64.ActiveCfg = Release|x64 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Release|x64.Build.0 = Release|x64 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Release|x86.ActiveCfg = Release|Win32 + {94371F76-B2CC-4212-8F44-31C220E94E22}.Release|x86.Build.0 = Release|Win32 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Debug|x64.ActiveCfg = Debug|x64 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Debug|x64.Build.0 = Debug|x64 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Debug|x86.ActiveCfg = Debug|Win32 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Debug|x86.Build.0 = Debug|Win32 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Release|x64.ActiveCfg = Release|x64 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Release|x64.Build.0 = Release|x64 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Release|x86.ActiveCfg = Release|Win32 + {5E968207-3ACE-4F80-968B-65CD0841B6DE}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2B4A490B-BC2E-4EA7-B0EA-55C5328D08A6} + EndGlobalSection +EndGlobal diff --git a/labalaba2/labalaba2/SourceDinam.cpp b/labalaba2/labalaba2/SourceDinam.cpp new file mode 100644 index 0000000..4dd21dd --- /dev/null +++ b/labalaba2/labalaba2/SourceDinam.cpp @@ -0,0 +1,154 @@ +#include +#include +#include + + +class Item { +private: + std::string name; + int weight; + int price; +public: + int GetItemWeight() { + return weight; + } + int GetItemPrice() { + return price; + } + double GetPriceOfWeight() { + return price / (double)weight; + } + void PrintParametres() { + std::cout << "\tName item: " << name << "\n\tWeight: " << weight << "\n\tPrice:" << price << std::endl; + } + void SetParametresOfItem() { + std::cout << "Enter name of item: "; + std::cin >> name; + std::cout << std::endl; + std::cout << "Enter weight of item: "; + std::cin >> weight; + std::cout << std::endl; + std::cout << "Enter price of item: "; + std::cin >> price; + std::cout << std::endl; + } +}; + +class ItemsHeap { +private: + std::vector itemsHeap; +public: + int GetSizeHeap() { + return itemsHeap.size(); + } + void AddItemInHeap() { + Item newItem; + newItem.SetParametresOfItem(); + itemsHeap.push_back(newItem); + } + Item getItem(int i) { + return itemsHeap[i]; + } +}; +class Knapsack { +private: + int max_weight; + int weight = 0; + int price = 0; + ItemsHeap heap; + std::vector items; + + void SumOfItems(Item temp) { + price += temp.GetItemPrice(); + } + void WeightOfItems(Item temp) { + weight += temp.GetItemWeight(); + } + void PushItem(Item temp) { + items.push_back(temp); + SumOfItems(temp); + WeightOfItems(temp); + } + void PrintItems() { + std::cout << "Items:\n"; + for (int i = 0; i < items.size(); ++i) { + items[i].PrintParametres(); + } + } +public: + int GetPiceOfKnapsack() { + return price; + } + int GetSizeOfHeapOnFloor() { + return heap.GetSizeHeap(); + } + void SetMaxWeight() { + std::cout << "Enter max weight: "; + std::cin >> max_weight; + std::cout << std::endl; + } + void PrintParametresKnapsack() { + std::cout << "Backpack parametres:\n" << "\tPrice: " << price << " " << "\n\tWeight: " << weight << " " << "\n\tMax weight: " << max_weight << std::endl; + PrintItems(); + } + void AddItemOnFloor() { + heap.AddItemInHeap(); + } + void AddItemInKnapsack() { + if (heap.GetSizeHeap() > 1) { + Knapsack** kp = new Knapsack * [heap.GetSizeHeap() + 1]; + Knapsack temp, temp2; + int max_price = 0, i_mx = 0, j_mx = 0; + for (int i = 0; i < max_weight; ++i) { + kp[i] = new Knapsack[max_weight + 1]; + } + for (int i = 0; i < heap.GetSizeHeap() + 1; ++i) { + for (int j = 0; j < max_weight + 1; ++j) { + if (i == 0 || j == 0) + { + kp[i][j] = temp; + } + else if (i == 1) { + if (heap.getItem(0).GetItemWeight() <= j) + kp[1][j].PushItem(heap.getItem(0)); + else + kp[1][j] = temp; + } + else + { + if (heap.getItem(i - 1).GetItemWeight() > j) + kp[i][j] = kp[i - 1][j]; + else + { + int newPrice = heap.getItem(i - 1).GetItemPrice() + kp[i - 1][j - heap.getItem(i - 1).GetItemWeight()].GetPiceOfKnapsack(); + if (kp[i - 1][j].GetPiceOfKnapsack() > newPrice) + kp[i][j] = kp[i - 1][j]; + else + { + kp[i][j] = kp[i - 1][j - heap.getItem(i - 1).GetItemWeight()]; + kp[i][j].PushItem(heap.getItem(i - 1)); + temp2 = kp[i][j]; + } + } + } + } + } + for (int i = 0; i < temp2.items.size(); ++i) { + PushItem(temp2.items[i]); + } + std::cout << "Complete task" << std::endl; + } + else { + PushItem(heap.getItem(0)); + } + } +}; + +void main() { + Knapsack backpack; + backpack.SetMaxWeight(); + backpack.AddItemOnFloor(); + backpack.AddItemOnFloor(); + backpack.AddItemInKnapsack(); + backpack.PrintParametresKnapsack(); +} \ No newline at end of file diff --git a/labalaba2/labalaba2/labalaba2.vcxproj b/labalaba2/labalaba2/labalaba2.vcxproj new file mode 100644 index 0000000..59521ad --- /dev/null +++ b/labalaba2/labalaba2/labalaba2.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {78a2c6d7-bab1-4ce3-b6b6-b52b3bdcbe5e} + labalaba2 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/labalaba2/labalaba2/labalaba2.vcxproj.filters b/labalaba2/labalaba2/labalaba2.vcxproj.filters new file mode 100644 index 0000000..bd31792 --- /dev/null +++ b/labalaba2/labalaba2/labalaba2.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file