sobota, 21 czerwca 2025

priority_queue - C++ Builder

 



#include <vcl.h>

#include <System.SysUtils.hpp>

#include <iostream>

#include <queue>

#include <tuple>

#pragma hdrstop


#include "Unit1.h"

using namespace std;

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

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


struct info_var_Data

{

float value;

UnicodeString name;

};


__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{


}

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

void __fastcall TForm1::Button1Click(TObject *Sender)

{


 float data_Values[8]={0.00,0.11,0.22,0.33,0.44,0.55,0.66,0.77};

 UnicodeString v_Names[8]={"VarEmty","varNull","varSmallint","varInteger","varSingle",

 "varDouble","varVurrency","varDate"};

 struct info_var_Data *v_Data=new struct info_var_Data[8];

 struct info_var_Data *temp_Values=new struct info_var_Data[8];

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

 {

v_Data[i].value=data_Values[i];

v_Data[i].name=v_Names[i];

 }


 using x_Date=pair<float,UnicodeString>;

 priority_queue<x_Date> temp;

 int temp_Index[8]={3,0,1,4,2,7,5,6,};

 initializer_list<x_Date> v8

 {

  {v_Data[temp_Index[0]].value,v_Data[temp_Index[0]].name},

  {v_Data[temp_Index[1]].value,v_Data[temp_Index[1]].name},

  {v_Data[temp_Index[2]].value,v_Data[temp_Index[2]].name},

  {v_Data[temp_Index[3]].value,v_Data[temp_Index[3]].name},

  {v_Data[temp_Index[4]].value,v_Data[temp_Index[4]].name},

  {v_Data[temp_Index[5]].value,v_Data[temp_Index[5]].name},

  {v_Data[temp_Index[6]].value,v_Data[temp_Index[6]].name},

  {v_Data[temp_Index[7]].value,v_Data[temp_Index[7]].name},

 };

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

 {

  temp_Values[i].value=v_Data[temp_Index[i]].value;

  temp_Values[i].name=v_Data[temp_Index[i]].name;

  StringGrid1->Cells[i][0]=FloatToStrF(temp_Values[i].value,ffGeneral,2,2);

  StringGrid1->Cells[i][1]=temp_Values[i].name;


 }

 for(const auto &it : v8)

 {

temp.push(it);

 }

 int j=0;

 while(!temp.empty())

 {

  StringGrid2->Cells[j][0]=FloatToStrF(temp.top().first,ffGeneral,2,2);

  StringGrid2->Cells[j][1]=temp.top().second;

  temp.pop();

  ++j;

 }

 delete [] v_Data;

 delete [] temp_Values;

}

czwartek, 19 czerwca 2025

Loading text files - C++ Builder

 



pFile.h

#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Vcl.FileCtrl.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
TLabel *Label1;
TFileListBox *FileListBox1;
TDirectoryListBox *DirectoryListBox1;
TDriveComboBox *DriveComboBox1;
TMemo *Memo1;
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FileListBox1Change(TObject *Sender);
private: // User declarations
void __fastcall init_Components();
void __fastcall Run();
public: // User declarations
DWORD size_Bytes;
HANDLE ident;
char buff[1024+1024+1024+1024];
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

pFile.cpp

#include <vcl.h>
#pragma hdrstop

#include "pFile.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
SECURITY_ATTRIBUTES s_a;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
  s_a={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Run()
{
int i;
char c='.';
memset(buff,0,sizeof(buff)-1);
ident=CreateFile(FileListBox1->FileName.c_str(),GENERIC_READ, FILE_SHARE_READ,
&s_a,OPEN_EXISTING,0,NULL);
if(ident!=INVALID_HANDLE_VALUE)
ReadFile(ident,&buff,sizeof(buff),&size_Bytes,NULL);
for(i=0;i<sizeof(buff)-1;i++)
if(buff[i]==NULL)
  buff[i]=c;
Memo1->Text=buff;
CloseHandle(ident);

}
void __fastcall TForm1::init_Components()
{
DirectoryListBox1->FileList=FileListBox1;
DriveComboBox1->DirList=DirectoryListBox1;

}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
 UnicodeString t,d;
 t=TimeToStr(Time());
 d=DateToStr(Date());
 Label1->Caption=t+"  "+d;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Components();
}





niedziela, 2 marca 2025

class info_Printers - C++ Builder



#ifndef pprinters1H
#define pprinters1H
//---------------------------------------------------------------------------

class info_Printers
{
private:
wchar_t name_Printer[128];
wchar_t driver[1024];
wchar_t port[1024];
unsigned int mode;
TListBox* list_Box;
public:
info_Printers(TListBox* l_Box);
void write_Printers();
};


#endif


#pragma hdrstop

#include "pprinters1.h"
#include <Printers.hpp>

//---------------------------------------------------------------------------
#pragma package(smart_init)
info_Printers::info_Printers(TListBox* l_Box)
{
Printer()->GetPrinter(name_Printer,driver,port,mode);
list_Box=l_Box;
list_Box->Items->Clear();

}
void info_Printers::write_Printers()
{
list_Box->Items->Add("All printers: \n");
list_Box->Items->AddStrings(Printer()->Printers);
list_Box->Items->Add("Default printer:");
        list_Box->Items->Add(UnicodeString(name_Printer));
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 info_Printers my_Printers(ListBox1);
 my_Printers.write_Printers();
}





 

sobota, 2 listopada 2024

Retrieving data from a file into C++ Builder components.



File: top_Population.txt (location in directory Project C++ Builder)

India 1450935791 17.78
Pakistan 251269164 3.08
China 1419321278 17.39
United_States 345426571 4.23
Nigeria 232679478 2.85
Indonesia 283487931 3.47





struct Date
{
char name_Country[64];
float population_Country;
float procent_World;
};

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 StringGrid1->Cells[0][0]="Country";
 StringGrid1->Cells[1][0]="Population";
 StringGrid1->Cells[2][0]="World Share";
 Button1->Caption="Open";
 Button2->Caption="Close";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 struct Date* population_Info=new struct Date[6];
 FILE* my_File=fopen("top_Population.txt","r");
 if(!my_File)
 {
ShowMessage("Can't open file!");
 }
 else
 {
  for(int i=0;i<6;i++)
  {
fscanf(my_File,"%s %f %f\n",population_Info[i].name_Country,
&population_Info[i].population_Country,&population_Info[i].procent_World);
  }
  fclose(my_File);
  for(int i=0;i<6;i++)
  {
StringGrid1->Cells[0][i+1]=UnicodeString(population_Info[i].name_Country);
StringGrid1->Cells[1][i+1]=FloatToStr(population_Info[i].population_Country);
StringGrid1->Cells[2][i+1]=FloatToStrF(population_Info[i].procent_World,ffGeneral,7,1)+"%";
  }

 }

 delete [] population_Info;
}


 

wtorek, 22 października 2024

Ascending and descending sorting - Example - class C++ Builder

 










New Unit:


File Class_Sorting.h


class multi_Sort

{

 private:

  int* tab_Date;

  int* min_Tab;

  int* max_Tab;

  int length;


 public:


multi_Sort(int size);

~multi_Sort();

void min_Sort(int *tab, int s);

void max_Sort(int* tab, int s);

void Load(int *tab);

int *min_Result();

     int *max_Result();

};

File Class_Sorting.cpp

#include "class_Sorting.h"

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

#pragma package(smart_init)

multi_Sort::multi_Sort(int size)

{

length=size;

tab_Date=new int[length];

min_Tab=new int[length];

max_Tab=new int[length];

}


multi_Sort::~multi_Sort()

{

delete max_Tab;

delete min_Tab;

delete tab_Date;

}


void multi_Sort::min_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 multi_Sort::max_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 multi_Sort::Load(int *tab)

{

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

{

tab_Date[i]=min_Tab[i]=max_Tab[i]=tab[i];

}


}

int* multi_Sort::min_Result()

{

  min_Sort(min_Tab,length);

  return min_Tab;

}

int* multi_Sort::max_Result()

{

  max_Sort(max_Tab,length);

  return max_Tab;

}

Main Project:


#include <vcl.h>

#include <stdlib.h>

#include <time.h>

#pragma hdrstop


#include "p_Example.h"

#include "class_Sorting.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

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

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

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

void __fastcall TForm1::FormCreate(TObject *Sender)

{

 srand(time(NULL));

 Button1->Caption="&RUN";

}

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

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  #define ssize 10

  #define mmax 100

  int j;

  int* main_Date=new int[ssize];

  int* min_Date=new int[ssize];

  int* max_Date=new int[ssize];

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

  {


  j=1+rand()%mmax;

  main_Date[i]=j;

  }

  multi_Sort my_Class(ssize);

  my_Class.Load(main_Date);

  min_Date=my_Class.min_Result();

  max_Date=my_Class.max_Result();

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

  {

  StringGrid1->Cells[i][0]=IntToStr(main_Date[i]);

  StringGrid1->Cells[i][1]=IntToStr(min_Date[i]);

      StringGrid1->Cells[i][2]=IntToStr(max_Date[i]);

  }

  delete max_Date;

  delete min_Date;

  delete main_Date;

}



wtorek, 27 sierpnia 2024

Scalar product - C++ Builder

 




__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
 srand(time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

 int i,j,k,width,height;
 width=5; height=2;
 float tab[width][height],r,d=10.0,scalar_Product;
 for(i=0;i<width;i++)
 {
for(j=0;j<height;j++)
{
k=1+rand()%100;
r=(1.00*k)/d;
tab[i][j]=r;
StringGrid1->Cells[i][j]=FloatToStrF(tab[i][j],ffGeneral,4,4);
}
 }

 std::valarray<float> x {tab[0][0],tab[0][1], tab[0][2], tab[0][3], tab[0][4]};
 std::valarray<float> y {tab[1][0],tab[1][1], tab[1][2], tab[1][3], tab[1][4]};
 scalar_Product=(x*y).sum();
 Label1->Caption="SCALAR PRODUCT = "+FloatToStr(scalar_Product);


}


czwartek, 22 sierpnia 2024

Negation and bit shift - C++ Builder


 

void __fastcall TForm1::write_Bits(char x, TListBox *Tl)

{

 unsigned char date[11]={1,2,4,8,16,32,64,128,256,512,1024};

 UnicodeString result;

 int i,y;

 result="";

 for(i=10;i>=0;i--)

 {

  y=(date[i] & x);

  if(y!=0)

   result+="1";

  else

   result+="0";

 }

 Tl->Items->Add(result);


}

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

void __fastcall TForm1::Button2Click(TObject *Sender)

{

 Close();

}

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

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 int a1,a2,a3,i;

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

 {

a1=i;

a2=~a1;

a3=a1<<1;

write_Bits(a1,ListBox1);

write_Bits(a2,ListBox2);

write_Bits(a3,ListBox3);

 }

}