Komlev_S.I. Lab4 #6

Closed
SemkaKMLV wants to merge 3 commits from Lab4 into Lab3
2 changed files with 52 additions and 64 deletions
Showing only changes of commit a87accfcb1 - Show all commits

View File

@ -45,13 +45,9 @@
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setTankers.Count; i++)
foreach (var tanker in _setTankers.GetTankers())
{
var Tanker = _setTankers.Get(i);
if (Tanker != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, Tanker);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, tanker);
}
return new(_pictureWidth, _pictureHeight);
}
@ -70,14 +66,14 @@
int j = _setTankers.Count - 1;
for (int i = 0; i < _setTankers.Count; i++)
{
if (_setTankers.Get(i) == null)
if (_setTankers[i] == null)
{
for (; j > i; j--)
{
var Tanker = _setTankers.Get(j);
if (Tanker != null)
var Truck = _setTankers[j];
if (Truck != null)
{
_setTankers.Insert(Tanker, i);
_setTankers.Insert(Truck, i);
_setTankers.Remove(j);
break;
}
@ -110,10 +106,10 @@
int xPosition = 10;
int yPosition = (_pictureHeight / _placeSizeHeight - 1) * _placeSizeHeight + 10;
for (int i = 0; i < _setTankers.Count; i++)
foreach (var tanker in _setTankers.GetTankers())
{
_setTankers.Get(i)?.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight);
_setTankers.Get(i)?.DrawningObject(g);
tanker.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight);
tanker.DrawningObject(g);
xPosition += _placeSizeWidth;
if (xPosition + _placeSizeWidth > _pictureWidth)
@ -123,6 +119,7 @@
}
}
}
}
}

View File

@ -3,78 +3,69 @@
internal class SetTankersGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetTankersGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
public int Insert(T tanker)
{
bool freeSpace = false;
int firstFreeElement = -1;
for (int i = Count - 1; i >= 0; i--)
{
if (_places[i] == null)
{
freeSpace = true;
firstFreeElement = i;
}
}
if (!freeSpace)
return -1;
for (int i = firstFreeElement - 1; i >= 0; i--)
{
_places[i + 1] = _places[i];
}
_places[0] = tanker;
return 0;
return Insert(tanker, 0);
}
public int Insert(T tanker, int position)
{
if (_places[position] != null)
if (position <= _places.Count && _places.Count < _maxCount && position >= 0)
{
bool freeSpace = false;
int firstFreeElement = -1;
for (int i = Count - 1; i < position; i--)
{
if (_places[i] == null)
{
freeSpace = true;
firstFreeElement = i;
}
}
if (!freeSpace)
return -1;
for (int i = firstFreeElement - 1; i > position; i--)
{
_places[i + 1] = _places[i];
}
_places.Insert(position, tanker);
return position;
}
_places[position] = tanker;
return position;
else
return -1;
}
public T Remove(int position)
{
if (_places[position] != null)
if (position < _places.Count && position >= 0)
{
T result = _places[position];
_places[position] = null;
return result;
var tanker = _places[position];
_places.RemoveAt(position);
return tanker;
}
else
return null;
}
public T Get(int position)
public T this[int position]
{
if (_places[position] != null)
return _places[position];
else return null;
get
{
if (position < _places.Count && position >= 0)
return _places[position];
else
return null;
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetTankers()
{
foreach (var tanker in _places)
{
if (tanker != null)
{
yield return tanker;
}
else
{
yield break;
}
}
}
}
}