Feedback

C# - PrimeNumberReader

Veröffentlicht von am 10/15/2006
(1 Bewertungen)
Liest eine Folge von Primzahlen aus der durch PrimeNumberGenerator generierten Datei (benötigt Snippet PrimeNumberGenerator)
/// <summary>
/// A class for sequentially reading prime numbers from the file specified in PrimeNumberGenerator.PrimeDatabaseFileName
/// </summary>
/// <remarks>
/// Usage: 
/// 
/// static void Main(string[] args)
/// {
///    PrimeNumberReader pnr = new PrimeNumberReader()
///    pnr.BeginGetNextPrime();
///    ...
///    pnr.GetNextPrime();
///    ...
///    pnr.EndGetNextPrime();
/// }
/// </remarks>
public class PrimeNumberReader
{
    private BinaryReader file = null;
    /// <summary>
    /// Prepare for reading prime numbers from file
    /// </summary>
    public void BeginGetNextPrime()
    {
        if (File.Exists(PrimeNumberGenerator.PrimeDatabaseFileName))
        {
            file = new BinaryReader(new FileStream(PrimeNumberGenerator.PrimeDatabaseFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
        }
        else
        {
            throw new PrimeNumbersNotCalculatedException();
        }
    }
    /// <summary>
    /// End reading prime numbers from file
    /// </summary>
    public void EndGetNextPrime()
    {
        file.Close();
        file = null;
    }
    /// <summary>
    /// Get the next prime number in file
    /// </summary>
    /// <returns>The next prime number found in file at current position</returns>
    public long GetNextPrime()
    {
        if (file == null)
        {
            throw new InvalidOperationException("Call BeginGetNextPrime() first");
        }
        if (file.BaseStream.Position < file.BaseStream.Length)
        {
            return file.ReadInt64();
        }
        else
        {
            throw new PrimeNumbersNotCalculatedException();
        }
    }
    /// <summary>
    /// Get the next prime number in file greater than a specified number
    /// </summary>
    /// <param name="Number">The prime has to be greater than this number</param>
    /// <returns>The next prime greater than the specified number</returns>
    public long GetNextPrime( long Number )
    {
        if (file == null)
        {
            throw new InvalidOperationException("Call BeginGetNextPrime() first");
        }
        long intern = 0;
        while ((file.BaseStream.Position < file.BaseStream.Length) && (intern < Number))
        {
            intern = file.ReadInt64();
        }
        if (intern > Number)
        {
            return intern;
        }
        else
        {
            throw new PrimeNumbersNotCalculatedException();
        }
    }
}
/// <summary>
/// This exception is thrown if a prime number is needed that is greater
/// than any by now calculated prime numbers => calculate more
/// </summary>
public class PrimeNumbersNotCalculatedException : System.Exception
{
    public long GreatestPrimeByNow
    {
        get
        {
            return PrimeNumberGenerator.GreatestKnownPrimeNumber;
        }
    }
    public PrimeNumbersNotCalculatedException() : base(){}
}

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!