.NET 4.x Организовал стек, разбив код на классы - C#
Формулировка задачи:
Необходимо разбить на классы и в них организовать свой стек... то есть в основном коде программы сделать лишь обращения к ним. Очень нужна помощь, спасибо.
using System;
using System.Collections.Generic;
namespace Mat
{
class Hill
{
private string name;
public int height;
public Hill(string name,int height)
{
this.name = name;
this.height = height;
}
public override string ToString()
{
return name + " " + height+ " метров";
}
}
class Program
{
static void Main()
{
Stack<Hill>hills=new Stack<Hill>();
hills.Push(new Hill("Гора 1 ",1000));
hills.Push(new Hill("Гора 2 ",2000));
double s = 0;
foreach (var a in hills)
{
s += a.height;
Console.WriteLine(a);
}
Console.WriteLine(s/hills.Count);
}
}
}Решение задачи: «.NET 4.x Организовал стек, разбив код на классы»
textual
Листинг программы
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication16
{
class QStack<T>:IEnumerator<T>,IEnumerable<T>,ICollection<T>
{
private T[] _arr;
private int _tail;
private int _foreachCounter;
/// <summary>
/// Stack class
/// </summary>
/// <param name="count">capacity</param>
public QStack(int count)
{
_arr=new T[count];
_tail = -1;
_foreachCounter = -1;
}
public QStack() : this(0) { }
public QStack(ICollection collection):this(0)
{
foreach (T a in collection)
{
Push(a);
}
}
public T Peek()
{
if(Count==0)
throw new Exception("Stack is empty");
return _arr[_tail];
}
public void Add(T item)
{
Push(item);
}
public void Clear()
{
_arr=new T[0];
Count = 0;
_tail = -1;
_foreachCounter = -1;
}
public bool Contains(T item)
{
bool res = false;
for (int i = 0; i <=_tail; i++)
{
if (_arr[i].Equals(item))
{
res = true;
break;
}
}
return res;
}
public void CopyTo( T[] array, int arrayIndex)
{
for (int i = arrayIndex,j=0; i < Count; i++,j++)
{
array[j] = _arr[i];
}
}
public bool Remove(T item)
{
if (!Contains(item))
return false;
T[]tmp=new T[Count];
Array.Copy(_arr,tmp,Count);
List<T>lst=new List<T>(tmp);
lst.Remove(item);
tmp = lst.ToArray();
Array.Resize(ref tmp,_arr.Length);
_arr = tmp;
Count--;
_tail--;
return true;
}
public int Count { get; private set; }
public bool IsReadOnly
{
get { return false; }
}
public void Push(T item)
{
if (_tail+1 < _arr.Length)
{
Count++;
_tail++;
_arr[_tail] = item;
}
else
{
Array.Resize(ref _arr, _arr.Length*2+10);
Count++;
_tail++;
_arr[_tail] = item;
}
}
public void Push(params T[] elements)
{
foreach (var a in elements)
{
Push(a);
}
}
public T Pop()
{
if(Count==0)
throw new Exception("Stack is empty!");
T item = _arr[_tail];
_tail--;
Count--;
return item;
}
public void Dispose()
{
_foreachCounter = -1;
}
public bool MoveNext()
{
_foreachCounter++;
return _foreachCounter < Count;
}
public void Reset()
{
throw new NotImplementedException();
}
public T Current
{
get { return _arr[_foreachCounter]; }
}
object IEnumerator.Current
{
get { return Current; }
}
public IEnumerator<T> GetEnumerator()
{
return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
class Hill
{
private string name;
public int height;
public Hill(string name, int height)
{
this.name = name;
this.height = height;
}
public override string ToString()
{
return name + " " + height + " метров";
}
}
class Program
{
static void Main(string[] args)
{
QStack<Hill> hills = new QStack<Hill>();
hills.Push(new Hill("Гора 1 ", 1000));
hills.Push(new Hill("Гора 2 ", 2000));
double s = 0;
foreach (var a in hills)
{
s += a.height;
Console.WriteLine(a);
}
Console.WriteLine(s / hills.Count);
Console.ReadKey(true);
}
}
}