czwartek, 16 października 2025

Discrete knapsack problem - C++ Builder

 



void __fastcall TForm1::alg_Greedy(float a, float x[3], float y[3], float z[3])
{
double temp=a;
const int k=3;
int i;
for(i=0;i<k;i++)
{
if(x[i]>temp)
break;
z[i]=1;
temp-=x[i];
if(i<k)
z[i]=temp/x[i];
}
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{

 Edit1->Text="";
 Edit2->Text="";
 Edit3->Text="";
 Edit4->Text="";
 Edit5->Text="";
 Edit6->Text="";
 Edit7->Text="";
 Edit8->Text="";
 Edit9->Text="";
 Edit10->Text="";
 Label1->Caption="Size = ";
 Label2->Caption="x[0]";
 Label3->Caption="x[1]";
 Label4->Caption="x[2]";
 Label5->Caption="y[0]";
 Label6->Caption="y[1]";
 Label7->Caption="y[2]";
 Label8->Caption="z[0]";
 Label9->Caption="z[1]";
 Label10->Caption="z[2]";
 Button1->Caption="&RUN";

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ListBox1->Items->Clear();
 const int k=3; int i;
 float Size, Sum;
 float x[k]; float y[k]; float z[k];
 Sum=0.0;
 Size=StrToFloat(Edit1->Text);
 x[0]=StrToFloat(Edit2->Text);
 x[1]=StrToFloat(Edit2->Text);
 x[2]=StrToFloat(Edit4->Text);
 y[0]=StrToFloat(Edit5->Text);
 y[1]=StrToFloat(Edit6->Text);
 y[2]=StrToFloat(Edit7->Text);
 z[0]=StrToFloat(Edit8->Text);
 z[1]=StrToFloat(Edit9->Text);
 z[2]=StrToFloat(Edit10->Text);
 alg_Greedy(Size,x,y,z);
 for(i=0;i<k;Sum+=z[i]*y[i],i++)
 {
ListBox1->Items->Add(IntToStr(i)+")  "+FloatToStr(x[i])+"   "+FloatToStr(y[i])+"   "+
FloatToStr(z[i])+"    \n");
 }
 Label11->Caption="Total: "+FloatToStr(Sum);

}


niedziela, 5 października 2025

CreateThread, SuspendThread, ResumeThread - C++ Builder



#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE ha;
TColor draw_Color(int c)
{
TColor value;
switch(c)
{
case 0: value=clBlue; break;
case 1: value=clRed; break;
case 2: value=clGreen; break;
case 3: value=clYellow; break;
case 4: value=clBlack; break;
case 5: value=clTeal; break;
case 6: value=clPurple; break;
case 7: value=clLime;break;
case 8: value=clOlive; break;
case 9: value=clMaroon;break;
default: value=clWhite;break;
}

return value;
}

DWORD WINAPI TProc01(void* pr)
{
int ma=3;
int cl1;
int cl2;
int x_min,y_min,x_0,y_0,x_max,y_max,shape;
HANDLE MainWnd(pr);
x_min=y_min=ma;
x_max=Form1->Width-ma;
y_max=Form1->Height-ma;
x_0=Form1->Width/2;
y_0=Form1->Height/2;
int a1,a2,a3,a4;
while(true) {
shape=rand()%3;
if(shape==0)
{
cl1=rand()%10;
cl2=rand()%10;
Form1->Canvas->Pen->Color=draw_Color(cl1);
Form1->Canvas->Brush->Color=draw_Color(cl2);
a1=rand()%x_0+x_min;
a2=rand()%y_0+y_min;
a3=rand()%x_0+x_0-ma;
a4=rand()%y_0+y_0-ma;
Form1->Canvas->MoveTo(a1,a2);
Form1->Canvas->LineTo(a3,a4);
}
else if(shape==1)
{
cl1=rand()%10;
cl2=rand()%10;
Form1->Canvas->Pen->Color=draw_Color(cl1);
Form1->Canvas->Brush->Color=draw_Color(cl2);
a1=rand()%x_0+x_min;
a2=rand()%y_0+y_min;
a3=rand()%x_0+x_0-ma;
a4=rand()%y_0+y_0-ma;
Form1->Canvas->Ellipse(a1,a2,a3,a4);
}
else
{
cl1=rand()%10;
cl2=rand()%10;
Form1->Canvas->Pen->Color=draw_Color(cl1);
Form1->Canvas->Brush->Color=draw_Color(cl2);
a1=rand()%x_0+x_min;
a2=rand()%y_0+y_min;
a3=rand()%x_0+x_0-ma;
a4=rand()%y_0+y_0-ma;
Form1->Canvas->Rectangle(a1,a2,a3,a4);
}
}



return 0;


}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

 DWORD dw;
 ha=CreateThread(NULL,0,TProc01,this,CREATE_SUSPENDED,&dw);
 srand(time(NULL));
 if(!ha)
 {
ShowMessage("The thread could not be started");
Application->Terminate();
 }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ResumeThread(ha);
 Button1->Enabled=false;
 Button2->Enabled=true;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 SuspendThread(ha);
 Button1->Enabled=true;
 Button2->Enabled=false;

}


 

wtorek, 2 września 2025

BeginThread() - C++ Builder


#include <vcl.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include <windows.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
HANDLE hh=NULL;
UINT id;
LONG thr=0;
int m_Date[1000];


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

bool __fastcall TForm1::is_First(int value)
{
int i,sum;
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_Date()
{
int i,j,k;
i=0;
j=1;
do
{
if(is_First(j))
{
m_Date[i]=j;
++i;
}
++j;
}while(i<1000);
}
int __fastcall random_Values(LPVOID value)
{
  int i,j;
  for(;;)
  {
  i=rand()%1000;
  j=m_Date[i];
  Form1->ListBox1->Items->Add(IntToStr(i+1)+")  - "+IntToStr(j));
  WaitForSingleObject((LPVOID)thr,1000);
  }
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
 srand(time(NULL));
 init_Date();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Button1->Caption="&Start";
 Button2->Caption="&Suspend";
 Button3->Caption="&Resume";
 Button4->Caption="&Terminate";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 thr=BeginThread(&s_a,4096,random_Values,this,CREATE_SUSPENDED,id);
 if(thr==(int)INVALID_HANDLE_VALUE)
 {
ShowMessage("ERROR! ");
 }
 else
 {
ResumeThread((LPVOID)thr);
hh=CreateEvent(NULL,FALSE,FALSE,NULL);
if(hh)
{
WaitForSingleObject(hh,1000);
CloseHandle(hh);
Button1->Enabled=FALSE;
hh=NULL;
}
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
  CloseHandle((LPVOID)hh);
  Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 if(hh)
 SuspendThread(hh);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 if(hh)
 ResumeThread(hh);
}




 

czwartek, 7 sierpnia 2025

Using a specific shuffle function in a class. C++ example (g++)

 #include <iostream>

#include <vector>

#include <fstream>

#include <algorithm>

#include <string>

#include <random>


using namespace std;


class rs_with_Shuffle

{

    private:

        int x,y,z;


        string file_Name;

        void i_Sort(int* tab, int s);

    public:

        int **tab;

        rs_with_Shuffle(int x, int y, int z, string fn);

        ~rs_with_Shuffle();

        void run_Rand();

        void write_Tab();

        void Save();



};

rs_with_Shuffle::rs_with_Shuffle(int a, int b, int c, string fn)

{

  x=a;

  y=b;

  z=c;

  file_Name=fn;


  tab=new int* [x];

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

    tab[i]=new int[y];

}

rs_with_Shuffle::~rs_with_Shuffle()

{

    for(int i;i<x;i++)

    {

        delete [] tab[i];

        delete [] tab;

    }

}

void rs_with_Shuffle::i_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 rs_with_Shuffle::run_Rand()

{

  std::random_device rd;

  std::mt19937 gen(rd());

  vector <int> all;

  int x1=1;

  int y1=z;

   for(int i=x1;i<y1;i++)

    all.push_back(i);

    std::shuffle(all.begin(),all.end(),gen);

   x1=0;

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

   {

    for(int j=0;j<y;j++)

    {

     tab[i][j]=all[x1];

     ++x1;

    }

   }

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

        i_Sort(tab[i],y);


}

void rs_with_Shuffle::write_Tab()

{

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

    {

        for(int j=0;j<y;j++)

        {

            cout<<tab[i][j]<<"  ";

        }

        cout<<"\n";

    }

}

void rs_with_Shuffle::Save()

{


    fstream file(file_Name,ios::out);

    if(file.good())

    {

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

    {

        for(int j=0;j<y;j++)

        {

            file<<tab[i][j]<<"  ";

            file.flush();

        }

        file<<"\n";



    }

    }

}

int main()

{

    rs_with_Shuffle my_Shuffle(4,10,99,"date.txt");

    my_Shuffle.run_Rand();

    my_Shuffle.write_Tab();

    my_Shuffle.Save();

    return 0;

}


niedziela, 20 lipca 2025

My class info_File - C++ Builder

 




 m_File.h

#ifndef m_FileH
#define m_FileH
//---------------------------------------------------------------------------
class info_File
{
 private:

 UnicodeString Name;
 UnicodeString Size;
 UnicodeString Modification;
 UnicodeString Attribute;
public:
 TFileListBox *TL;
 TListBox *TB;
 TSearchRec t_Rec;
 TStrings* i_File;
 info_File(TFileListBox *a1, TListBox *b1);
 void Load();
 void Run();
 ~info_File();

};
#endif

m_File.cpp


#pragma hdrstop
#include <vcl.h>
#include <Vcl.FileCtrl.hpp>
#include <Vcl.StdCtrls.hpp>
#include <System.Classes.hpp>
#include "m_File.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
info_File::info_File(TFileListBox *a1, TListBox *b1)
{
UnicodeString temp;
TL=a1;
TB=b1;
temp=TL->FileName;
if(FindFirst(temp,faAnyFile,t_Rec)!=0)
{
return;
}
FindClose(t_Rec);
i_File=new TStringList();


}
void info_File::Load()
{
   Name=(UnicodeString)t_Rec.Name;
   Size=(UnicodeString)t_Rec.Size;
   Modification=(UnicodeString)DateTimeToStr(FileDateToDateTime(t_Rec.Time));
   Attribute=IntToStr(t_Rec.Attr);
   i_File->Add("Name: "+ Name);
   i_File->Add("Size: "+Size);
   i_File->Add("Date last modification: "+Modification);
   i_File->Add("Attribute: "+Attribute);


}
void info_File::Run()
{
   TB->Items->Clear();
   TB->Items->Assign(i_File);


}
info_File::~info_File()
{
    delete i_File;
}



Project: Unit1.cpp

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "m_File.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 DirectoryListBox1->FileList=FileListBox1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FileListBox1Click(TObject *Sender)
{
 info_File Date(FileListBox1,ListBox1);
 Date.Load();
 Date.Run();
}



sobota, 12 lipca 2025

std::sort() - C++ Builder


#include <vcl.h>
#include <algorithm>
#include <random>
#include <vector>

#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Button1->Caption="&RUN";
 Button2->Caption="&CLOSE";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i,j,k;
 struct Point
 {
int x;
int y;
 };
 std::vector<Point> date;
 std::random_device r1;
 std::mt19937 gen(r1());
 std::uniform_int_distribution<>dist(1,25);
 for(i=0;i<5;i++)
 {
date.push_back({dist(gen),dist(gen)});
 }
 j=0;
 for(const auto& temp1 : date)
 {
StringGrid1->Cells[j][0]=IntToStr(temp1.x);
StringGrid1->Cells[j][1]=IntToStr(temp1.y);
++j;
 }
 std::sort(begin(date),end(date),[] (const Point &v1, const Point &v2)
 {
return v1.y<v2.y;
 });
 j=0;
 for(const auto& temp2 : date)
 {
StringGrid2->Cells[j][0]=IntToStr(temp2.x);
StringGrid2->Cells[j][1]=IntToStr(temp2.y);
++j;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}


 

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();
}