niedziela, 4 marca 2018

StringGrid and tables to use distance calculations (C++ Builder)

Sometimes you need to create a timetable, or calculate vectors. The tables ares useful. 




private: // User declarations
int **table1,**table2,*value_x,*value_y;
void __fastcall init_Tables();
void __fastcall delete_Tables();
void __fastcall init_Random();


void __fastcall TForm1::init_Tables()
{
int i;
value_x=new int[20];
value_y=new int[20];
table1=new int *[20];
table2=new int *[20];
for(i=0;i<20;i++)
{
table1[i]=new int[20];
table2[i]=new int[20];
}
}
void __fastcall TForm1::delete_Tables()
{
int i;
for(i=0;i<20;i++)
{
delete [] table1[i];
delete [] table2[i];
}
delete [] *table1;
delete [] *table2;
delete value_x;
delete value_y;
}

void __fastcall TForm1::init_Random()
{
  srand(time(NULL));
}



void __fastcall TForm1::Button2Click(TObject *Sender)
{
 delete_Tables();
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Tables();
 init_Random();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int i,j,k;
 value_x[0]=0;
 value_y[0]=0;
 for(i=1;i<20;i++)
 {
value_x[i]=1+rand()%20;
value_y[i]=value_x[i];
 }
 for(i=0;i<20;i++)
 {
StringGrid1->Cells[i][0]=IntToStr(value_x[i]);
StringGrid1->Cells[0][i]=IntToStr(value_y[i]);
StringGrid2->Cells[i][0]=IntToStr(value_x[i]);
StringGrid2->Cells[0][i]=IntToStr(value_y[i]);
table1[i][0]=value_x[i];
table1[0][i]=value_y[i];
table2[i][0]=value_x[i];
table2[0][i]=value_y[i];

 }
 for(i=1;i<20;i++)
 {
for(j=1;j<20;j++)
{
if(i==j)
{
 table1[i][j]=0;
 table2[i][j]=0;
}
else
{
table1[i][j]=table1[i][0]+table1[0][j];
table2[i][j]=table2[i][0]*table2[0][j];
}
}
 }

 for(i=0;i<20;i++)
 {
for(j=0;j<20;j++)
{
StringGrid1->Cells[i][j]=IntToStr(table1[i][j]);
StringGrid2->Cells[i][j]=IntToStr(table2[i][j]);
}
 }

}

Brak komentarzy:

Prześlij komentarz