niedziela, 25 września 2022

Reading/opening files with Windows API (C++ Builder)







 private: // User declarations

         DWORD temp_Size;

         HANDLE temp_Handle;

         bool __fastcall handle_True(HANDLE value);


TForm1 *Form1;


SECURITY_ATTRIBUTES sec_Atr={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{


}

//---------------------------------------------------------------------------


bool __fastcall TForm1::handle_True(HANDLE value)

{

 if(value!=INVALID_HANDLE_VALUE)

  return true;

 else

  return false;

}


void __fastcall TForm1::FormCreate(TObject *Sender)

{

 DirectoryListBox1->FileList=FileListBox1;

 DriveComboBox1->DirList=DirectoryListBox1;

 FileListBox1->FileEdit=Edit1;

 Label1->Caption=DateToStr(Date())+"   "+TimeToStr(Time());


}

//---------------------------------------------------------------------------

void __fastcall TForm1::FileListBox1Change(TObject *Sender)

{

 int a;

  char buff[64*64];

 memset(buff,0,sizeof(buff)-1);

 AnsiString temp_Text=FileListBox1->FileName;

 temp_Handle=CreateFile(temp_Text.c_str(),GENERIC_READ,FILE_SHARE_READ,&sec_Atr,

 OPEN_EXISTING,0,NULL);

 if(handle_True(temp_Handle))

  ReadFile(temp_Handle,&buff,sizeof(buff),&temp_Size,NULL);

 Memo1->Text=buff;

 CloseHandle(temp_Handle);


}

poniedziałek, 19 września 2022

Connecting to Word and Excel (C++ Builder)


 

private: // User declarations


        Variant m_Excel;

        Variant m_Word;


TForm1 *Form1;


PropertySet to_Word("Visible");

PropertySet to_Excel("Visible");

Procedure close_Word("Quit");

Procedure close_Excel("Quit");


//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

 try

 {

  m_Excel=Variant::CreateObject("Excel.Application");

  to_Excel<<true;

  m_Excel.Exec(to_Excel);

 }

 catch(...)

 {

  ShowMessage("i can't open. Excel is not installed!");

 }

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 try

 {

  m_Word=Variant::CreateObject("Word.Application");

  to_Word<<true;

  m_Word.Exec(to_Word);

 }

 catch(...)

 {

  ShowMessage("I can't open. Word is not installed!");

 }

}


//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)

{

 try

 {

  close_Excel<<false;

  m_Excel.Exec(close_Excel);

  m_Excel=Unassigned;

 }

 catch(...)

 {

  ShowMessage("Excel is not installed!");

 }

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)

{

 try

 {

  close_Word<<false;

  m_Word.Exec(close_Word);

  m_Word=Unassigned;


 }

 catch(...)

 {

  ShowMessage("Word is not installed!");

 }

}

niedziela, 11 września 2022

Voronoi shapes (C++ Builder and Lazarus)




C++ Builder 

private: // User declarations

       double __fastcall write_Distance(int a, int b, int c, int d);
       int r,g,b;
       int date_X[10], date_Y[10];

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

 srand(time(NULL));

}

double __fastcall TForm1::write_Distance(int a, int b, int c, int d)

{

 double result;

 result=sqrt((a-c)*(a-c)+(b-d)*(b-d));

 return result;

}

//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)

{

 TColor base_Color[10];

 int i,j,k,l,w,h;

 double d1,d2;

 j=20000+rand()%20000;

 d1=(1.00*j);

 w=PaintBox1->Width;

 h=PaintBox1->Height;

 for(i=0;i<10;i++)

 {

  r=rand()%255;

  g=rand()%255;

  b=rand()%255;

  base_Color[i]=RGB(r,g,b);

  date_X[i]=rand()%w;

  date_Y[i]=rand()%h;

 }

 for(i=0;i<w;i++)

 {

  for(j=0;j<h;j++)

  {

   d2=d1;

   for(k=0;k<10;k++)

   {

    if(write_Distance(i,j,date_X[k],date_Y[k])<d2)

    {

     l=k;

     d2=write_Distance(i,j,date_X[k],date_Y[k]);

    }

   }

   PaintBox1->Canvas->Pixels[i][j]=base_Color[l];

  }

 }

}


Lazarus


function distance_P(a,b,c,d:Integer):Real;

var

dist: real;

begin

  dist:=Sqrt((a-c)*(a-c)+(b-d)*(b-d));

  distance_P:=dist;

end;   


procedure TForm1.FormCreate(Sender: TObject);

begin

    Randomize;

end;


procedure TForm1.Button1Click(Sender: TObject);

var

  i,j,k,l,x_Width,y_Height: Integer;

  base_Colors : array[0..10] of TColor;

  x_Date : array[0..10] of Integer;

  y_Date : array[0..10] of Integer;

  temp_Distance1, temp_Distance2 : Real;

begin

  i:=20000+random(20000);

  temp_Distance1:=(1.00*i);

  base_Colors[0]:=clLime;

  base_Colors[1]:=clYellow;

  base_Colors[2]:=clRed;

  base_Colors[3]:=clGreen;

  base_Colors[4]:=clBlue;

  base_Colors[5]:=clBlack;

  base_Colors[6]:=clWhite;

  base_Colors[7]:=clAqua;

  base_Colors[8]:=clGray;

  base_Colors[9]:=clPurple;

  base_Colors[10]:=clOlive;

  x_Width:=PaintBox1.Width;

  y_Height:=PaintBox1.Height;

  for i:=0 to 10 do

  begin

   x_Date[i]:=random(x_Width);

   y_Date[i]:=random(y_Height);

  end;

  for i:=0 to x_Width do

  begin

   for j:=0 to y_Height do

   begin

       temp_Distance2:=temp_Distance1;

       for k:=0 to 10 do

       begin

           if distance_P(i,j,x_Date[k],y_Date[k])<temp_Distance2 then

           begin

             l:=k;

             temp_Distance2:=distance_P(i,j,x_Date[k],y_Date[k]);


           end;

       end;

       PaintBox1.Canvas.Pixels[i,j]:=base_Colors[l];

   end;

  end;


end;


end.

              

 

środa, 27 lipca 2022

Polish lotteries (C++ Builder)

 



__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

 srand(time(NULL));

}

//---------------------------------------------------------------------------


void __fastcall TForm1::iSort(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 mini_Lotto[8][5];

 int lotto_Lotto[8][6];

 int lotto_Plus[8][6];

 int multi_Multi[8][20];

 int Kaskada[8][12];

 int i,j,k;

 int temp_Mini[8][42],temp_lotto_Lotto[8][49],temp_lotto_Plus[8][49],

 temp_multi_Multi[8][80],temp_Kaskada[8][24];


 for(i=0;i<8;i++)

  for(j=0;j<42;j++)

   temp_Mini[i][j]=0;

 for(i=0;i<8;i++)

 {

  for(j=0;j<49;j++)

  {

    temp_lotto_Lotto[i][j]=0;

    temp_lotto_Plus[i][j]=0;

  }

 }

 for(i=0;i<8;i++)

  for(j=0;j<80;j++)

   temp_multi_Multi[i][j]=0;


 for(i=0;i<8;i++)

  for(j=0;j<24;j++)

   temp_Kaskada[i][j]=0;



 //Mini Lotto 5/52


 i=0;

 do

 {

   for(j=0;j<5;j++)

   {

     do

     {

      k=rand()%42;

     }while(temp_Mini[i][k]!=0);

     temp_Mini[i][k]=1;

     mini_Lotto[i][j]=k+1;

   }

   ++i;

 }while(i<8);

 for(i=0;i<8;i++)

  iSort(mini_Lotto[i],6);

 for(i=0;i<5;i++)

  for(j=0;j<8;j++)

   StringGrid1->Cells[i][j]=IntToStr(mini_Lotto[j][i]);


//Lotto (6/49)


 i=0;

 do

 {

   for(j=0;j<6;j++)

   {

     do

     {

      k=rand()%49;

     }while(temp_lotto_Lotto[i][k]!=0);

     temp_lotto_Lotto[i][k]=1;

     lotto_Lotto[i][j]=k+1;

   }

   ++i;

 }while(i<8);

 for(i=0;i<8;i++)

  iSort(lotto_Lotto[i],6);

 for(i=0;i<6;i++)

  for(j=0;j<8;j++)

   StringGrid2->Cells[i][j]=IntToStr(lotto_Lotto[j][i]);


//Lotto Plus (6/49)


 i=0;

 do

 {

   for(j=0;j<6;j++)

   {

     do

     {

      k=rand()%49;

     }while(temp_lotto_Plus[i][k]!=0);

     temp_lotto_Plus[i][k]=1;

     lotto_Plus[i][j]=k+1;

   }

   ++i;

 }while(i<8);

 for(i=0;i<8;i++)

  iSort(lotto_Plus[i],6);

 for(i=0;i<6;i++)

  for(j=0;j<8;j++)

   StringGrid3->Cells[i][j]=IntToStr(lotto_Plus[j][i]);

//Multi Multi (20/80)


 i=0;

 do

 {

   for(j=0;j<20;j++)

   {

     do

     {

      k=rand()%80;

     }while(temp_multi_Multi[i][k]!=0);

     temp_multi_Multi[i][k]=1;

     multi_Multi[i][j]=k+1;

   }

   ++i;

 }while(i<8);

 for(i=0;i<8;i++)

  iSort(multi_Multi[i],20);

 for(i=0;i<20;i++)

  for(j=0;j<8;j++)

   StringGrid4->Cells[i][j]=IntToStr(multi_Multi[j][i]);


//Kaskada(12/24)


 i=0;

 do

 {

   for(j=0;j<12;j++)

   {

     do

     {

      k=rand()%24;

     }while(temp_Kaskada[i][k]!=0);

     temp_Kaskada[i][k]=1;

     Kaskada[i][j]=k+1;

   }

   ++i;

 }while(i<8);

 for(i=0;i<8;i++)

  iSort(Kaskada[i],12);

 for(i=0;i<12;i++)

  for(j=0;j<8;j++)

   StringGrid5->Cells[i][j]=IntToStr(Kaskada[j][i]);





}

wtorek, 7 czerwca 2022

Sum of sines (C++)

 #include <iostream>

#include <math.h>

#include <stdlib.h>

#include <time.h>


#define ss 8

using namespace std;


double y1(double b, double c)

{

return sin(b)+sin(c);

}

double y2(double b, double c)

{

double r;

r=2*sin(0.5*(b+c))*cos(0.5*(c-b));

return r;

}

double date[ss]={15.00,30.0,45.00,60.00,90.00,120.00,150.00,180.00};


double a1[ss],a2[ss];



int main(int argc, char **argv)

{

int k1,k2;

srand(time(NULL));

    for(int i=0;i<ss;i++)

    {

k1=rand()%ss;k2=rand()%ss;

a1[i]=y1(date[k1],date[k2]);

a2[i]=y2(date[k1],date[k2]);

cout<<a1[i]<<" ---- "<<a2[i]<<endl;

}


return 0;

}


sobota, 23 kwietnia 2022

Trigonometric - continue (C++)

 #include <iostream>

#include <math.h>

#include <fstream>

#define a 8


using namespace std;



double date[a]={0.00,30.00,45.00,60.00,90.00,180.00,270.00,360.00};


double f_Rad(double value)

{

return (value*3.14159265359)/180.00;

}

double msin[a],mcos[a],mtg[a],mctg[a];


int main(int argc, char **argv)

{

int i;

for(i=0;i<a;i++)

{

msin[i]=sin(f_Rad(date[i]));

mcos[i]=cos(f_Rad(date[i]));

mtg[i]=msin[i]/mcos[i];

mctg[i]=1.00/mtg[i];

}

fstream file("table.txt",ios::out);

if(file.good())

{

file<<"sin: 0,30,45,60,90,180,270,360\n";

for(i=0;i<a;i++)

{

file<<msin[i]<<"  ";

}

file<<"\n";

file<<"cos: 0,30,45,60,90,180,270,360\n";

for(i=0;i<a;i++)

{

file<<mcos[i]<<"  ";

}

file<<"\n";

file<<"tg: 0,30,45,60,90,180,270,360\n";

for(i=0;i<a;i++)

{

file<<mtg[i]<<"  ";

}

file<<"\n";

file<<"ctg: 0,30,45,60,90,180,270,360\n";

for(i=0;i<a;i++)

{

file<<mctg[i]<<"  ";

}

file<<"\n";

file.close();

}

return 0;

}


czwartek, 21 kwietnia 2022

Trigonometrict function (C++ Builder)

 


void __fastcall TForm1::TrackBar1Change(TObject *Sender)

{

 int x;

 double y,y1;

 x=TrackBar1->Position;

 Label1->Caption=IntToStr(x);

 y=(1.00*x);

 y1=y*(M_PI/180.00);

 Label2->Caption="sin: "+FloatToStrF(sin(y),ffGeneral,2,2);

 Label3->Caption="cos: "+FloatToStrF(cos(y),ffGeneral,2,2);

 Label4->Caption="tg: "+FloatToStrF(sin(y)/cos(y),ffGeneral,2,2);

 Label5->Caption="ctg:"+FloatToStrF(1.00/(sin(y)/cos(y)),ffGeneral,2,2);

 Label6->Caption="sin: (rad)  "+FloatToStrF(sin(y1),ffGeneral,2,2);

 Label7->Caption="cos: (rad)  "+FloatToStrF(cos(y1),ffGeneral,2,2);

 Label8->Caption="tg:  (rad)  "+FloatToStrF(sin(y1)/cos(y1),ffGeneral,2,2);

 Label9->Caption="ctg: (rad)  "+FloatToStrF(1.00/(sin(y1)/cos(y1)),ffGeneral,2,2);



}