sobota, 23 grudnia 2017

Status CD Drive (class C++ Builder)

#include <mmsystem.h>
#include <ExtCtrls.hpp>
#include <FileCtrl.hpp>

class state_CD
{
private:

MCI_OPEN_PARMS cd_Parameters;
MCI_STATUS_PARMS cd_State;

int sent_Value;
UnicodeString cD;
public:

state_CD()
{

 cD="CDAudio";
 cd_Parameters.dwCallback=0;
 cd_Parameters.lpstrDeviceType=cD.w_str();

}
int read_Value(UnicodeString type_CD)
{

 cd_Parameters.lpstrElementName=type_CD.w_str();
 mciSendCommand(0,MCI_OPEN,MCI_OPEN_ELEMENT | MCI_OPEN_TYPE,(long)&cd_Parameters);
 cd_State.dwItem=MCI_STATUS_MODE;
 mciSendCommand(cd_Parameters.wDeviceID,MCI_STATUS,MCI_WAIT | MCI_STATUS_ITEM,(long)&cd_State);
 sent_Value=cd_State.dwReturn;
 mciSendCommand(cd_Parameters.wDeviceID,MCI_CLOSE,MCI_NOTIFY,(long)&cd_Parameters);
 return sent_Value;

}
void write_ListBox(TDriveComboBox *tDCB,TListBox *tLB)
{
UnicodeString test_Value;
test_Value=UnicodeString(tDCB->Drive+":");
if(read_Value(test_Value)==526)
 tLB->Items->Add("Playing");
else if(read_Value(test_Value)==525)
 tLB->Items->Add("Pause");
else if(read_Value(test_Value)==530)
 tLB->Items->Add("No CD");
else if(read_Value(test_Value)==1242732)
 tLB->Items->Add("No CD Drive");
else if(read_Value(test_Value)==1242348)
 tLB->Items->Add("Symbol is not CD Drive");
else
 tLB->Items->Add("Default operation: "+IntToStr(read_Value((UnicodeString)tDCB->Drive+":")));

    }
};

And example: 

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 state_CD test_CD;
 test_CD.write_ListBox(DriveComboBox1,ListBox1);

}

piątek, 22 grudnia 2017

Bit prime numbers (C++)

#include <iostream>
#include <conio.h>
#define max 20
#define max_2 4*max
#define yes '1'
#define no '0'

using namespace std;



char *bits_F;
int *value_F;

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;

}
void test_Bits(char bit,char *tab_Bytes)
{
    int value,i;
    for(i=max-1;i>=0;i--)
    {
        value=(tab_Bytes[i]&bit);
        if(value!=0)
            cout<<yes;
        else
            cout<<no;
    }
}
int main()
{
    int i,j;
    int negation;
    bits_F=new char[max];
    value_F=new int[max_2];


   i=0;
   j=2;
   do
   {
       if(is_First(j))
       {
           bits_F[i]=j;

           ++i;
       }
       ++j;


   }while(i<max);

   i=0;
   j=2;
   do
   {
       if(is_First(j))
       {
           value_F[i]=j;
           ++i;
       }
       ++j;
   }while(i<max_2);
   for(i=0;i<max_2;i++)
   {
       negation=~i;
       cout<<i+1<<"\t";
       cout<<value_F[i]<<"\t";
       test_Bits(i+1,bits_F);cout<<"\t";
       test_Bits(negation+1,bits_F);
       cout<<endl;
   }




    delete bits_F;
    delete value_F;
    getch();
    return 0;
}

niedziela, 17 grudnia 2017

Drawing a cuboid (DrawLine - Visual C++)




private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
Graphics^ cu=panel1->CreateGraphics();
System::Drawing::Color my_Color;
my_Color=System::Drawing::Color::Black;
Pen^ my_Pen=gcnew Pen(my_Color);
Pen^ dot_Pen1=gcnew Pen(System::Drawing::Color::Red);
Pen^ dot_Pen2=gcnew Pen(System::Drawing::Color::Blue);
Pen^ dot_Pen3=gcnew Pen(System::Drawing::Color::Green);
dot_Pen1->DashStyle=System::Drawing::Drawing2D::DashStyle::DashDotDot;
dot_Pen2->DashStyle=System::Drawing::Drawing2D::DashStyle::DashDotDot;
dot_Pen3->DashStyle=System::Drawing::Drawing2D::DashStyle::DashDotDot;
System::Int16 margin_up_Y;
System::Int16 margin_down_Y;
System::Int16 margin_left_X;
System::Int16 margin_right_X;
margin_up_Y=10;
margin_down_Y=panel1->Height-10;
margin_left_X=10;
margin_right_X=panel1->Width-10;
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y,margin_right_X-(4*margin_left_X),margin_down_Y);
cu->DrawLine(my_Pen,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y));
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y,(4*margin_left_X),margin_down_Y-(8*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(4*margin_left_X),margin_down_Y,margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y));
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y,(2*margin_left_X),margin_down_Y-(20*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(4*margin_left_X),margin_down_Y,margin_right_X-(4*margin_left_X),margin_down_Y-(20*margin_up_Y));
cu->DrawLine(my_Pen,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),(4*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y-(20*margin_up_Y),(4*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
        cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y-(20*margin_up_Y),margin_right_X-(4*margin_left_X),margin_down_Y-(20*margin_up_Y));
cu->DrawLine(my_Pen,(4*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(4*margin_left_X),margin_down_Y-(20*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(dot_Pen1,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(4*margin_left_X),margin_down_Y);
cu->DrawLine(dot_Pen2,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(dot_Pen3,margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y),margin_right_X-(4*margin_left_X),margin_down_Y);
 

}  
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
};
}

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