środa, 28 czerwca 2017

Manipulation of data in dynamic tables (C++ Builder)




private: // User declarations
int *table_first_Value,*min1,*min2,*max1,*max2,*sum1,*sum2;
float *dif1,*dif2,*dif_m1,*dif_m2,*aver1,*aver2;
int **table1;
int **table2;
bool __fastcall is_First(int value);
void __fastcall init_Table();
void __fastcall delete_Table();
void __fastcall init_Random();
int __fastcall sum_Table(int *tab, int size);
void __fastcall sort_Bubble(int *table, int size) ;
float __fastcall average_Table(int *tab, int size);
float __fastcall average_MinMax(int *tab, int size);
int __fastcall max_Table(int *tab, int size);
int __fastcall min_Table(int *tab, int size);
float __fastcall difference_Average(int *tab, int size);


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::init_Random()
{
srand(time(NULL));
}

int __fastcall TForm1::sum_Table(int *tab, int size)
{
int i,sum;
sum=0;
for(i=0;i<size;i++)
sum+=tab[i];
return sum;
}
void __fastcall TForm1::sort_Bubble(int *table, int size)
{
int i,j,temporary;
for(i=1;i<size;i++)
for(j=size-1;j>=i;j--)
if(table[j]<table[j-1])
{
temporary=table[j-1];
table[j-1]=table[j];
table[j]=temporary;
}

}
float __fastcall TForm1::average_Table(int *tab, int size)
{
float result;
result=(1.*sum_Table(tab,size))/(size*1.0);
return result;
}
int __fastcall TForm1::max_Table(int *tab, int size)
{
int i,max_t;
max_t=tab[0];
for(i=1;i<size;i++)
{
if(tab[i]>=max_t)
max_t=tab[i];
}
return max_t;

}
int __fastcall TForm1::min_Table(int *tab, int size)
{
int i,min_t;
min_t=tab[0];
for(i=1;i<size;i++)
{
if(tab[i]<min_t)
min_t=tab[i];

}
return min_t;
}
float __fastcall TForm1::average_MinMax(int *tab, int size)
{
float result;
result=1.0*(((1.0*max_Table(tab,size))+(1.0*min_Table(tab,size)))/2.0);
return result;
}

float __fastcall TForm1::difference_Average(int *tab, int size)
{
float result1,result2,result_end;
result1=average_MinMax(tab,size);
result2=average_Table(tab,size);
if(result1>=result2)
result_end=result1-result2;
else
result_end=result2-result1;
return result_end;
}
bool __fastcall TForm1::is_First(int value)
{
int sum,i;
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_Table()
{
table_first_Value=new int[100];
min1=new int[11];
min2=new int[11];
max1=new int[11];
max2=new int[11];
sum1=new int[11];
sum2=new int[11];
dif1=new float[11];
dif2=new float[11];
dif_m1=new float[11];
dif_m2=new float[11];
aver1=new float[11];
aver2=new float[11];
int i,j,k1,k2;
i=0;
j=2;
do
{
 if(is_First(j))
 {
 table_first_Value[i]=j;
 ++i;
 }
 ++j;

}while(i<100);
table1=new int *[11];
table2=new int *[11];
for(i=0;i<11;i++)
{
 table1[i]=new int[5];
 table2[i]=new int[5];

}
for(i=0;i<11;i++)
{
for(j=0;j<5;j++)
{
k1=rand()%100;
k2=rand()%100;
table1[i][j]=table_first_Value[k1];
table2[i][j]=table_first_Value[k2];
}
}
for(i=0;i<11;i++)
{
sort_Bubble(table1[i],5);
sort_Bubble(table2[i],5);
}
for(i=0;i<11;i++)
{
max1[i]=max_Table(table1[i],5);
max2[i]=max_Table(table2[i],5);
min1[i]=min_Table(table1[i],5);
min2[i]=min_Table(table2[i],5);
sum1[i]=sum_Table(table1[i],5);
sum2[i]=sum_Table(table2[i],5);
dif1[i]=average_Table(table1[i],5);
dif2[i]=average_Table(table2[i],5);
dif_m1[i]=average_MinMax(table1[i],5);
dif_m2[i]=average_MinMax(table2[i],5);
aver1[i]=difference_Average(table1[i],5);
        aver2[i]=difference_Average(table2[i],5);
}


}
void __fastcall TForm1::delete_Table()
{
int i;
delete table_first_Value;
delete min1;
delete min2;
delete max1;
delete max2;
delete sum1;
delete sum2;
delete dif1;
delete dif2;
delete dif_m1;
delete dif_m2;
delete aver1;
delete aver2;
for(i=0;i<11;i++)
{
delete []  table1[i];
delete []  table2[i];
}
delete [] *table1;
delete [] *table2;

}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Random();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
 init_Table();
 int i,j;
 for(i=0;i<5;i++)
 {
for(j=0;j<11;j++)
{
StringGrid1->Cells[i][j]=IntToStr(table1[j][i]);
StringGrid3->Cells[i][j]=IntToStr(table2[j][i]);


}
 }
 for(i=0;i<11;i++)
 {
StringGrid2->Cells[6][i]=IntToStr(i+1);
StringGrid4->Cells[6][i]=IntToStr(i+1);
StringGrid2->Cells[0][i]=IntToStr(max1[i]);
StringGrid4->Cells[0][i]=IntToStr(max2[i]);
StringGrid2->Cells[1][i]=IntToStr(min1[i]);
StringGrid4->Cells[1][i]=IntToStr(min2[i]);
StringGrid2->Cells[2][i]=IntToStr(sum1[i]);
StringGrid4->Cells[2][i]=IntToStr(sum2[i]);
StringGrid2->Cells[3][i]=FloatToStr(dif1[i]);
StringGrid4->Cells[3][i]=FloatToStr(dif2[i]);
StringGrid2->Cells[4][i]=FloatToStr(dif_m1[i]);
StringGrid4->Cells[4][i]=FloatToStr(dif_m2[i]);
StringGrid2->Cells[5][i]=FloatToStr(aver1[i]);
StringGrid4->Cells[5][i]=FloatToStr(aver2[i]);
 }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
 delete_Table();
 Close();
}
//---------------------------------------------------------------------------

Brak komentarzy:

Prześlij komentarz