czwartek, 14 grudnia 2017

Communication of processes (example class - g++ - Linux)

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

class connect_Process
{
    private:
    pid_t temp_pid;
    int *temp;
    FILE *file_stream;
    void write_Message(FILE *file_message,char *test_message)
    {
        fprintf(file_message,"%s\n",test_message);
        fflush(file_message);
    }
    void read_Message(FILE *file_message)
    {
        char *temp;
        temp=new char[1024];
        while(!feof(file_message) && !ferror(file_message)
        && fgets(temp,sizeof(temp),file_message)!=NULL)
        fputs(temp,stdout);
        delete temp;
    }
    public:
    connect_Process()
    {
        temp=new int[2];
       
    }
    void info_Process(char *path)
    {
        pipe(temp);
        temp_pid=fork();
        if(temp_pid==(pid_t)0)
        {
            close(temp[1]);
            file_stream=fdopen(temp[0],"r");
            read_Message(file_stream);
            close(temp[0]);
        }
        else
        {
            close(temp[0]);
            file_stream=fdopen(temp[1],"w");
            write_Message(file_stream,path);
            close(temp[1]);
        }
       
       
    }
    ~connect_Process()
    {
        delete temp;
    }
};
int main(int argc, char **argv)
{
    char *path=argv[0];
    connect_Process c_Proc;
    c_Proc.info_Process(path);
   
    return 0;
}

środa, 13 grudnia 2017

Some useful functions for arrays C++/CLI (Visual C++). Maybe help someone.

System::Int64 min_Table(array<System::Int64>^ table,System::Int64 size)
{
System::Int64 i;
System::Int64 min;
min=table[0];
for(i=1;i<size;i++)
if(table[i]<min)
min=table[i];
return min;
}
System::Int64 max_Table(array<System::Int64>^ table,System::Int64 size)
{
System::Int64 i;
System::Int64 max;
max=table[0];
for(i=1;i<size;i++)
if(table[i]>max)
max=table[i];
return max;

}
System::Int64 sum_Table(array<System::Int64>^ table,System::Int64 size)
{
System::Int64 i;
System::Int64 sum;
sum=0;
for(i=0;i<size;i++)
sum+=table[i];
return sum;
}
System::Double average_Table(array<System::Int64>^ table,System::Int64 size)
{
System::Double average;
average=(1.0*sum_Table(table,size))/(1.0*size);
return average;
}
System::Void reverse_Tables(array<System::Int64>^ table1,array<System::Int64>^ table2,System::Int64 size)
{
array<System::Int64>^ temp;
System::Int64 i;
temp=gcnew array<System::Int64>(size);
for(i=0;i<size;i++)
temp[i]=table1[i];
for(i=0;i<size;i++)
table1[i]=table2[i];
for(i=0;i<size;i++)
table2[i]=temp[i];
}
System::Int64 even_in_Table(array<System::Int64>^ table,System::Int64 size)
{
System::Int64 sum_even;
System::Int64 i;
sum_even=0;
for(i=0;i<size;i++)
if(table[i]%2==0)
++sum_even;
return sum_even;
}
System::Int64 odd_in_Table(array<System::Int64>^ table,System::Int64 size)
{
System::Int64 sum_odd;
System::Int64 i;
sum_odd=0;
for(i=0;i<size;i++)
if(table[i]%2!=0)
++sum_odd;
return sum_odd;

}
System::Boolean is_in_Table(array<System::Int64>^ table,System::Int64 size,System::Int64 find_value)
{
System::Boolean temp_bool;
System::Int64 i;
temp_bool=false;
for(i=0;i<size;i++)
if(find_value==table[i])
temp_bool=true;
return temp_bool;
}
System::Void make_array_Table(array<System::Int64>^ table,System::Int64 size)
{
Array^ array_Table;
System::Int64 i;
array_Table=Array::CreateInstance(System::Int64::typeid,size);
for(i=0;i<size;i++)
array_Table->SetValue(table[i],i);
}
System::Void make_double_Table(System::Int64 size1, System::Int64 size2,System::Int64 max_value)
{
array<System::Int64,2>^ double_Table;
System::Int64 i;
System::Int64 j;
Random^ random_value=gcnew Random();
double_Table=gcnew array<System::Int64,2>(size1,size2);
for(i=0;i<size1;i++)
for(j=0;j<size2;j++)
double_Table[i,j]=random_value->Next(max_value);

}

piątek, 8 grudnia 2017

A class that displays dependencies that display results and saves data to a file

.h

#include <vcl.h>
#include <stdio.h>
class prime_Numbers
{
 private:
  AnsiString file_Name;
  int size;
  bool is_First(int value);
  int sum_Table(int *tab, int size);
  int *first_Numbers;
  int *sum_Numbers;
  float *average_Numbers;
 public:
  prime_Numbers(AnsiString Name, int size_N);
  void Init();
  void Write(TListBox *LB);
  void Save();
  ~prime_Numbers();
};

.cpp
prime_Numbers::prime_Numbers(AnsiString Name, int size_N)
{

 file_Name=Name;
 size=size_N;
 first_Numbers=new int[size];
 sum_Numbers=new int[size];
 average_Numbers=new float[size];
}
void prime_Numbers::Init()
{
 int i,j,k,l;
 for(i=0;i<size;i++)
  sum_Numbers[i]=0;
 i=0;
 j=2;
 do
 {
  if(is_First(j))
  {
   first_Numbers[i]=j;
   ++i;
   l=0;
   for(k=2;k<j+1;k++)
    if(is_First(k))
     sum_Numbers[i-1]+=k;


  }
  ++j;
 }while(i<size+1);
 for(i=0;i<size;i++)
  average_Numbers[i]=(1.0*sum_Numbers[i])/(1.0*first_Numbers[i]);
}
void prime_Numbers::Write(TListBox *LB)
{
 int i;
 for(i=0;i<size;i++)
  LB->Items->Add(IntToStr(first_Numbers[i])+" - "+IntToStr(sum_Numbers[i])+" - "+
  average_Numbers[i]);

}
void prime_Numbers::Save()
{
 char *name;
 FILE *f_Name;
 int i;
 name=file_Name.c_str();
 f_Name=fopen(name,"w");
 for(i=0;i<size;i++)
 {
  fprintf(f_Name,"%d %d %.2f",first_Numbers[i],sum_Numbers[i],average_Numbers[i]);
  fprintf(f_Name,"\n");
 }
}
int prime_Numbers::sum_Table(int *tab, int size)
{
 int i,sum;
 sum=0;
 for(i=0;i<size;i++)
  sum+=tab[i];
 return sum;
}
prime_Numbers::~prime_Numbers()
{
 delete sum_Numbers;
 delete average_Numbers;
 delete first_Numbers;
}
bool prime_Numbers::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;
}

Example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 prime_Numbers p_Numbers(Edit1->Text,StrToInt(Edit2->Text));
 p_Numbers.Init();
 p_Numbers.Write(ListBox1);
 p_Numbers.Save();
}

niedziela, 19 listopada 2017

Sorting, dynamic arrays, example - GNU GCC Compiler

#include <iostream>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

#define max 20

int **TABLE,*FIRST_VALUES,*SUM_VALUES;
int **MIN_TABLE,**MAX_TABLE;
float *AVERAGE_VALUES;

void sort_Table(int *tab)
{
   int i,j,temp;
   for(i=1;i<max;i++)
    for(j=max-1;j>=i;j--)
    if(tab[j]<tab[j-1])
   {
       temp=tab[j-1];
       tab[j-1]=tab[j];
       tab[j]=temp;
   }

}

int comp_Values(int value1,int value2)
{
    return (value1<value2);
}
void move_Values(int *value1,int *value2)
{
    int temp_value;
    temp_value=*value1;
    *value1=*value2;
    *value2=temp_value;
}
void max_Sort(int *table)
{
    int i,j;
    for(i=0;i<max;i++)
    {
        for(j=0;j<max-1;j++)
        {
            if(comp_Values(table[j],table[j+1]))
                move_Values(&table[j],&table[j+1]);
        }
    }
}

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


}

int sum_Table(int *tab, int size)
{
    int i,result;
    result=0;
    for(i=0;i<size;i++)
        result+=tab[i];
    return result;

}

float average_Table(int *tab, int size)
{

  float result;
  result=(1.0*sum_Table(tab,size))/(1.0*size);
  return result;
}

int main()
{
    srand(time(NULL));
    int i,j,k;
    TABLE=new int *[max];
    MIN_TABLE=new int *[max];
    MAX_TABLE=new int *[max];
    FIRST_VALUES=new int[max];
    SUM_VALUES=new int[max];
    AVERAGE_VALUES=new float [max];
    for(i=0;i<max;i++)
    {

        MIN_TABLE[i]=new int[max];
        MAX_TABLE[i]=new int[max];
        TABLE[i]=new int [max];
    }
    i=0;
    j=2;
    do
    {
        if(is_First(j))
        {

            FIRST_VALUES[i]=j;
            SUM_VALUES[i]=0;
            ++i;
        }
        ++j;

    }while(i<max);

    for(i=0;i<max;i++)
    {


        for(j=0;j<max;j++)
        {


         k=FIRST_VALUES[rand()%20];
         TABLE[i][j]=k;
         MIN_TABLE[i][j]=k;
         MAX_TABLE[i][j]=k;
         SUM_VALUES[i]+=TABLE[i][j];

        }

    }
    for(i=0;i<max;i++)
        AVERAGE_VALUES[i]=(1.0*SUM_VALUES[i])/(1.0*max);

    for(i=0;i<max;i++)
    {

       max_Sort(MAX_TABLE[i]);
      sort_Table(MIN_TABLE[i]);

    }

    cout<<"Original table: \n";
    for(i=0;i<max;i++)
    {
        for(j=0;j<max;j++)
        {
            cout<<TABLE[i][j]<<"  ";
        }
        cout<<"Sum =  "<<SUM_VALUES[i];
        cout<<" Average ="<<AVERAGE_VALUES[i];
        cout<<"\n";
    }

    cout<<"\n\n Sorted in descending order: \n";
    for(i=0;i<max;i++)
    {
        for(j=0;j<max;j++)
        {
            cout<<MIN_TABLE[i][j]<<"  ";
        }
        cout<<"Sum =  "<<SUM_VALUES[i];
        cout<<" Average ="<<AVERAGE_VALUES[i];
        cout<<"\n";
    }

     cout<<"\n\n Sorted ascending: \n";
    for(i=0;i<max;i++)
    {
        for(j=0;j<max;j++)
        {
            cout<<MAX_TABLE[i][j]<<"  ";
        }
        cout<<"Sum =  "<<SUM_VALUES[i];
        cout<<" Average ="<<AVERAGE_VALUES[i];
        cout<<"\n";
    }



    for(i=0;i<max;i++)
    {


        delete [] TABLE[i];
        delete [] MIN_TABLE[i];
        delete [] MAX_TABLE[i];
    }
    delete [] MAX_TABLE;
    delete [] MIN_TABLE;
    delete [] TABLE;
    delete AVERAGE_VALUES;
    delete SUM_VALUES;
    delete FIRST_VALUES;

    getch();
    return 0;
}

poniedziałek, 13 listopada 2017

Example type Binary (Visual C++)



#pragma endregion
private: System::String^ text_Binary(char value)
{
unsigned char test_bin[11]={1,2,4,8,16,32,64,128,256,512,1024};
String^ text;
text="";
System::Int16 bytes;
System::Int16 i;
for(i=10;i>=0;i--)
{
bytes=(test_bin[i]&value);
if(bytes!=0)
text+="1";
else
text+="0";
}
return text;
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
System::Int16 i;
System::Int16 j;
System::Int16 k;
for(i=0;i<1024;i++)
{
listBox1->Items->Add(text_Binary(i));
listBox2->Items->Add(text_Binary(~i));
listBox3->Items->Add(text_Binary(i<<1));
}

niedziela, 12 listopada 2017

Examples type Variant (C++ Builder)

#include <vcl.h>
#include <stdlib.h>
#include <time.h>

#define max 50

void init_Random()
{
srand(time(NULL));
}
bool 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;
}

class test_Variant
{
  private:

   Variant Array;
   typedef int temp_Array[max];
   temp_Array *t_A,*t_B;
   int v_min,v_max;

  public:
   test_Variant()
   {
   int i,j,k;
   init_Random();
   Array=VarArrayCreate(OPENARRAY(int,(0,max)),varInteger);
   t_A=(temp_Array *)VarArrayLock(Array);
   t_B=(temp_Array *)VarArrayLock(Array);
   i=0;
   j=2;
   do
   {
if(is_First(j))
{
(*t_A)[i]=j;
++i;
}
++j;
   }while(i<max);
   v_min=VarArrayLowBound(t_A,VarArrayDimCount(t_A));
   v_max=VarArrayHighBound(t_A,VarArrayDimCount(t_A));
   for(i=v_min;i<v_max;i++)
   {
k=rand()%max;
(*t_B)[i]=(*t_A)[k];
   }
   }
   ~test_Variant()
   {
   VarArrayUnlock(t_A);
   VarArrayUnlock(t_B);
   }
   void Write(TListBox *LB)
   {

int i;
for(i=v_min;i<v_max;i++)
  LB->Items->Add(IntToStr(i+1)+" ) -  "+VarToStr((*t_A)[i])+"  -  "+VarToStr((*t_B)[i]));

   }
};


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 test_Variant *t_V=new class test_Variant;
 t_V->Write(ListBox1);
}

niedziela, 5 listopada 2017

Class reading Shell Folders (C++ Builder)




#include <vcl.h>
#include <shlobj.h>
#include <Registry.hpp>

class read_Directory
{
private:
UnicodeString my_Key;
UnicodeString my_Result;
UnicodeString text_Folders;
public:
read_Directory(UnicodeString tF)
{
text_Folders=tF;
TRegistry *reg=new TRegistry();
reg->Access=KEY_READ;
reg->RootKey=HKEY_CURRENT_USER;
my_Key="\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
if(reg->OpenKey(my_Key,false))
{
  if(reg->ValueExists(text_Folders))
my_Result=reg->ReadString(text_Folders);
  else
my_Result="Value not exists";
}
else
{
  my_Result="Key not exists";
}
delete reg;
}
UnicodeString Write()
{
return my_Result;
}
};



Example:


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 read_Directory r_D1("Personal");
 Edit1->Text=r_D1.Write();
 read_Directory r_D2("History");
 Edit2->Text=r_D2.Write();
 read_Directory r_D3("Cookies");
 Edit3->Text=r_D3.Write();
 read_Directory r_D4("Linux");
 Edit4->Text=r_D4.Write();
}