C/C++ Algorithms and programs
wtorek, 2 września 2025
BeginThread() - C++ Builder
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
sobota, 12 lipca 2025
std::sort() - C++ Builder
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
niedziela, 2 marca 2025
class info_Printers - C++ Builder