niedziela, 5 listopada 2023

std::tuple C++ Builder


void __fastcall TForm1::info_File(UnicodeString name, int size, UnicodeString date_Modification)
{
Edit1->Text=name;
Edit2->Text=IntToStr(size);
Edit3->Text=date_Modification;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
using INFO=tuple<UnicodeString,int,UnicodeString>;
TSearchRec info_Sys;
AnsiString date,name_File;
int bytes;
 name_File=Application->ExeName;
 if(FindFirst(name_File,faAnyFile,info_Sys)!=0)
 {
ShowMessage("I can't open file");
Close();
 }
 bytes=info_Sys.Size;
 date=DateTimeToStr(FileDateToDateTime(info_Sys.Time));
 INFO application_exe={name_File,bytes,date};
 const auto &[name,size,date_Modification]=application_exe;
 info_File(name,size,date_Modification);
}


 

wtorek, 31 października 2023

Directory /proc/ - g++

 
#include <iostream>
#include <fstream>

using namespace std;

void printf_File(string name_File)
{
    
    ifstream main_File(name_File.c_str());
    if(main_File.is_open())
    {
        char temp_String[10000];
        while(main_File.getline(temp_String,10000))
        {
            cout<<temp_String<<endl;
        }
    }
    else
    cout<<"I can't read - "<<name_File<<endl;
    
}

int main(int argc, char **argv)
{
    string proc_Info[3]={"/proc/version","/proc/cpuinfo","/proc/meminfo"};
    for(int i=0;i<3;i++)
    {
        printf_File(proc_Info[i]);
    }
    
    return 0;
}

czwartek, 14 września 2023

My class - save_Text - C++ Builder

 class save_Text

{

private:

char** buff;

int l_size;

    char* name;

ofstream my_File;

UnicodeString* date;

const int length=256;

public:

save_Text(char* file_Name,UnicodeString* text,int lines);

void Save();

~save_Text();


};

save_Text::save_Text(char* file_Name,UnicodeString* text,int lines)

{

 l_size=lines;

 name=file_Name;

 date=text;

 buff=new char*[l_size];

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

  buff[i]=new char[length];

}

void save_Text::Save()

{

 my_File.open(name);

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

 {

wcstombs(buff[i],date[i].w_str(),length);

my_File<<buff[i]<<"\n";

 }

 my_File.close();

}

save_Text::~save_Text()

{

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

delete [] buff[i];

    delete []buff;

}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
 UnicodeString safety_Tips[5];
 safety_Tips[0]="Report to Safety Cordinator";
 safety_Tips[1]="Stricty obey Safety Rules";
 safety_Tips[2]="Smoke only in designated areas";
 safety_Tips[3]="Keep Emergency Number near you";
 safety_Tips[4]="Do not take risks";

 save_Text Safety("safety.txt",safety_Tips,5);
 Safety.Save();

}

poniedziałek, 11 września 2023

Type Variant examples - C++ Builder

 


void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Variant x;
 TVarData(x).VDate=Now();
 x=TVarData(x).VDate;
 Label1->Caption=VarToStr(x);
 int i,j;
 Variant integer_Tab(OPENARRAY(int,(1,10,1,10)),varInteger);
 Variant double_Tab(OPENARRAY(int,(1,10,1,10)),varDouble);
 Variant currency_Tab(OPENARRAY(int,(1,10,1,10)),varCurrency);
 Variant date_Tab(OPENARRAY(int,(1,10,1,10)),varDate);
 double k=0.01;
 for(i=0;i<10;i++)
 {
for(j=0;j<10;j++)
{
integer_Tab.PutElement((i+1)*(j+1),i+1,j+1);
double_Tab.PutElement(k*(i+1*1.00)*(j+1*1.00),i+1,j+1);
currency_Tab.PutElement(k*(i+1*1.00)+(j+1*1.00),i+1,j+1);
         date_Tab.PutElement(x,i+1,j+1);

}
 }
 for(i=0;i<10;i++)
 {
for(j=0;j<10;j++)
{
StringGrid1->Cells[i][j]=integer_Tab.GetElement(i+1,j+1);
StringGrid2->Cells[i][j]=double_Tab.GetElement(i+1,j+1);
StringGrid3->Cells[i][j]=currency_Tab.GetElement(i+1,j+1);
         StringGrid4->Cells[i][j]=date_Tab.GetElement(i+1,j+1);

}
 }

}


niedziela, 23 lipca 2023

Time file information - class C++ Builder

 



class time_File
{
private:
TSearchRec modification;
TSystemTime d1,t1,d2,t2;
AnsiString FileN;
public:
time_File(AnsiString name);
AnsiString last_Modfication();
AnsiString create_File();
AnsiString last_Access();

};

time_File::time_File(AnsiString name)
{
FileN=name;
FindFirst(FileN,faAnyFile,modification);
FindClose(modification);
}
AnsiString time_File::last_Modfication()
{

return  (AnsiString) DateTimeToStr(FileDateToDateTime(modification.Time));
}

AnsiString time_File::create_File()
{
FileTimeToSystemTime(&(modification.FindData.ftCreationTime),&d1);
SystemTimeToTzSpecificLocalTime(NULL,&d1,&t1);
return (AnsiString) DateTimeToStr(SystemTimeToDateTime(t1));
}
AnsiString time_File::last_Access()
{
FileTimeToSystemTime(&(modification.FindData.ftLastAccessTime),&d2);
SystemTimeToTzSpecificLocalTime(NULL,&d2,&t2);
return (AnsiString) DateTimeToStr(SystemTimeToDateTime(t2));

}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  AnsiString name_Project=Application->ExeName;
  time_File info_File(name_Project);
  Label4->Caption=name_Project;
  Label1->Caption="Last modification: "+info_File.last_Modfication();
  Label2->Caption="Create time: "+info_File.create_File();
  Label3->Caption="Last access time: "+info_File.last_Access();
}








                

niedziela, 9 lipca 2023

Sorting - C++ example

 


#include <iostream>

#include <stdlib.h>

#include <time.h>

#define kk 12

using namespace std;


void i_Sort(int* tab, int ssize1)

{

int i,j,k;

for(i=1;i<ssize1;i++)

{

j=i;

k=tab[j];

while((j>0) && (tab[j-1]>k))

{

tab[j]=tab[j-1];

j--;

}

tab[j]=k;

}

}


void z_Sort(int* tab, int ssize1)

{

int i,j,k;

for(i=1;i<ssize1;i++)

{

j=i;

k=tab[j];

while((j>0) && (tab[j-1]<k))

{

tab[j]=tab[j-1];

j--;

}

tab[j]=k;

}

}

int *tab1,*tab2,*tab3;

int main(int argc, char **argv)

{

 int i,j;

 srand(time(NULL));

 tab1=new int[kk];

 tab2=new int[kk];

 tab3=new int[kk];

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

 {

j=rand()%100;

tab1[i]=j;

tab2[i]=tab1[i];

tab3[i]=tab2[i];

 }

 i_Sort(tab2,kk);

 z_Sort(tab3,kk);

 cout<<"Original array: \n";

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

 {

cout<<tab1[i]<<"   ";

 }

 cout<<endl;

 cout<<"Descending sort: \n";

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

 {

cout<<tab3[i]<<"   ";

 }

 cout<<endl;

 cout<<"Ascending sort: \n";

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

 {

cout<<tab2[i]<<"   ";

 }

 cout<<endl;

 delete tab3;

 delete tab2;

 delete tab1;

 

return 0;

}

niedziela, 4 czerwca 2023

Title Form Application - Visual C++

 



private:

Form^ my_T;


private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

my_T = gcnew Form;

int w, h,centr_X,centr_Y;

Label^ text1;

Label^ text2;

Button^ close_B = gcnew Button;

text1 = gcnew Label;

text2 = gcnew Label;

System::Drawing::Font^ font1=gcnew 

System::Drawing::Font(System::Drawing::FontFamily::GenericSansSerif, 18, FontStyle::Bold);

System::Drawing::Font^ font2 = gcnew

System::Drawing::Font(System::Drawing::FontFamily::GenericSansSerif, 22, FontStyle::Italic);

        my_T->Width = 601; my_T->Height = 301;

w = my_T->Width; h = my_T->Height;

centr_X = w / 2;

centr_Y = h / 2;

text1->Text = "Version 1.0";

text2->Text = "Author:  lukasz.wierzbicki2@gmail.com";

text1->Font = font1;

text2->Font = font2;

text1->AutoSize = true;

text2->AutoSize = true;

text1->Location = Point(centr_X-300, centr_Y-100);

text2->Location = Point(centr_X-300, (centr_Y-100) + 30);

close_B->Text = "CLOSE";

close_B->Location = Point(centr_X - 50, (centr_Y - 100) + 180);

close_B->AutoSize = true;

my_T->Text = "Title Aplication: ";

my_T->Controls->Add(text1);

my_T->Controls->Add(text2);

my_T->Controls->Add(close_B);

my_T->TopMost = true;

close_B->Click += gcnew EventHandler(this, &Form1::close_B_Click);

my_T->Show();

}

private: System::Void close_B_Click(System::Object^ sender, System::EventArgs^ e)

{

my_T->Close();

}

wtorek, 30 maja 2023

Dynamic array - Visual C++


 

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

int i, j;

 

array<System::Int64, 2>^ tabInt = gcnew array<System::Int64, 2>(10, 10);

array<System::Double, 2>^ tabDouble = gcnew array<System::Double, 2>(10, 10);

dataGridView1->ColumnCount = 10; dataGridView2->ColumnCount = 10;

dataGridView1->RowCount = 10; dataGridView2->RowCount = 10;

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

{

dataGridView1->Rows[i]->DefaultCellStyle->BackColor =

System::Drawing::Color::BlueViolet;

dataGridView2->Rows[i]->DefaultCellStyle->BackColor = System::Drawing::Color::Beige;

dataGridView1->Columns[i]->Width = 50;

dataGridView2->Columns[i]->Width = 50;

dataGridView1->Rows[i]->Height = 25;

dataGridView2->Rows[i]->Height = 25;

}

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

{

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

{

tabInt[i, j] = (i + 1) * (j + 1);

tabDouble[i, j] = sqrt(1.00 * tabInt[i, j]);

dataGridView1->Rows[i]->Cells[j]->Value = tabInt[i, j].ToString();

dataGridView2->Rows[i]->Cells[j]->Value = tabDouble[i, j].ToString();

}

}

}

piątek, 28 kwietnia 2023

Use of pointers (C)

 


#include <stdio.h>

#include <stdlib.h>

#include <time.h>


#define kk 10


int a;

void int_S(int* tab);

void double_S(double* tab);

void text_S(char* text);


void init()

{

srand(time(NULL));

a=1+rand()%kk;

}

char write_Letter(int b)

{

if(b%2==0)

return 'A';

else

return 'B';

}


void int_S(int* tab)

{

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

tab[i]=a;

}

void double_S(double* tab)

{

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

tab[i]=(1.00*a)/100.00;

}

void text_S(char* text)

{

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

text[i]=write_Letter(a);

}


int main(int argc, char **argv)

{

void* int_T; int* temp_I;

void* double_T; double* temp_D;

void* char_T; char* temp_C;

init();

int_T=malloc(kk*sizeof(int));

double_T=malloc(kk*sizeof(double));

char_T=malloc(kk*sizeof(char));

temp_I=(int*)int_T;

temp_D=(double*)double_T;

temp_C=(char*)char_T;

int_S(temp_I);

double_S(temp_D);

text_S(temp_C);

free(temp_C);

free(temp_D);

free(temp_I);

return 0;

}

czwartek, 6 kwietnia 2023

sendfile, malloc, free() - example C

 #include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/sendfile.h>


#define kk 10

#define mm kk*kk


int** tab;

double** tab_Sqrt;

double** tab_Aver;


struct stat new_Buff;


struct Point

{

int m_Int;

double m_Sqrt;

double m_Aver;

};


 int date_File,copy_date_File;

int main(int argc, char **argv)

{

int i,j,k,l;

FILE *fp;

off_t off1;

tab=malloc(kk*sizeof(int*));

tab_Sqrt=malloc(kk*sizeof(double*));

tab_Aver=malloc(kk*sizeof(double*));

off1=0;

struct Point* new_Point=malloc(mm*(sizeof(struct Point)));

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

{

tab[i]=malloc(kk*sizeof(int));

tab_Sqrt[i]=malloc(kk*sizeof(double));

tab_Aver[i]=malloc(kk*sizeof(double));

}

l=0;

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

{

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

{

k=(i+1)*(j+1);

tab[i][j]=k;

tab_Sqrt[i][j]=sqrt(1.00*k);

tab_Aver[i][j]=1.00/(1.00*k);

new_Point[l].m_Int=tab[i][j];

new_Point[l].m_Sqrt=tab_Sqrt[i][j];

new_Point[l].m_Aver=tab_Aver[i][j];

++l;

}

}

if ((fp=fopen("new_Point.txt", "w"))==NULL) 

{

     printf ("Can't open the file - new_Point.txt\n");

     exit(1);

     }

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

     {

fprintf(fp,"%d",new_Point[i].m_Int);

fprintf(fp,"%.2f",new_Point[i].m_Sqrt);

fprintf(fp,"%.2f",new_Point[i].m_Aver);

fprintf(fp,"\n");

}

fclose(fp);

date_File=open("new_Point.txt",O_RDONLY);

fstat(date_File,&new_Buff);

copy_date_File=open("copy_Point.txt",O_WRONLY | O_CREAT,new_Buff.st_mode);

sendfile(copy_date_File,date_File,off1,new_Buff.st_size);

close(copy_date_File);

close(date_File);

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

{

free(tab[i]);

free(tab_Sqrt[i]);

free(tab_Aver[i]);

}

free(new_Point);

free(tab_Aver);

free(tab_Sqrt);

free(tab);

return 0;

}


czwartek, 9 marca 2023

RGN_COPY, SetWindowRgn() - C++ Builder


 



private: // User declarations

        int form_Min(int x, int y);

        int r,margn;

        HRGN temp_RGN,temp_Ellipse;

        void init_Date();

public: // User declarations

        void rgn_Xor();

        void rgn_Or();

        void rgn_And();

        void rgn_Diff();

        __fastcall TForm1(TComponent* Owner);

};


int TForm1::form_Min(int x, int y)

{

 if(x<y)

  return x;

 else

  return y;

}

void TForm1::init_Date()

{

 temp_RGN=CreateRectRgn(0,0,Width,Height);

 r=form_Min(Height,Width);

 margn=Height-ClientHeight;

 temp_Ellipse=CreateEllipticRgn(0,margn,r,r);

}

void TForm1::rgn_Xor()

{

 init_Date();

 CombineRgn(temp_Ellipse,temp_RGN,temp_Ellipse,RGN_XOR);

 SetWindowRgn(Handle,temp_Ellipse,true);

}

void TForm1::rgn_Or()

{

 init_Date();

 CombineRgn(temp_Ellipse,temp_RGN,temp_Ellipse,RGN_OR);

 SetWindowRgn(Handle,temp_Ellipse,true);

}

void TForm1::rgn_And()

{

 init_Date();

 CombineRgn(temp_Ellipse,temp_RGN,temp_Ellipse,RGN_AND);

 SetWindowRgn(Handle,temp_Ellipse,true);

}

void TForm1::rgn_Diff()

{

 init_Date();

 CombineRgn(temp_Ellipse,temp_RGN,temp_Ellipse,RGN_DIFF);

 SetWindowRgn(Handle,temp_Ellipse,true);

}

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

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


void __fastcall TForm1::Button1Click(TObject *Sender)

{

 rgn_Xor();

}

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

void __fastcall TForm1::Button2Click(TObject *Sender)

{

  rgn_Or();

}

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

void __fastcall TForm1::Button3Click(TObject *Sender)

{

 rgn_And();

}

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

void __fastcall TForm1::Button4Click(TObject *Sender)

{

 rgn_Diff();

}


środa, 8 lutego 2023

Project files in ListBoxes - Visual C++


 #pragma endregion

private:

String^ char_to_String(char* value)

{

String^ result = gcnew String(value);

return result;

}

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

char* name1 = "Form1.h";

char* name2 = "Form1.cpp";

char* name3 = "pch.h";

char* name4 = "pch.cpp";

char* name5 = "Resource.h";

String^ temp;

char buff1[10000], buff2[10000], buff3[10000], buff4[10000], buff5[10000];

ifstream file1(name1);

if (file1.is_open())

{

while (file1.getline(buff1, 1000))

{

temp = char_to_String(buff1);

listBox1->Items->Add(temp);

}

file1.close();

}

ifstream file2(name2);

if (file2.is_open())

{

while (file2.getline(buff2, 1000))

{

temp = char_to_String(buff2);

listBox2->Items->Add(temp);

}

file2.close();

}

ifstream file3(name3);

if (file3.is_open())

{

while (file3.getline(buff3, 1000))

{

temp = char_to_String(buff3);

listBox3->Items->Add(temp);

}

file3.close();

}

ifstream file4(name4);

if (file4.is_open())

{

while (file4.getline(buff4, 1000))

{

temp = char_to_String(buff4);

listBox4->Items->Add(temp);

}

file4.close();

}

ifstream file5(name5);

if (file5.is_open())

{

while (file5.getline(buff5, 1000))

{

temp = char_to_String(buff5);

listBox5->Items->Add(temp);

}

file5.close();

}


}

czwartek, 2 lutego 2023

STL, shared_ptr, make shared - example g++ console

#include <iostream>

#include <string>

#include <memory>


using namespace std;


class CPU

{

public:

    string model_Name;

    int cache_Size;

    double cpu_MHz;


    CPU(string a1, int a2, double a3)

    {

        model_Name=a1;

        cache_Size=a2;

        cpu_MHz=a3;

        cout<<"START\n";

    }


    ~CPU()

    {

        cout<<"END\n";

    }


};


shared_ptr<double>shared_Double;

shared_ptr<int>shared_Int;

shared_ptr<string>shared_String;


int main()

{

 auto date_CPU(make_shared<CPU>("Pentium III",1024,900.65));

 shared_Double=shared_ptr<double>(date_CPU,&date_CPU->cpu_MHz);

 shared_Int=shared_ptr<int>(date_CPU,&date_CPU->cache_Size);

 shared_String=shared_ptr<string>(date_CPU,&date_CPU->model_Name);

 cout<<"Report CPU: \n";

 cout<<"Model:  "<<*shared_String<<endl;

 cout<<"Cache: "<<*shared_Int<<endl;

 cout<<"MHz: "<<*shared_Double<<endl;

    return 0;

}

sobota, 7 stycznia 2023

Dynamic form and components (Visual C++)

 


private:

Form^ my_Form;

Button^ info_Button;

Button^ close_Button;

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

}

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

my_Form = gcnew Form;

info_Button = gcnew Button;

close_Button = gcnew Button;

info_Button->Text = "Date";

close_Button->Text = "Close";

info_Button->Location = Point(10, 10);

close_Button->Location = Point(10, 100);

my_Form->Controls->Add(info_Button);

my_Form->Controls->Add(close_Button);

info_Button->Click += gcnew EventHandler(this, &Form1::info_Button_Click);

close_Button->Click += gcnew EventHandler(this, &Form1::close_Button_Click);

my_Form->Show();

}

private:

System::Void close_Button_Click(System::Object^ sender, System::EventArgs^ e)

{

my_Form->Close();

}

private:

System::Void info_Button_Click(System::Object^ sender, System::EventArgs^ e)

{

DateTime^ now_Date = gcnew DateTime;

now_Date = DateTime::Now;

int width, height;

width = my_Form->Width;

height = my_Form->Height;

Label^ date_Label = gcnew Label;

String^ temp_Format = now_Date->Year.ToString()+"-"+now_Date->Month.ToString() +

"-"+now_Date->Day.ToString();

date_Label->Text = temp_Format;

date_Label->Location = Point(width / 2, height / 2);

((Button^)sender)->Parent->Controls->Add(date_Label);

}