64 lines
1.5 KiB
C#
64 lines
1.5 KiB
C#
using System.Collections;
|
|
|
|
namespace DigitSequence;
|
|
|
|
public sealed class DigitEnumerator : IEnumerator<int>
|
|
{
|
|
private int _divisor;
|
|
private bool _started;
|
|
private readonly int _number;
|
|
|
|
/// <summary>
|
|
/// The constructor for <see cref="DigitEnumerator"/>
|
|
/// </summary>
|
|
/// <param name="number">The number</param>
|
|
public DigitEnumerator(int number)
|
|
{
|
|
_number = Math.Abs(number);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves to the next position
|
|
/// Default is invalid
|
|
/// </summary>
|
|
/// <returns>True, if the move was successful, false otherwise</returns>
|
|
public bool MoveNext()
|
|
{
|
|
if (!_started)
|
|
{
|
|
_started = true;
|
|
_divisor = (int)Math.Pow(10, (int)Math.Log10(_number));
|
|
return _divisor >= 0;
|
|
}
|
|
|
|
_divisor /= 10;
|
|
return _divisor > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the current position
|
|
/// </summary>
|
|
public void Reset()
|
|
{
|
|
_started = false;
|
|
_divisor = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Value of the current position
|
|
/// </summary>
|
|
public int Current => _divisor > 0 ? (_number / _divisor) % 10 : 0;
|
|
|
|
/// <summary>
|
|
/// IEnumerator type for <seealso cref="Current"/>
|
|
/// </summary>
|
|
object IEnumerator.Current => Current;
|
|
|
|
/// <summary>
|
|
/// Disposes resources
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
// TODON'T! it doesn't seem that there is anything that has the need to be disposed
|
|
}
|
|
}
|