piątek, 26 sierpnia 2016

Fitrst numbers in ListBox C++ Builder



private: // User declarations
        bool __fastcall is_First(int x);
        int *tab;
        int *difference_value;
        int *value_plus_one;
        void __fastcall init_Tab();
        void __fastcall destroyed_Tab();

#include <vcl.h>
#pragma hdrstop

#include "m_first01.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define max 5000
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

bool __fastcall TForm1::is_First(int x)
{
 int i,sum;
 sum=0;
 for(i=1;i<x+1;i++)
  if(x%i==0)
   ++sum;
 if(sum==2)
  return true;
 else
  return false;
}
void __fastcall TForm1::init_Tab()
{
 tab=new int[max];
 difference_value=new int[max-1];
 value_plus_one=new int[max-1];
}
void __fastcall TForm1::destroyed_Tab()
{
 delete difference_value;
 delete value_plus_one;
 delete tab;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 destroyed_Tab();
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Tab();
 int i,j;
 i=0;j=0;
 do
 {
  if(is_First(j))
  {
   tab[i]=j;
   ++i;
  }
  ++j;
 }while(i<max);

 for(i=-1,j=i+1;i<max-1;i++,j++)
 {
  difference_value[i+1]=tab[j+1]-tab[i+1];
  value_plus_one[i+1]=difference_value[i+1]+1;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 int i;
 ListBox1->Items->Clear();
 for(i=0;i<max;i++)
 {
  ListBox1->Items->Add(IntToStr(i+1)+")   "+IntToStr(tab[i]));
 }
 ListBox2->Items->Add("--------");
 for(i=0;i<max-1;i++)
 {
  ListBox2->Items->Add(IntToStr(i+1)+")   "+IntToStr(tab[i])+"   -   "+IntToStr(difference_value[i])+"   -   "+IntToStr(value_plus_one[i]));
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 int i;
 ListBox3->Items->Clear();
 i=0;
 do
 {
   if((!is_First(difference_value[i])) && (!is_First(value_plus_one[i])))
   {
    ListBox3->Items->Add(IntToStr(tab[i])+"  -  "+IntToStr(difference_value[i])+"  - "+IntToStr(value_plus_one[i]));
   }
   ++i;
 }while(i<max-1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
 int i;
 ListBox4->Items->Clear();
 i=0;
 do
 {
   if((is_First(difference_value[i])) && (is_First(value_plus_one[i])))
   {
    ListBox4->Items->Add(IntToStr(tab[i])+"  -  "+IntToStr(difference_value[i])+"  - "+IntToStr(value_plus_one[i]));
   }
   ++i;
 }while(i<max-1);


}
//---------------------------------------------------------------------------


Brak komentarzy:

Prześlij komentarz