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