Генерация последовательности с IEnumerator - C#
Формулировка задачи:
Приветствую!
Необходимо реализовать генерацию последовательности xn=1/yn до тех пор, пока |x|> e(задается заранее). Сделать это с реализацией интерфейсов IEnumerable/IEnumerator.
При реализации программ запрещено использовать стандартные классы коллекций библиотеки .NET.
Я сделал класс для реализации интерфейсов, но не могу сообразить, как сделать реализацию рассчета - у меня х подается как массив, а количество элементов изначально неизвестно, тк зависит от параметра е. Помогите реализовать функцию для рассчета хn, пожалуйста!
Кусок кода с классом:
class RowEnumerator : IEnumerator,IEnumerable
{
public double[] x;
int pos = -1;
public RowEnumerator(double[] xo)
{
x = new double[xo.Length];
for (int i = 0; i < xo.Length; i++)
{
x[i] = xo[i];
}
}
public bool MoveNext()
{
pos++;
return (pos < x.Length);
}
public void Reset()
{
pos = -1;
}
public double Current
{
get
{
try
{
return x[pos];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
object IEnumerator.Current
{
get { return Current; }
}
public IEnumerator GetEnumerator()
{
return GetEnumerator();
}
}Решение задачи: «Генерация последовательности с IEnumerator»
textual
Листинг программы
class FuncEnumerable : IEnumerable<double>
{
private readonly double _y, _e;
public FuncEnumerable(double y, double e)
{
_y = y;
_e = e;
}
public IEnumerator<double> GetEnumerator()
{
return new FuncEnumerator(_y, _e);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
class FuncEnumerator : IEnumerator<double>
{
private int _n;
private readonly double _y, _e;
public double Current { get; private set; }
public FuncEnumerator(double y, double e)
{
_y = y;
_e = e;
Current = double.MaxValue;
}
object IEnumerator.Current
{
get { return Current; }
}
public void Reset()
{
_n = 0;
Current = double.MaxValue;
}
public bool MoveNext()
{
if (Math.Abs(Current) < _e) return false;
_n++;
Current = 1/Math.Pow(_y, _n);
return !(Math.Abs(Current) < _e);
}
void IDisposable.Dispose()
{
}
}
}