добавление стратегии
This commit is contained in:
parent
125be39c5f
commit
a69215962d
@ -5,6 +5,7 @@
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
Unknow = -1,
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
@ -23,5 +24,5 @@ public enum DirectionType
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
Right = 4,
|
||||
}
|
@ -31,23 +31,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;
|
||||
base.DrawTransport(g);
|
||||
@ -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);
|
||||
}
|
||||
|
||||
//крыло колеса
|
||||
|
@ -43,6 +43,26 @@ public class DrawingTanker
|
||||
/// </summary>
|
||||
private readonly int _drawingTankerHeight = 50;
|
||||
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawingTankerWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawingTankerHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
@ -72,8 +92,8 @@ public class DrawingTanker
|
||||
/// <param name="_drawingTankerHeight">Высота прорисовки газовоза</param>
|
||||
protected DrawingTanker(int _drawingTankerWidth, int _drawingTankerHeight) : this()
|
||||
{
|
||||
_drawingTankerWidth = _drawingTankerWidth;
|
||||
_drawingTankerHeight = _drawingTankerHeight;
|
||||
this._drawingTankerWidth = _drawingTankerWidth;
|
||||
this._drawingTankerHeight = _drawingTankerHeight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -98,9 +118,9 @@ public class DrawingTanker
|
||||
_startPosY = _pictureHeight.Value - _drawingTankerHeight;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawingTanker? _drawingTanker;
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация формы
|
||||
/// Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _strategy;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор формы
|
||||
/// </summary>
|
||||
public FormGasolineTanker()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки бензовоза
|
||||
/// </summary>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Попытка переместить объект в указанном направлении
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект класса DrawingTanker или его наследника
|
||||
/// </summary>
|
||||
private readonly DrawingTanker? _tanker = null;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="tanker">Объект класса DrawingTanker</param>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конвертация из MovementDirection в DirectionType
|
||||
/// </summary>
|
||||
/// <param name="direction">MovementDirection</param>
|
||||
/// <returns>DirectionType</returns>
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
@ -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,
|
||||
}
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Координата x
|
||||
/// </summary>
|
||||
private readonly int _x;
|
||||
|
||||
/// <summary>
|
||||
/// Координата y
|
||||
/// </summary>
|
||||
private readonly int _y;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
private readonly int _width;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
private readonly int _height;
|
||||
|
||||
/// <summary>
|
||||
/// Левая граница
|
||||
/// </summary>
|
||||
public int LeftBorder => _x;
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
public int TopBorder => _y;
|
||||
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
public int RightBorder => _x + _width;
|
||||
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
public int DownBorder => _y + _height;
|
||||
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="x">Координата x</param>
|
||||
/// <param name="y">Координата y</param>
|
||||
/// <param name="width">Ширина объекта</param>
|
||||
/// <param name="height">Высота объекта</param>
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
31
laboap1/laboap1.sln
Normal file
31
laboap1/laboap1.sln
Normal file
@ -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
|
151
laboap1/laboap1/laboap1.cpp
Normal file
151
laboap1/laboap1/laboap1.cpp
Normal file
@ -0,0 +1,151 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
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;
|
||||
}
|
136
laboap1/laboap1/laboap1.vcxproj
Normal file
136
laboap1/laboap1/laboap1.vcxproj
Normal file
@ -0,0 +1,136 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{1f4724c2-6bc1-42c2-acc1-93aaa85ba624}</ProjectGuid>
|
||||
<RootNamespace>laboap1</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard_C>Default</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="laboap1.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
22
laboap1/laboap1/laboap1.vcxproj.filters
Normal file
22
laboap1/laboap1/laboap1.vcxproj.filters
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Исходные файлы">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Файлы заголовков">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Файлы ресурсов">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="laboap1.cpp">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user