From c1dbdd80ed275dc0f602f53da7e2b8cd4131c091 Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Mon, 3 Oct 2022 16:01:05 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D1=82=D0=B8=D0=BF=D1=8B=20=D0=B2=D0=BE=D0=B7=D0=B2?= =?UTF-8?q?=D1=80=D0=B0=D1=89=D0=B0=D0=B5=D0=BC=D1=8B=D1=85=20=D0=B7=D0=BD?= =?UTF-8?q?=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetArtilleries.cs | 4 ++-- .../MapWithSetArtilleriesGeneric.cs | 8 +++---- .../SetArtilleriesGeneric.cs | 24 ++++++++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs index 2317b42..47f71e5 100644 --- a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs +++ b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs @@ -52,7 +52,7 @@ namespace Artilleries if (form.ShowDialog() == DialogResult.OK) { DrawingObjectArtillery car = new(form.SelectedArtillery); - if (_mapArtilleriesCollectionGeneric + car) + if (_mapArtilleriesCollectionGeneric + car != -1) { MessageBox.Show("Объект добавлен"); pictureBoxArtilleries.Image = _mapArtilleriesCollectionGeneric.ShowSet(); @@ -75,7 +75,7 @@ namespace Artilleries return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapArtilleriesCollectionGeneric - pos) + if (_mapArtilleriesCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); pictureBoxArtilleries.Image = _mapArtilleriesCollectionGeneric.ShowSet(); diff --git a/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs index d53c409..4c03bfe 100644 --- a/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs @@ -27,14 +27,14 @@ namespace Artilleries _map = map; } - public static bool operator +(MapWithSetArtilleriesGeneric map, T artillery) + public static int operator +(MapWithSetArtilleriesGeneric map, T artillery) { - return map._setArtilleries.Insert(artillery); // TODO + return map._setArtilleries.Insert(artillery); } - public static bool operator -(MapWithSetArtilleriesGeneric map, int position) + public static T operator -(MapWithSetArtilleriesGeneric map, int position) { - return map._setArtilleries.Remove(position); // TODO + return map._setArtilleries.Remove(position); } public Bitmap ShowSet() diff --git a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs index 2f87663..35c69f5 100644 --- a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs @@ -17,22 +17,22 @@ namespace Artilleries _places = new T[count]; } - public bool Insert(T artillery) + public int Insert(T artillery) { return Insert(artillery, 0); } - public bool Insert(T artillery, int position) + public int Insert(T artillery, int position) { if (position < 0 || position >= Count) { - return false; + return -1; } if (_places[position] == null) { _places[position] = artillery; - return true; + return position; } int firstNull = -1; @@ -48,27 +48,29 @@ namespace Artilleries if (firstNull == -1) { - return false; + return -1; } for (int i = firstNull; i > position; i--) { - (_places[i], _places[i - 1]) = (_places[i - 1], _places[i]); + _places[i] = _places[i - 1]; } _places[position] = artillery; - return true; + return position; } - public bool Remove(int position) + public T Remove(int position) { - if (position < 0 || position >= Count || _places[position] == null) + if (position < 0 || position >= Count) { - return false; + return null; } + var result = _places[position]; + _places[position] = null; - return true; + return result; } public T Get(int position)