czwartek, 27 stycznia 2022

Special sum function (C++ Builder)

 





__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 srand(time(NULL));
}
//---------------------------------------------------------------------------
int* __fastcall TForm1::rand_Tab2(int *tab, int size_Tab, int find_Sum)
{
int i,sum;
int *result_Tab=new int [size_Tab];
for(i=0;i<size_Tab;i++)
result_Tab[i]=tab[i];
sum=0;
do
{
i=0;
do

{
result_Tab[i]=rand()%20;
         sum+=result_Tab[i];
         ++i;
}while (i<size_Tab);
if(sum!=find_Sum)
sum=0;

}while(sum!=find_Sum);

return result_Tab;

}
void __fastcall TForm1::Sort(int *tab, int s)
{
int i,j,k;
for(i=1;i<s;i++)
{
j=i;
k=tab[j];
while((j>0) && (tab[j-1]>k))
{
tab[j]=tab[j-1];
j--;
}
tab[j]=k;
}
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int i,j,k,SUM;
 k=20;
 SUM=StrToInt(Edit1->Text);
 int **TAB=new int*[k];
 for(i=0;i<k;i++)
  TAB[i]=new int[k];
 for(i=0;i<k;i++)
 {
  TAB[i]=rand_Tab2(TAB[i],k,SUM);
 }
 for(i=0;i<k;i++)
  Sort(TAB[i],k);
 for(i=0;i<k;i++)
 {
  for(j=0;j<k;j++)
  {
   StringGrid1->Cells[j][i]=IntToStr(TAB[i][j]);
  }
 }
 for(i=0;i<k;i++)
  delete [] TAB[i];
 delete [] TAB;

}

środa, 12 stycznia 2022

this - example component TListBox - C++ Builder

 







TForm1 *Form1;
int number=4;
TListBox *newList[4];
TColor colors[4]={clBlue,clYellow,clGreen,clRed};
TColor penCol[4]={clWhite,clBlack,clYellow,clBlue};
AnsiString mFont[4]={"Arial","Times","Alchemist","GE Glob"};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 srand(time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 int i,mLeft,mTop,mWidth,mHeight;
 mWidth=120;mHeight=240;
 mTop=Form1->Height/4;
 mWidth=Form1->Width/4;
 AnsiString a="ListBoxA";
 for(i=0;i<number;i++)
 {
  newList[i]=new TListBox(this);
  newList[i]->Name=a+IntToStr(i+1);
  newList[i]->Width=mWidth;
  newList[i]->Height=mHeight;
  newList[i]->Left=((i+1)*mWidth)-mWidth;
  newList[i]->Top=mTop;
  newList[i]->Brush->Color=colors[i];
  newList[i]->Font->Color=penCol[i];
  newList[i]->Font->Size=15;
  newList[i]->Font->Name=mFont[i];
  newList[i]->Parent=this;




 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
 int i;
 for(i=0;i<number;i++)
 {
  delete newList[i];
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int i,j;
  for(i=0;i<number*2;i++)
  {
   for(j=0;j<number;j++)
   {
     newList[j]->Items->Add(IntToStr(i+1)+") - Random(10) - "+IntToStr(1+rand()%10));
   }
  }

}




niedziela, 9 stycznia 2022

Simple chart (C++ Builder)

 



void __fastcall TForm1::Button1Click(TObject *Sender)

{

 int pwidth,pheight,centrx,centry,rvalue,svalue,i,j;

 pwidth=PaintBox1->Width;

 pheight=PaintBox1->Height;

 centrx=pwidth/2;

 centry=pheight/2;

 rvalue=15;

 svalue=10;

 PaintBox1->Canvas->Brush->Color=clYellow;

 PaintBox1->Canvas->FillRect(PaintBox1->Canvas->ClipRect) ;

 PaintBox1->Canvas->Pen->Color=clBlack;

 PaintBox1->Canvas->MoveTo(centrx,0);

 PaintBox1->Canvas->LineTo(centrx,pheight);

 PaintBox1->Canvas->MoveTo(0,centry);

 PaintBox1->Canvas->LineTo(pwidth,centry);

 PaintBox1->Canvas->Pen->Color=clBlue;

 for(i=centrx+rvalue;i<pwidth+rvalue;i+=rvalue)

 {

  PaintBox1->Canvas->MoveTo(i,centry-svalue);

  PaintBox1->Canvas->LineTo(i,centry+svalue);

 }

 j=1;

 for(i=centry-rvalue;i>0;i-=rvalue)

 {

  PaintBox1->Canvas->MoveTo(centrx-svalue,i);

  PaintBox1->Canvas->LineTo(centrx+svalue,i);

  PaintBox1->Canvas->TextOut(centrx+(2*svalue),i-(svalue-1),IntToStr(j));

  ++j;

 }

 PaintBox1->Canvas->Pen->Color=clRed;

 for(i=centrx-rvalue;i>0;i-=rvalue)

 {

  PaintBox1->Canvas->MoveTo(i,centry-svalue);

  PaintBox1->Canvas->LineTo(i,centry+svalue);

 }

 j=1;

 for(i=centry+rvalue;i<pheight+rvalue;i+=rvalue)

 {

  PaintBox1->Canvas->MoveTo(centrx-svalue,i);

  PaintBox1->Canvas->LineTo(centrx+svalue,i);

  PaintBox1->Canvas->TextOut(i-(svalue-5),centry-(2*svalue),IntToStr(j));

  ++j;

 }

 PaintBox1->Canvas->Pen->Color=clGreen;

 PaintBox1->Canvas->MoveTo(centrx,0);

 PaintBox1->Canvas->LineTo(centrx-svalue,rvalue*2);

 PaintBox1->Canvas->MoveTo(centrx,0);

 PaintBox1->Canvas->LineTo(centrx+svalue,rvalue*2);

 PaintBox1->Canvas->MoveTo(pwidth,centry);

 PaintBox1->Canvas->LineTo(pwidth-(rvalue*2),centry-svalue);

 PaintBox1->Canvas->MoveTo(pwidth,centry);

 PaintBox1->Canvas->LineTo(pwidth-(rvalue*2),centry+svalue);


 PaintBox1->Canvas->TextOut(centrx-(3*rvalue),centry+rvalue,"(0,0)");


 PaintBox1->Canvas->Pen->Color=clGreen;

 PaintBox1->Canvas->MoveTo(centrx,centry);

 PaintBox1->Canvas->LineTo(pwidth,0);

 PaintBox1->Canvas->TextOut(centrx+(centrx/2)+rvalue,centry-(centry/2),"y=x");



}