Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) PrimeExpertVcl

 

Technical facts

 

 

 

 

 

 

./ToolPrimeExpertVcl/ProjectPrimeExpert.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("UnitFormPrimeExpert.cpp", FormPrimeExpert);
USEFORM("UnitFormPrimeExpertAbout.cpp", FormPrimeExpertAbout);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 Application->Initialize();
                 Application->Title = "PrimeExpert";
                 Application->CreateForm(__classid(TFormPrimeExpert), &FormPrimeExpert);
                 Application->CreateForm(__classid(TFormPrimeExpertAbout), &FormPrimeExpertAbout);
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        catch (...)
        {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
        }
        return 0;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolPrimeExpertVcl/UnitFormPrimeExpert.h

 

//---------------------------------------------------------------------------
/*
  PrimeExpert, tool to calculate whether a number is prime
  Copyright (C) 2008  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormPrimeExpertH
#define UnitFormPrimeExpertH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
#include "UnitPrimeExpert.h"
#include <Mask.hpp>
#include <Grids.hpp>
#include <ValEdit.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TFormPrimeExpert : public TForm
{
__published: // IDE-managed Components
        TLabeledEdit *EditValue;
        TButton *ButtonAbout;
        TRichEdit *RichEdit1;
        void __fastcall EditValueChange(TObject *Sender);
        void __fastcall ButtonAboutClick(TObject *Sender);
private: // User declarations
  PrimeExpert mPrimeExpert;
public: // User declarations
        __fastcall TFormPrimeExpert(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormPrimeExpert *FormPrimeExpert;
//---------------------------------------------------------------------------
bool IsInt(const std::string& s, int& rInt);

#endif

 

 

 

 

 

./ToolPrimeExpertVcl/UnitFormPrimeExpert.cpp

 

//---------------------------------------------------------------------------
/*
  PrimeExpert, tool to calculate whether a number is prime
  Copyright (C) 2008  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <boost/shared_ptr.hpp>
#include <boost/timer.hpp>
#include <boost/lexical_cast.hpp>
#include <vcl.h>
#pragma hdrstop

#include "UnitFormPrimeExpert.h"
#include "UnitFormPrimeExpertAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormPrimeExpert *FormPrimeExpert;
//---------------------------------------------------------------------------
__fastcall TFormPrimeExpert::TFormPrimeExpert(TComponent* Owner)
        : TForm(Owner)
{
  RichEdit1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TFormPrimeExpert::EditValueChange(TObject *Sender)
{
  {
    int temp = 0;
    if ( IsInt(EditValue->Text.c_str(),temp) == false)
    {
      RichEdit1->Lines->Insert(0,"NaN");
      return;
    }
  }
  const int value = boost::lexical_cast<int>( EditValue->Text.c_str() );
  if (value < 2)
  {
    RichEdit1->Lines->Insert(0,"Not prime");
    return;
  }
  if (value == 2)
  {
    RichEdit1->Lines->Insert(0,"Prime");
    return;
  }
  const boost::timer t;
  const bool isPrime = mPrimeExpert.IsPrime(value);
  const double time = t.elapsed();
  const String text
    = String(isPrime == true ? "Prime" : "Not prime")
    + String(" (")
    + FloatToStr(time)
    + String(" sec)");
  RichEdit1->Lines->Insert(0,text);
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppStrIsInt.htm
bool IsInt(const std::string& s, int& rInt)
{
  std::istringstream i(s);
  if (!(i >> rInt))
  {
    rInt = 0;
    return false;
  }
  return true;
}
//---------------------------------------------------------------------------

void __fastcall TFormPrimeExpert::ButtonAboutClick(TObject *Sender)
{
  boost::shared_ptr<TFormPrimeExpertAbout> form(new TFormPrimeExpertAbout(0));
  form->ShowModal();
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolPrimeExpertVcl/UnitFormPrimeExpertAbout.h

 

//---------------------------------------------------------------------------
/*
  PrimeExpert, tool to calculate whether a number is prime
  Copyright (C) 2008  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormPrimeExpertAboutH
#define UnitFormPrimeExpertAboutH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <jpeg.hpp>
//---------------------------------------------------------------------------
class TFormPrimeExpertAbout : public TForm
{
__published: // IDE-managed Components
        TRichEdit *RichEdit1;
        TPanel *PanelTop;
        TImage *Image1;
        TPanel *PanelTopRight;
        TPanel *Panel1;
        TPanel *Panel2;
        TPanel *Panel3;
        TPanel *Panel4;
        TPanel *Panel5;
        TPanel *Panel6;
private: // User declarations
public: // User declarations
        __fastcall TFormPrimeExpertAbout(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormPrimeExpertAbout *FormPrimeExpertAbout;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolPrimeExpertVcl/UnitFormPrimeExpertAbout.cpp

 

//---------------------------------------------------------------------------
/*
  PrimeExpert, tool to calculate whether a number is prime
  Copyright (C) 2008  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormPrimeExpertAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormPrimeExpertAbout *FormPrimeExpertAbout;
//---------------------------------------------------------------------------
__fastcall TFormPrimeExpertAbout::TFormPrimeExpertAbout(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolPrimeExpertVcl/UnitPrimeExpert.h

 

//---------------------------------------------------------------------------
/*
  PrimeExpert, tool to calculate whether a number is prime
  Copyright (C) 2008  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitPrimeExpertH
#define UnitPrimeExpertH
//---------------------------------------------------------------------------
#include <vector>
//---------------------------------------------------------------------------
struct PrimeExpert
{
  PrimeExpert();

  const bool IsPrime(const int x);

  private:
  std::vector<int> mPrimes;

  void CalculateNextPrime();
  const int CalculateMax(const int x);
  friend std::ostream& operator<<(std::ostream&, const PrimeExpert&);
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const PrimeExpert& primeExpert);
//---------------------------------------------------------------------------


#endif

 

 

 

 

 

./ToolPrimeExpertVcl/UnitPrimeExpert.cpp

 

//---------------------------------------------------------------------------
/*
  PrimeExpert, tool to calculate whether a number is prime
  Copyright (C) 2008  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <cassert>
#pragma hdrstop

#include "UnitPrimeExpert.h"
//---------------------------------------------------------------------------
PrimeExpert::PrimeExpert()
{
  mPrimes.push_back(2);
}
//---------------------------------------------------------------------------
const bool PrimeExpert::IsPrime(const int x)
{
  assert(x > 2);
  //Calculate the maximum value for devision
  const int max = CalculateMax(x);

  assert(CalculateMax( 4) == 3); //When examining 4, divide (from 2) to 3
  assert(CalculateMax( 5) == 3);
  assert(CalculateMax( 6) == 3);
  assert(CalculateMax( 7) == 3);
  assert(CalculateMax( 8) == 3);
  assert(CalculateMax( 9) == 4); //When examining 9, divide (from 2) to 4
  assert(CalculateMax(10) == 4);
  assert(CalculateMax(11) == 4);
  assert(CalculateMax(12) == 4);
  assert(CalculateMax(13) == 4);
  assert(CalculateMax(14) == 4);
  assert(CalculateMax(15) == 4);
  assert(CalculateMax(16) == 5); //When examining 16, divide (from 2) to 5
  assert(CalculateMax(17) == 5);

  //Collect enough prime numbers to find if x is prime,
  //  if these are not yet present
  while ( mPrimes.back() < CalculateMax(x) )
    CalculateNextPrime();

  for (int i=0; ;++i)
  {
    assert( i < static_cast<int>(mPrimes.size() ) );
    const int knownPrime = mPrimes[i];
    if (knownPrime >= max) return true;
    if ( x % knownPrime == 0) return false;
  }

}
//---------------------------------------------------------------------------
const int PrimeExpert::CalculateMax(const int x)
{
  return 1 + static_cast<int>(
    std::sqrt(static_cast<double>(x) ) );

}
//---------------------------------------------------------------------------
void PrimeExpert::CalculateNextPrime()
{
  const int heighestKnownPrime = mPrimes.back();

  for (int i = heighestKnownPrime + 1; ;++i)
  {
    if (this->IsPrime(i)==true) { mPrimes.push_back(i); return; }
  }
}
//------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const PrimeExpert& primeExpert)
{
  std::copy(
    primeExpert.mPrimes.begin(),
    primeExpert.mPrimes.end(),
    std::ostream_iterator<int>(os," ") );
  return os;
}
//------------------------------------------------------------


#pragma package(smart_init)

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml