private: // User declarations
int **tab;
int **first_tab;
int *f_tab;
int X;
int Y;
void __fastcall Sort(int *tab, int size_tab);
bool __fastcall is_First(int value);
void __fastcall init_First();
void __fastcall random_Values();
void __fastcall sorting_Values();
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
int i;
tab=new int *[size];
first_tab=new int *[size];
f_tab=new int[size*size];
for(i=0;i<size;i++)
{
tab[i]=new int[size];
first_tab[i]=new int[size];
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
int i;
for(i=0;i<size;i++)
{
delete [] first_tab[i];
delete [] tab[i];
}
delete [] *first_tab;
delete [] *tab;
delete f_tab;
}
void __fastcall TForm1::Sort(int *tab, int size_tab)
{
int i,j,k;
for(i=1;i<size_tab;i++)
{
j=i;
k=tab[j];
while((j>0) && (tab[j-1]>k))
{
tab[j]=tab[j-1];
j--;
}
tab[j]=k;
}
}
bool __fastcall TForm1::is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
void __fastcall TForm1::init_First()
{
int i,j,k;
i=0;
j=2;
do
{
if(is_First(j))
{
f_tab[i]=j;
++i;
}
++j;
}while(i<(size*size));
k=0;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
first_tab[i][j]=f_tab[k];
++k;
}
}
}
void __fastcall TForm1::random_Values()
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
X=rand()%size;
Y=rand()%size;
tab[i][j]=first_tab[X][Y];
}
}
}
void __fastcall TForm1::sorting_Values()
{
int i,j;
for(i=0;i<size;i++)
{
Sort(tab[i],size);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
srand(time(NULL));
init_First();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i,j;
random_Values();
sorting_Values();
for(i=0;i<size;i++)
for(j=0;j<size;j++)
StringGrid1->Cells[i][j]=IntToStr(tab[j][i]);
fstream test_file("test.txt",ios::out);
if(test_file.good())
{
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
test_file<<tab[i][j]<<", ";
test_file.flush();
}
}
}
test_file.close();
}
Brak komentarzy:
Prześlij komentarz