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

}


piątek, 30 grudnia 2022

PointF Visual C++

 








date.h 

#pragma once



int tab1[40];

int tab2[40];

int tab3[40];


void m_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;

}

}

Form1.h


#pragma once

#include <stdlib.h>

#include <time.h>

#include "date.h"

#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
int i;
srand(time(NULL));
dataGridView1->ColumnCount = 40;
dataGridView1->RowCount = 2;
for (i = 0; i < 40; i++)
dataGridView1->Columns[i]->Width = 30;
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int max_X = panel1->Width;
int max_Y = panel1->Height;
int min_X, min_Y,i,j,k,l;
min_X = min_Y = 1;
Graphics^ gr = panel1->CreateGraphics();
Pen^ my_Pen = gcnew Pen(System::Drawing::Color::Blue);
array<System::Drawing::PointF>^ xy = gcnew array<System::Drawing::PointF>(40);
my_Pen->Width = 3;
for (i = 0; i < 40; i++)
{
k = rand() % max_X + min_X;
l = rand() % max_Y + min_Y;
tab1[i] = k;
tab2[i] = l;
}
m_Isort(tab1, 40);
m_Isort(tab2, 40);
for (i = 0; i < 40; i++)
tab3[i] = max_Y - tab2[i];
for (i = 0; i < 40; i++)
{
dataGridView1->Rows[0]->Cells[i]->Value = tab1[i].ToString();
dataGridView1->Rows[1]->Cells[i]->Value = tab2[i].ToString();
xy[i].X = tab1[i];
xy[i].Y = tab3[i];

}
gr->DrawCurve(my_Pen, xy);
}
};
}

sobota, 3 grudnia 2022

Mondial 2022 group C - TStringGrid - C++ Builder

 




struct mundial_2022
{
 AnsiString Name;
 int win_Goal;
 int los_Goal;
 int pkt;
}soccer_GroupC[4];;

 int __fastcall Result(int x, int y);
 void __fastcall write_Match(AnsiString team1,AnsiString team2,int goal1,int goal2,int rows);
 void __fastcall date_Team1();


__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  soccer_GroupC[0].Name="Argentina";
  soccer_GroupC[1].Name="Poland";
  soccer_GroupC[2].Name="Mexico";
  soccer_GroupC[3].Name="Saudi Arabia";
  for(int i=0;i<4;i++)
  {
   soccer_GroupC[i].win_Goal=0;
   soccer_GroupC[i].los_Goal=0;
   soccer_GroupC[i].pkt=0;
  }
}

int __fastcall TForm1::Result(int x, int y)
{
 int pkt;
 if(x>y)
  pkt=3;
 else if(x<y)
  pkt=0;
 else
  pkt=1;
 return pkt;
}
void __fastcall TForm1::write_Match(AnsiString team1,AnsiString team2,int goal1,int goal2,int rows)
{
  StringGrid1->Cells[0][rows]=team1;
  StringGrid1->Cells[1][rows]=team2;
  StringGrid1->Cells[2][rows]=IntToStr(goal1);
  StringGrid1->Cells[3][rows]=IntToStr(goal2);
}
void __fastcall TForm1::date_Team1()
{

  int goals_Arg[6]={1,2,2,0,2,0};
  int w,l;
  w=l=0;
  soccer_GroupC[0].pkt+=Result(goals_Arg[0],goals_Arg[1]);
  soccer_GroupC[0].pkt+=Result(goals_Arg[2],goals_Arg[3]);
  soccer_GroupC[0].pkt+=Result(goals_Arg[4],goals_Arg[5]);
  w=goals_Arg[0]+goals_Arg[2]+goals_Arg[4];
  l=goals_Arg[1]+goals_Arg[3]+goals_Arg[5];
  soccer_GroupC[0].win_Goal=w;
  soccer_GroupC[0].los_Goal=l;

  int goals_Pol[6]={0,0,2,0,0,2};
  w=l=0;
  soccer_GroupC[1].pkt+=Result(goals_Pol[0],goals_Pol[1]);
  soccer_GroupC[1].pkt+=Result(goals_Pol[2],goals_Pol[3]);
  soccer_GroupC[1].pkt+=Result(goals_Pol[4],goals_Pol[5]);
  w=goals_Pol[0]+goals_Pol[2]+goals_Pol[4];
  l=goals_Pol[1]+goals_Pol[3]+goals_Pol[5];
  soccer_GroupC[1].win_Goal=w;
  soccer_GroupC[1].los_Goal=l;

  int goals_Mex[6]={0,0,0,2,2,1};
  w=l=0;
  soccer_GroupC[2].pkt+=Result(goals_Mex[0],goals_Mex[1]);
  soccer_GroupC[2].pkt+=Result(goals_Mex[2],goals_Mex[3]);
  soccer_GroupC[2].pkt+=Result(goals_Mex[4],goals_Mex[5]);
  w=goals_Mex[0]+goals_Mex[2]+goals_Mex[4];
  l=goals_Mex[1]+goals_Mex[3]+goals_Mex[5];
  soccer_GroupC[2].win_Goal=w;
  soccer_GroupC[2].los_Goal=l;

  int goals_Sau[6]={2,1,0,2,1,2};
  w=l=0;
  soccer_GroupC[3].pkt+=Result(goals_Sau[0],goals_Sau[1]);
  soccer_GroupC[3].pkt+=Result(goals_Sau[2],goals_Sau[3]);
  soccer_GroupC[3].pkt+=Result(goals_Sau[4],goals_Sau[5]);
  w=goals_Sau[0]+goals_Sau[2]+goals_Sau[4];
  l=goals_Sau[1]+goals_Sau[3]+goals_Sau[5];
  soccer_GroupC[3].win_Goal=w;
  soccer_GroupC[3].los_Goal=l;


}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{

 write_Match(soccer_GroupC[0].Name,soccer_GroupC[3].Name,1,2,0);
 write_Match(soccer_GroupC[1].Name,soccer_GroupC[2].Name,0,0,1);
 write_Match(soccer_GroupC[3].Name,soccer_GroupC[1].Name,0,2,2);
 write_Match(soccer_GroupC[2].Name,soccer_GroupC[0].Name,0,2,3);
 write_Match(soccer_GroupC[1].Name,soccer_GroupC[0].Name,0,2,4);
 write_Match(soccer_GroupC[2].Name,soccer_GroupC[3].Name,2,1,5);
 date_Team1();
 for(int i=0;i<4;i++)
 {
  StringGrid2->Cells[0][i]=soccer_GroupC[i].Name;
  StringGrid2->Cells[1][i]=IntToStr(soccer_GroupC[i].pkt);
  StringGrid2->Cells[2][i]=IntToStr(soccer_GroupC[i].win_Goal);
  StringGrid2->Cells[3][i]=IntToStr(soccer_GroupC[i].los_Goal);
 }
}
//---------------------------------------------------------------------------