Feedback

C# - PrimeNumberReader

Veröffentlicht von am 15.10.2006
(1 Bewertungen)
Liest eine Folge von Primzahlen aus der durch PrimeNumberGenerator generierten Datei (benötigt Snippet PrimeNumberGenerator)
GFU-Schulungen  [Anzeige]

Visual Studio Team Foundation Server 2017/2015 (TFS) für Administratoren - Kompakt

Nach dieser Schulung beherrschen Sie die Grundlagen des TFS. Sie erledigen administrative Aufgaben schnell und sicher.

JavaScript für .NET-Entwickler

Sie sind .NET-Entwickler und nun stehen Sie vor der Aufgabe, JavaScript in Ihre Arbeit einzubinden. Die Schulung vermittelt Ihnen die JavaScript Grundlagen und die Funktionen der Scriptsprache. Sie wissen, wie objektorientierte Programmierung in JavaScript funktioniert und lernen abschließend Best Practicies Fälle kennen.

/// <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!