From a69215962d5178c32c323df35a56423e77cca207 Mon Sep 17 00:00:00 2001 From: ILYAkuznetsov73 <148066069+ILYAkuznetsov73@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:48:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawings/DirectionType.cs | 3 +- .../Drawings/DrawingGasolineTanker.cs | 21 +-- .../Drawings/DrawingTanker.cs | 26 ++- .../FormGasolineTanker.Designer.cs | 26 +++ .../FormGasolineTanker.cs | 42 ++++- .../MovementStrategy/AbstractStrategy.cs | 78 +++++++++ .../MovementStrategy/IMoveableObject.cs | 27 ++++ .../MovementStrategy/MoveToBorder.cs | 41 +++++ .../MovementStrategy/MoveToCenter.cs | 42 +++++ .../MovementStrategy/MoveableTanker.cs | 66 ++++++++ .../MovementStrategy/MovementDirection.cs | 15 ++ .../MovementStrategy/ObjectParameters.cs | 73 +++++++++ .../MovementStrategy/StrategyStatus.cs | 14 ++ laboap1/laboap1.sln | 31 ++++ laboap1/laboap1/laboap1.cpp | 151 ++++++++++++++++++ laboap1/laboap1/laboap1.vcxproj | 136 ++++++++++++++++ laboap1/laboap1/laboap1.vcxproj.filters | 22 +++ 17 files changed, 787 insertions(+), 27 deletions(-) create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/IMoveableObject.cs create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveableTanker.cs create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MovementDirection.cs create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/ObjectParameters.cs create mode 100644 ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/StrategyStatus.cs create mode 100644 laboap1/laboap1.sln create mode 100644 laboap1/laboap1/laboap1.cpp create mode 100644 laboap1/laboap1/laboap1.vcxproj create mode 100644 laboap1/laboap1/laboap1.vcxproj.filters diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DirectionType.cs b/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DirectionType.cs index 0e27236..f552d6b 100644 --- a/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DirectionType.cs +++ b/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DirectionType.cs @@ -5,6 +5,7 @@ /// public enum DirectionType { + Unknow = -1, /// /// Вверх /// @@ -23,5 +24,5 @@ public enum DirectionType /// /// Вправо /// - Right = 4 + Right = 4, } \ No newline at end of file diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs b/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs index 5210b0f..97b8d07 100644 --- a/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs +++ b/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingGasolineTanker.cs @@ -30,23 +30,6 @@ public class DrawingGasolineTanker : DrawingTanker Pen pen = new Pen(Color.Black); Brush dopBrush = new SolidBrush(tanker.AdditionalColor); - - //колеса - Brush brBlack = new SolidBrush(Color.Black); - g.FillEllipse(brBlack, _startPosX.Value + 130, _startPosY.Value + 35, 20, 20); - g.FillEllipse(brBlack, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20); - g.FillEllipse(brBlack, _startPosX.Value + 30, _startPosY.Value + 35, 20, 20); - //корпус - Brush br = new SolidBrush(tanker?.BodyColor ?? Color.Black); - g.FillRectangle(br, _startPosX.Value + 115, _startPosY.Value + 5, 40, 40); - g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 35, 140, 10); - //окно - Brush brBlue = new SolidBrush(Color.LightBlue); - g.FillRectangle(brBlue, _startPosX.Value + 120, _startPosY.Value + 10, 25, 25); - //окантовка - g.DrawRectangle(pen, _startPosX.Value + 120, _startPosY.Value + 10, 25, 25); - g.DrawRectangle(pen, _startPosX.Value + 115, _startPosY.Value + 5, 40, 40); - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 35, 105, 10); _startPosX += 10; _startPosY += 10; @@ -57,8 +40,8 @@ public class DrawingGasolineTanker : DrawingTanker //бак бензовоза if (tanker != null && tanker.Tank) { - g.FillRectangle(dopBrush, _startPosX.Value + 25, _startPosY.Value + 5, 100, 35); - g.FillRectangle(dopBrush, _startPosX.Value + 80, _startPosY.Value, 10, 5); + g.FillRectangle(dopBrush, _startPosX.Value + 15, _startPosY.Value + 3, 100, 35); + g.FillRectangle(dopBrush, _startPosX.Value + 70, _startPosY.Value, 5, 5); } //крыло колеса diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingTanker.cs b/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingTanker.cs index 8aa437f..84e09f2 100644 --- a/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingTanker.cs +++ b/ProjectGasolineTanker/ProjectGasolineTanker/Drawings/DrawingTanker.cs @@ -43,6 +43,26 @@ public class DrawingTanker /// private readonly int _drawingTankerHeight = 50; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawingTankerWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawingTankerHeight; + /// /// Пустой конструктор /// @@ -72,8 +92,8 @@ public class DrawingTanker /// Высота прорисовки газовоза protected DrawingTanker(int _drawingTankerWidth, int _drawingTankerHeight) : this() { - _drawingTankerWidth = _drawingTankerWidth; - _drawingTankerHeight = _drawingTankerHeight; + this._drawingTankerWidth = _drawingTankerWidth; + this._drawingTankerHeight = _drawingTankerHeight; } /// @@ -98,9 +118,9 @@ public class DrawingTanker _startPosY = _pictureHeight.Value - _drawingTankerHeight; } } - return true; } + /// /// Установка позиции /// diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.Designer.cs b/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.Designer.cs index fcc0ec1..2e74d11 100644 --- a/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.Designer.cs +++ b/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.Designer.cs @@ -35,6 +35,8 @@ buttonRight = new Button(); buttonUp = new Button(); buttonCreateTanker = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxGasolineTanker).BeginInit(); SuspendLayout(); // @@ -117,11 +119,33 @@ buttonCreateTanker.UseVisualStyleBackColor = true; buttonCreateTanker.Click += buttonCreateTanker_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(1118, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(182, 33); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(1188, 51); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(112, 34); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormGasolineTanker // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1312, 749); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateTanker); Controls.Add(buttonUp); Controls.Add(buttonRight); @@ -144,5 +168,7 @@ private Button buttonRight; private Button buttonUp; private Button buttonCreateTanker; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs b/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs index bf37407..ba4eed7 100644 --- a/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs +++ b/ProjectGasolineTanker/ProjectGasolineTanker/FormGasolineTanker.cs @@ -1,4 +1,5 @@ using ProjectGasolineTanker.Drawings; +using ProjectGasolineTanker.MovementStrategy; using System; using System.Collections.Generic; using System.ComponentModel; @@ -17,16 +18,21 @@ namespace ProjectGasolineTanker /// Поле-объект для прорисовки объекта /// private DrawingTanker? _drawingTanker; + /// - /// Инициализация формы + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + + /// + /// Конструктор формы /// public FormGasolineTanker() { InitializeComponent(); + _strategy = null; } - /// - /// Метод прорисовки бензовоза - /// + private void Draw() { if (_drawingTanker == null) @@ -60,6 +66,8 @@ namespace ProjectGasolineTanker } _drawingTanker.SetPictureSize(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height); _drawingTanker.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -116,5 +124,31 @@ namespace ProjectGasolineTanker Draw(); } } + + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingTanker == null) return; + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) return; + _strategy.SetData(new MoveableTanker(_drawingTanker), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height); + } + if (_strategy == null) return; + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/AbstractStrategy.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..018f319 --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,78 @@ +using System; + +namespace ProjectGasolineTanker.MovementStrategy +{ + public abstract class AbstractStrategy + { + private IMoveableObject? _moveableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + + protected int FieldWidth { get; private set; } + protected int FieldHeight { get; private set; } + + public StrategyStatus GetStatus() { return _state; } + + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + + MoveToTarget(); + } + + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + + protected bool MoveRight() => MoveTo(MovementDirection.Right); + + protected bool MoveUp() => MoveTo(MovementDirection.Up); + + protected bool MoveDown() => MoveTo(MovementDirection.Down); + + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + + protected abstract void MoveToTarget(); + + protected abstract bool IsTargetDestinaion(); + + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } + } +} diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/IMoveableObject.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..48131ff --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasolineTanker.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToBorder.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..805069b --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasolineTanker.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth && objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.RightBorder - FieldWidth + 11; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) MoveLeft(); + else MoveRight(); + } + int diffY = objParams.DownBorder - FieldHeight + 11; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) MoveUp(); + else MoveDown(); + } + } +} diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToCenter.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..844964e --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasolineTanker.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) MoveLeft(); + else MoveRight(); + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) MoveUp(); + else MoveDown(); + } + } +} diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveableTanker.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveableTanker.cs new file mode 100644 index 0000000..632bcf2 --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MoveableTanker.cs @@ -0,0 +1,66 @@ +using ProjectGasolineTanker.Drawings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasolineTanker.MovementStrategy; + +public class MoveableTanker : IMoveableObject +{ + /// + /// Поле-объект класса DrawingTanker или его наследника + /// + private readonly DrawingTanker? _tanker = null; + + /// + /// Конструктор + /// + /// Объект класса DrawingTanker + public MoveableTanker(DrawingTanker tanker) + { + _tanker = tanker; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_tanker == null || _tanker.EntityTanker == null || !_tanker.GetPosX.HasValue || !_tanker.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_tanker.GetPosX.Value, _tanker.GetPosY.Value, _tanker.GetWidth, _tanker.GetHeight); + } + } + + public int GetStep => (int)(_tanker?.EntityTanker?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_tanker == null || _tanker.EntityTanker == null) + { + return false; + } + + return _tanker.MoveTransport(GetDirectionType(direction)); + } + + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknow, + }; + } +} diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MovementDirection.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..8a070b4 --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/MovementDirection.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasolineTanker.MovementStrategy; + +public enum MovementDirection +{ + Up = 1, + Down = 2, + Left = 3, + Right = 4, +} diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/ObjectParameters.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..efa9bbc --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasolineTanker.MovementStrategy; + +public class ObjectParameters +{ + /// + /// Координата x + /// + private readonly int _x; + + /// + /// Координата y + /// + private readonly int _y; + + /// + /// Ширина объекта + /// + private readonly int _width; + + /// + /// Высота объекта + /// + private readonly int _height; + + /// + /// Левая граница + /// + public int LeftBorder => _x; + /// + /// Верхняя граница + /// + public int TopBorder => _y; + + /// + /// Правая граница + /// + public int RightBorder => _x + _width; + + /// + /// Нижняя граница + /// + public int DownBorder => _y + _height; + + /// + /// Середина объекта + /// + public int ObjectMiddleHorizontal => _x + _width / 2; + /// + /// Середина объекта + /// + public int ObjectMiddleVertical => _y + _height / 2; + + /// + /// Конструктор + /// + /// Координата x + /// Координата y + /// Ширина объекта + /// Высота объекта + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/StrategyStatus.cs b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..9a92c58 --- /dev/null +++ b/ProjectGasolineTanker/ProjectGasolineTanker/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectGasolineTanker.MovementStrategy; + +public enum StrategyStatus +{ + NotInit, + InProgress, + Finish +} diff --git a/laboap1/laboap1.sln b/laboap1/laboap1.sln new file mode 100644 index 0000000..9f5ffa8 --- /dev/null +++ b/laboap1/laboap1.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34525.116 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "laboap1", "laboap1\laboap1.vcxproj", "{1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}" +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 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Debug|x64.ActiveCfg = Debug|x64 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Debug|x64.Build.0 = Debug|x64 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Debug|x86.ActiveCfg = Debug|Win32 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Debug|x86.Build.0 = Debug|Win32 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Release|x64.ActiveCfg = Release|x64 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Release|x64.Build.0 = Release|x64 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Release|x86.ActiveCfg = Release|Win32 + {1F4724C2-6BC1-42C2-ACC1-93AAA85BA624}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {898933DD-7FA8-4FA7-872F-63991ACB024A} + EndGlobalSection +EndGlobal diff --git a/laboap1/laboap1/laboap1.cpp b/laboap1/laboap1/laboap1.cpp new file mode 100644 index 0000000..0329b66 --- /dev/null +++ b/laboap1/laboap1/laboap1.cpp @@ -0,0 +1,151 @@ +#include +#include +#include + +void printArray(int arr[], int size) { + for (int i = 0; i < size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} + +void insertionSort(int arr[], int size) { + int i, key, j; + for (i = 1; i < size; i++) { + key = arr[i]; + j = i - 1; + + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +void merge(int arr[], int left, int mid, int right) { + int i, j, k; + int n1 = mid - left + 1; + int n2 = right - mid; + + int L[n1], R[n2]; + + for (i = 0; i < n1; i++) + L[i] = arr[left + i]; + for (j = 0; j < n2; j++) + R[j] = arr[mid + 1 + j]; + + i = 0; + j = 0; + k = left; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } + else { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +} + +void mergeSort(int arr[], int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + + merge(arr, left, mid, right); + } +} + +int main() { + srand(time(0)); + + int size; + printf("Enter the size of the array: "); + scanf("%d", &size); + + int arr[size]; + for (int i = 0; i < size; i++) { + arr[i] = rand() % 100; + } + + printf("Randomly generated array: "); + printArray(arr, size); + + int choice; + printf("Enter 1 to delete an element, 2 to insert a new element: "); + scanf("%d", &choice); + + if (choice == 1) { + int index; + printf("Enter the index of the element you want to delete: "); + scanf("%d", &index); + + for (int i = index; i < size - 1; i++) { + arr[i] = arr[i + 1]; + } + size--; + } + else if (choice == 2) { + int index, element; + printf("Enter the index at which you want to insert the new element: "); + scanf("%d", &index); + printf("Enter the new element: "); + scanf("%d", &element); + + for (int i = size; i > index; i--) { + arr[i] = arr[i - 1]; + } + arr[index] = element; + size++; + } + else { + printf("Invalid choice"); + return 0; + } + + printf("Modified array: "); + printArray(arr, size); + + int arr_copy[size]; + for (int i = 0; i < size; i++) { + arr_copy[i] = arr[i]; + } + + clock_t start_time = clock(); + insertionSort(arr, size); + clock_t end_time = clock(); + double insertion_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC; + + printf("Array sorted using Insertion Sort: "); + printArray(arr, size); + printf("Time taken by Insertion Sort: %f seconds\n", insertion_time); + + start_time = clock(); + mergeSort(arr_copy, 0, size - 1); + end_time = clock(); + double merge_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC; + + printf("Array sorted using Merge Sort: "); + printArray(arr_copy, size); + printf("Time taken by Merge Sort: %f seconds\n", merge_time); + + return 0; +} \ No newline at end of file diff --git a/laboap1/laboap1/laboap1.vcxproj b/laboap1/laboap1/laboap1.vcxproj new file mode 100644 index 0000000..085cb5f --- /dev/null +++ b/laboap1/laboap1/laboap1.vcxproj @@ -0,0 +1,136 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {1f4724c2-6bc1-42c2-acc1-93aaa85ba624} + laboap1 + 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 + Default + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/laboap1/laboap1/laboap1.vcxproj.filters b/laboap1/laboap1/laboap1.vcxproj.filters new file mode 100644 index 0000000..2bff827 --- /dev/null +++ b/laboap1/laboap1/laboap1.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