sobota, 28 kwietnia 2018

Using recursion to draw (Visual C++)




pragma endregion
private: System::Void Drawing(Graphics^ temp_Graph,Pen^ temp_Pen,int x, int y, int z)
{
int step=5;
if(x>0)
{
temp_Graph->DrawLine(temp_Pen,y+x,z,y+x,z+x);
temp_Graph->DrawLine(temp_Pen,y+step,z+x,y+step,z+step);
Drawing(temp_Graph,temp_Pen,x-(2*step),y+step,z+step);
}
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 
Graphics^ graph_Panel=panel1->CreateGraphics();
Pen^ pen_Panel1=gcnew Pen(System::Drawing::Color::Blue);
pen_Panel1->Width=3;
Pen^ pen_Panel2=gcnew Pen(System::Drawing::Color::Green);
pen_Panel2->Width=3;
Pen^ pen_Panel3=gcnew Pen(System::Drawing::Color::Violet);
pen_Panel3->Width=3;
Pen^ pen_Panel4=gcnew Pen(System::Drawing::Color::Brown);
pen_Panel4->Width=3;
int centr_X=panel1->Width/2;
int centr_Y=centr_X;
Drawing(graph_Panel,pen_Panel1,centr_X/2,centr_X/2,centr_Y);
Drawing(graph_Panel,pen_Panel2,centr_X/2,centr_X,centr_Y);
Drawing(graph_Panel,pen_Panel3,centr_X/2,centr_X,centr_Y/2);
Drawing(graph_Panel,pen_Panel4,centr_X/2,centr_X/2,centr_Y/2);
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
};
}


piątek, 27 kwietnia 2018

Use pipe (g++ class) Linux

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

using namespace std;

class PIPE
{
    private:
    int *base;
    int *temp_tab;
    bool which_Pid(pid_t pid_value)
    {
        if(pid_value==(pid_t)0)
         return true;
        else
         return false;
    }
    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 init_Base(int size)
    {
        int i,j;
        i=0;
        j=2;
        do
        {
            if(is_First(j))
            {
                base[i]=j;
                ++i;
            }
            ++j;
           
        }while(i<size);
       
    }
    FILE *temp_stream;
    pid_t temp_pid;
    void read_Base(FILE *temp_stream)
    {
        char *temp_buf;
        temp_buf=new char[1024];
        while(!feof(temp_stream) && fgets(temp_buf,sizeof(temp_buf),temp_stream)!=NULL)
         fputs(temp_buf,stdout);
       
        delete temp_buf;
    }
    void write_Base(int count, FILE *temp_stream)
    {
        int i;
        for(i=0;i<count;i++)
        {
            fprintf(temp_stream,"%d\n",base[i]);
            fflush(temp_stream);
        }
    }
    public:
    PIPE()
    {
        temp_tab=new int[2];
        base=new int[1000];
        init_Base(1000);
        pipe(temp_tab);
        temp_pid=fork();
   
    }
    void Run(int count)
    {
       
        if(which_Pid(temp_pid))
        {
           
            close(temp_tab[1]);
            temp_stream=fdopen(temp_tab[0],"r");
            read_Base(temp_stream);
            close(temp_tab[0]);
        }
       
        else
        {
           
            close(temp_tab[0]);
            temp_stream=fdopen(temp_tab[1],"w");
            write_Base(count,temp_stream);
            close(temp_tab[1]);
        }
    }
    ~PIPE()
    {
        delete temp_tab;
        delete base;
    }
};

int main(int argc, char **argv)
{
    PIPE test_Pipe;
    test_Pipe.Run(100);
   
   
    return 0;
}

poniedziałek, 16 kwietnia 2018

A simple graph with coordinates arrays - PointF. Visual C ++


#pragma endregion
private:
System::Void Sorting(array<System::Int32>^table, int size)
{
int i,j,temp;
for(i=1;i<size;i++)
{
j=i;
temp=table[j];
while((j>0) && (table[j-1]>temp))
{
table[j]=table[j-1];
j--;
}
table[j]=temp;
}
}
private:
array<System::Int32>^dateX;
private:
array<System::Int32>^dateY;
private:
array<System::Drawing::PointF>^points;
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
dataGridView1->ColumnCount=2;
dataGridView1->RowCount=50;
int i;
for(i=0;i<2;i++)
dataGridView1->Columns[i]->Width=40;
for(i=0;i<50;i++)
dataGridView1->Rows[i]->Height=14;
srand(time(NULL));
points=gcnew array<System::Drawing::PointF>(50);
dateX=gcnew array<System::Int32>(50);
dateY=gcnew array<System::Int32>(50);
 
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
int i;
Graphics^ graph=panel1->CreateGraphics();
System::Drawing::Color temp_Color;
for(i=0;i<50;i++)
{
dateX[i]=rand()%panel1->Width;
dateY[i]=rand()%panel1->Height;
}
Sorting(dateX,50);
for(i=0;i<50;i++)
{
dataGridView1->Rows[i]->Cells[0]->Value=dateX[i];
dataGridView1->Rows[i]->Cells[1]->Value=dateY[i];
points[i].X=dateX[i];
points[i].Y=dateY[i];
}
dataGridView1->AutoResizeRows();
for(i=0;i<50;i++)
{
int r_color,g_color,b_color;
r_color=rand()%255;
g_color=rand()%255;
b_color=rand()%255;
temp_Color=System::Drawing::Color::FromArgb(r_color,g_color,b_color);
Pen^ temp_Pen=gcnew Pen(temp_Color);
graph->DrawCurve(temp_Pen,points);

}

}
};
}


piątek, 13 kwietnia 2018

The class returns the program name for the process - /proc (g++/linux)

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
class get_Name
{
    private:
    int read_value;
    char *start_file;
    char *end_file;
    pid_t pid_value;
    char name[128];
    char info_value[1024];
    char *text_value;
    int file;
    public:
    get_Name(pid_t m_pid)
    {
      int int_pid;   
      pid_value=m_pid;
      int_pid=(int)pid_value;   
      snprintf(name,sizeof(name),"/proc/%d/stat",int_pid);
      file=open(name,O_RDONLY);
      read_value=read(file,info_value,sizeof(info_value)-1);
      close(file);
      info_value[read_value]='\0';
     
    }
    char *get_Text()
    {
        start_file=strchr(info_value,'(');
        end_file=strchr(info_value,')');
        text_value=new char[end_file-start_file];
        strncpy(text_value,start_file+1,end_file-start_file-1);
        text_value[end_file-start_file-1]='\0';
        return text_value;
    }
    ~get_Name()
    {
        delete text_value;
    }
};

int main(int argc, char **argv)
{
    pid_t test_pid=(pid_t) atoi(argv[1]);
    get_Name Get(test_pid);
    cout<<Get.get_Text()<<endl;
    return 0;
}

sobota, 7 kwietnia 2018

class with disk information, wchar_t use (C++ Builder)


CLASS:

#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <vcl.h>
#define length_bufor 255
class Disc
{
private:
DWORD sectors;
DWORD serial;
DWORD free_cl;
DWORD bytes_in_sector;
DWORD number_claster;
DWORD total_free_bytes;
wchar_t *changeDWORD(unsigned int value)
{
size_t i,j,width;
j=0;
wchar_t temp_buff1[50];
static wchar_t temp_buff2[75];
width=wcslen(temp_buff1);
swprintf(temp_buff1,sizeof(temp_buff1),L"%1u",value);
for(i=0;i<width;i++)
{
;
if(i&&((width-i)%3)==0)
{
;
temp_buff2[i+j]=' ';j++;
}
temp_buff2[i+j]=temp_buff1[i];
}
temp_buff2[i+j]=temp_buff1[i];

return temp_buff2;

}
wchar_t temp_Str[40];
UnicodeString Volumen;
UnicodeString BytSectors;
UnicodeString Serial;
UnicodeString temp_Path;
UnicodeString Sectors;
UnicodeString Claster;
UnicodeString FreeB;
public:
Disc(UnicodeString discPath)
{

UnicodeString vol;
vol.SetLength(length_bufor);
temp_Path=discPath;
if(GetVolumeInformation(temp_Path.w_str(),vol.w_str(),vol.Length(),&serial,
NULL,NULL,NULL,NULL))
{
_itow(serial,temp_Str,20);
Volumen=vol;
Serial=temp_Str;
GetDiskFreeSpace(temp_Path.w_str(),&sectors,&bytes_in_sector,&free_cl,&number_claster);
total_free_bytes=bytes_in_sector*sectors*number_claster;
Sectors=changeDWORD(sectors);
BytSectors=changeDWORD(bytes_in_sector);
Claster=changeDWORD(number_claster);
FreeB=changeDWORD(total_free_bytes);


}
else
{
ShowMessage("Disc is protected");
Application->Terminate();
}


}
UnicodeString give_Volumen()
{
return Volumen;
}
UnicodeString give_Serial()
{
return Serial;
}
UnicodeString give_Sectors()
{
return Sectors;
}
UnicodeString give_byte_in_Sector()
{
return BytSectors;
}
UnicodeString give_Claster()
{
return Claster;
}
UnicodeString give_free_Bytes()
{
return FreeB;
}

};

EXAMPLE:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Disc my_D("C:\\");
  ListBox1->Items->Add(my_D.give_Volumen());
  ListBox1->Items->Add(my_D.give_Serial());
  ListBox1->Items->Add(my_D.give_Claster());
  ListBox1->Items->Add(my_D.give_Sectors());
  ListBox1->Items->Add(my_D.give_byte_in_Sector());
  ListBox1->Items->Add(my_D.give_free_Bytes());
}