piątek, 6 stycznia 2017

Operations on first numbers and tables. C++ Builder.

The algorithm primitive and time-consuming.



private: // User declarations
int __fastcall init_Random();
int __fastcall sum_Table(int *table, int size);
bool __fastcall is_First(int x);
int *a_table,*b_table,*c_table;
int *a_small_table, *b_small_table, *c_small_table;
int *a_index_table, *b_index_table, *c_index_table;
int *my_testing_table;
int *temporary_index;
int random_value;
int long_value;
void __fastcall init_Table();
void __fastcall destroy_Table();

TForm1 *Form1;
 int __fastcall TForm1::init_Random()
 {
srand(time(NULL));
 }
 int __fastcall TForm1::sum_Table(int *table, int size)
 {
int i,sum;
sum=0;
for(i=0;i<size;i++)
 sum+=table[i];
return sum;
 }
 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_Table()
 {
a_table=new int[100];
b_table=new int[100];
c_table=new int[100];
a_small_table=new int [10];
b_small_table=new int [10];
c_small_table=new int [10];
a_index_table=new int [10];
b_index_table=new int [10];
c_index_table=new int [10];
my_testing_table=new int[30];
temporary_index=new int[30];

int i,j;
i=0;
j=1;
do
{
 if(is_First(j))
 {
 a_table[i]=b_table[i]=c_table[i]=j;
 ++i;
 ++j;
 }
 else
  ++j;

}while(i<100);

 }
 void __fastcall TForm1::destroy_Table()
 {
delete temporary_index;
delete c_table;
delete b_table;
delete a_table;
delete a_index_table;
delete b_index_table;
delete c_index_table;
delete a_small_table;
delete b_small_table;
delete c_small_table;
delete my_testing_table;
 }
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 destroy_Table();
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Random();
 init_Table();
 int i;
 i=rand()%25+75;
 random_value=a_table[i];
 long_value=3*random_value;
 Edit1->Text=IntToStr(random_value);
 Edit2->Text=IntToStr(random_value);
 Edit3->Text=IntToStr(random_value);
 Edit4->Text=IntToStr(long_value);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 ;
 int i,j,k,value;
 do
 {
 i=0;
 do
 {
   value=rand()%100;
   a_small_table[i]=a_table[value];
   a_index_table[i]=value;
   i++;
 }while(i<10);
 }while(sum_Table(a_small_table,10)!=random_value);

 do
 {
   i=0;
   do
   {
  value=rand()%100;
  b_small_table[i]=b_table[value];
  b_index_table[i]=value;
  i++;
   }while(i<10);
 }while(sum_Table(b_small_table,10)!=random_value);

 do
 {
   i=0;
   do
   {
 value=rand()%100;
 c_small_table[i]=c_table[value];
 c_index_table[i]=value;
 i++;
   }while(i<10);
 }while(sum_Table(c_small_table,10)!=random_value);
 for(i=0;i<10;i++)
 {
  ListBox1->Items->Add(IntToStr(a_small_table[i]));
  ListBox2->Items->Add(IntToStr(b_small_table[i]));
  ListBox3->Items->Add(IntToStr(c_small_table[i]));
  StringGrid1->Cells[0][i]=IntToStr(a_index_table[i]);
  StringGrid2->Cells[0][i]=IntToStr(b_index_table[i]);
  StringGrid3->Cells[0][i]=IntToStr(c_index_table[i]);
 }
 for(i=0,j=10,k=20;i<10;i++,j++,k++)
 {
my_testing_table[i]=a_small_table[i];
my_testing_table[j]=b_small_table[i];
my_testing_table[k]=c_small_table[i];
temporary_index[i]=a_index_table[i];
temporary_index[j]=b_index_table[i];
temporary_index[k]=c_index_table[i];
 }
 for(i=0;i<30;i++)
 {
StringGrid4->Cells[i][0]=IntToStr(temporary_index[i]);
StringGrid4->Cells[i][1]=IntToStr(my_testing_table[i]);
 }
}

Brak komentarzy:

Prześlij komentarz