poniedziałek, 29 listopada 2021

Sleep() nad time() (C++)

 #include <iostream>

#include <fstream>

#include <string.h>

#include <time.h>

#include <unistd.h>

using namespace std;



class time_File

{

private:

char* nameF;

int timeS;

time_t now_T;

public:

time_File(char* fileName, int sleepTime)

{

nameF=fileName;

timeS=sleepTime;

}

void Save()

{

int i;

fstream file(nameF,ios::out);

if (file.good())

{

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

{

now_T=time(NULL);

char *temp_File=asctime(localtime(&now_T));

file<<temp_File;

file.flush();

sleep(1);

}

file.close();

}

}

};


int main(int argc, char **argv)

{

time_File Timer("date.txt",7);

Timer.Save();

return 0;

}

Result: date.txt


Tue Nov 30 08:32:10 2021
Tue Nov 30 08:32:11 2021
Tue Nov 30 08:32:12 2021
Tue Nov 30 08:32:13 2021
Tue Nov 30 08:32:14 2021
Tue Nov 30 08:32:15 2021
Tue Nov 30 08:32:16 2021

Copying to Multiple Files (C++)

 


#include <iostream>

#include <string.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/sendfile.h>

#include <sys/stat.h>


using namespace std;



class copy_File

{

private:

string rname;

string *names;

int s;

int *files_W;

off_t temp_Off;

public:

copy_File(int s1,string* mNames, string rName)

{

int i;

s=s1;

names=new string[s];

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

names[i]=mNames[i];

rname=rName;

files_W=new int [s];

temp_Off=0;

}

~copy_File()

{

delete files_W;

delete names;

}

void send_F()

{

int file_R,i;

struct stat buff_Fil;

file_R=open(rname.c_str(),O_RDONLY);

fstat(file_R,&buff_Fil);

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

{

files_W[i]=open(names[i].c_str(),O_WRONLY | O_CREAT,buff_Fil.st_mode);

sendfile(files_W[i],file_R,&temp_Off,buff_Fil.st_size);

}

close(file_R);

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

close(files_W[i]);

}

};

 





int main(int argc, char **argv)

{

string read_Name="chromo.cxx";

string weeks[7]={"Monday","Tuesday","Wednesday","Thursday","Friday",

"Saturday","Sunday"};

copy_File m_File(7,weeks,read_Name);

m_File.send_F();

return 0;

}


piątek, 26 listopada 2021

Analysis and simulation lottery-"lotto" problems - C++

 #include <iostream>

#include <time.h>

#include <stdlib.h>


#define lotto_size 42

#define min_size 5

#define max_size 1000


using namespace std;

int main_Size[min_size];


int test_Size[max_size][min_size];

int temp_Size1[lotto_size];

int temp_Size2[max_size][lotto_size];


void sort_Lotto(int *tab, int m_Size)

{

    int i,j,k;

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

    {


        j=i;

        k=tab[j];

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

        {

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

            j--;

        }

        tab[j]=k;

    }

}


bool is_Zero(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

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

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==0)

        return true;

    else

        return false;

}

bool is_One(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

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

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==1)

        return true;

    else

        return false;

}

bool is_Two(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

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

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==2)

        return true;

    else

        return false;

}


bool is_Three(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

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

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==3)

        return true;

    else

        return false;

}

bool is_Four(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

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

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==4)

        return true;

    else

        return false;

}

bool is_Five(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

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

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==5)

        return true;

    else

        return false;

}






void init_Tab()

{

    int i,j,k,l;

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

        temp_Size1[i]=0;

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

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

        temp_Size2[i][j]=0;

    i=0;

    do

    {

        do

        {

           k=1+rand()%lotto_size;

        }while(temp_Size1[k-1]!=0);

        temp_Size1[k-1]=1;

        main_Size[i]=k;

        ++i;

    }while(i<min_size);

    l=0;

    do

    {

        i=0;

        do

        {

            do

            {

                k=1+rand()%lotto_size;

            }while(temp_Size2[i][k-1]!=0);

            temp_Size2[l][k-1]=1;

            test_Size[l][i]=k;

            ++i;


        }while(i<min_size);


        ++l;

    }while(l<max_size);

    sort_Lotto(main_Size,min_size);

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

        sort_Lotto(test_Size[i],min_size);

}

int main()

{

    int i,a0,a1,a2,a3,a4,a5;

    a0=a1=a2=a3=a4=a5=0;

    srand(time(NULL));

    init_Tab();

    

    cout<<"The coupon is drawn (5/42) : \n";

    

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

    {

        cout<<i+1<<") "<<main_Size[i]<<endl;


    }

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

    {

        if(is_Zero(main_Size,test_Size[i],min_size))

            ++a0;

        if(is_One(main_Size,test_Size[i],min_size))

            ++a1;

        if(is_Two(main_Size,test_Size[i],min_size))

            ++a2;

        if(is_Three(main_Size,test_Size[i],min_size))

            ++a3;

        if(is_Four(main_Size,test_Size[i],min_size))

            ++a4;

        if(is_Five(main_Size,test_Size[i],min_size))

            ++a5;

    }

    

    cout<<"\nAnalysis after 1000 draws\n";


    cout<<"\n-------------------------------------------------\n";

    cout<<"0/5 - "<<a0<<endl;

    cout<<"1/5 - "<<a1<<endl;

    cout<<"2/5 - "<<a2<<endl;

    cout<<"3/5 - "<<a3<<endl;

    cout<<"4/5 - "<<a4<<endl;

    cout<<"5/5 - "<<a5<<endl;

    return 0;

}


czwartek, 18 listopada 2021

Sleep() example C++

 #include <iostream>

#include <fcntl.h>

#include <fstream>

#include <stdio.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <time.h>

#include <unistd.h>

using namespace std;




class time_in_File

{

private:

char* now_Time();

char* file_Name;

int sl;

public:

char* write_Time();

time_in_File(char* name, int m_Sleep);

void Save();

};


char* time_in_File::now_Time()

{

char *n_Time;

time_t act;

act=time(NULL);

n_Time=asctime(localtime(&act));

return n_Time;

}

char* time_in_File::write_Time()

{

return now_Time();

}

time_in_File::time_in_File(char* name, int m_Sleep)

{

file_Name=name;

sl=m_Sleep;

}

void time_in_File::Save()

{

int i;

fstream my_File(file_Name,ios::out);

if(my_File.good())

{

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

{

my_File<<write_Time()<<"\n";

sleep(sl);

}

my_File.close();

}

}


int main(int argc, char **argv)

{

char* n="Example.txt";

int s=5;

time_in_File my_Class(n,s);

my_Class.Save();

return 0;

}

RESULT File: Example.txt

Thu Nov 18 18:26:52 2021

Thu Nov 18 18:26:57 2021

Thu Nov 18 18:27:02 2021

Thu Nov 18 18:27:07 2021

Thu Nov 18 18:27:12 2021

wtorek, 2 listopada 2021

Saving text to Word (C++ Builder)


 void __fastcall TForm1::Button1Click(TObject *Sender)

{

 Close();

}

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

void __fastcall TForm1::Button2Click(TObject *Sender)

{

 ListBox1->Items->Clear();

}

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

void __fastcall TForm1::Button3Click(TObject *Sender)

{

 ListBox1->Items->Clear();

 if(FileExists(Edit1->Text))

  ListBox1->Items->LoadFromFile(Edit1->Text);

}

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

void __fastcall TForm1::Button4Click(TObject *Sender)

{

 ListBox1->Items->SaveToFile(Edit2->Text);

}

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

void __fastcall TForm1::Button5Click(TObject *Sender)

{

 ListBox1->Items->Add(Edit3->Text);

 Edit3->Text="";

}

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

void __fastcall TForm1::Button6Click(TObject *Sender)

{

 Close();

}

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

void __fastcall TForm1::Button7Click(TObject *Sender)

{

 int i;

 Variant mWord, mValue;

 for (i=0;i<ListBox1->Items->Count;i++)

 {

if(ListBox1->Selected[i])

  Edit4->Text=ListBox1->Items->Strings[i];

 }


 UnicodeString a1,a2;

 a1="Selection";

 a2="Select";

 mWord=Variant::GetActiveObject("Word.Application");

 mValue=mWord.OlePropertyGet(a1);

 mValue.OlePropertySet("Text",Edit4->Text.w_str());

 mValue.OleProcedure(a2);

 mWord=mValue=Unassigned;

}

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

void __fastcall TForm1::FormCreate(TObject *Sender)

{

 Edit4->Text="Select text from the ListBox";

}