środa, 28 czerwca 2017

Manipulation of data in dynamic tables (C++ Builder)




private: // User declarations
int *table_first_Value,*min1,*min2,*max1,*max2,*sum1,*sum2;
float *dif1,*dif2,*dif_m1,*dif_m2,*aver1,*aver2;
int **table1;
int **table2;
bool __fastcall is_First(int value);
void __fastcall init_Table();
void __fastcall delete_Table();
void __fastcall init_Random();
int __fastcall sum_Table(int *tab, int size);
void __fastcall sort_Bubble(int *table, int size) ;
float __fastcall average_Table(int *tab, int size);
float __fastcall average_MinMax(int *tab, int size);
int __fastcall max_Table(int *tab, int size);
int __fastcall min_Table(int *tab, int size);
float __fastcall difference_Average(int *tab, int size);


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::init_Random()
{
srand(time(NULL));
}

int __fastcall TForm1::sum_Table(int *tab, int size)
{
int i,sum;
sum=0;
for(i=0;i<size;i++)
sum+=tab[i];
return sum;
}
void __fastcall TForm1::sort_Bubble(int *table, int size)
{
int i,j,temporary;
for(i=1;i<size;i++)
for(j=size-1;j>=i;j--)
if(table[j]<table[j-1])
{
temporary=table[j-1];
table[j-1]=table[j];
table[j]=temporary;
}

}
float __fastcall TForm1::average_Table(int *tab, int size)
{
float result;
result=(1.*sum_Table(tab,size))/(size*1.0);
return result;
}
int __fastcall TForm1::max_Table(int *tab, int size)
{
int i,max_t;
max_t=tab[0];
for(i=1;i<size;i++)
{
if(tab[i]>=max_t)
max_t=tab[i];
}
return max_t;

}
int __fastcall TForm1::min_Table(int *tab, int size)
{
int i,min_t;
min_t=tab[0];
for(i=1;i<size;i++)
{
if(tab[i]<min_t)
min_t=tab[i];

}
return min_t;
}
float __fastcall TForm1::average_MinMax(int *tab, int size)
{
float result;
result=1.0*(((1.0*max_Table(tab,size))+(1.0*min_Table(tab,size)))/2.0);
return result;
}

float __fastcall TForm1::difference_Average(int *tab, int size)
{
float result1,result2,result_end;
result1=average_MinMax(tab,size);
result2=average_Table(tab,size);
if(result1>=result2)
result_end=result1-result2;
else
result_end=result2-result1;
return result_end;
}
bool __fastcall TForm1::is_First(int value)
{
int sum,i;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
 ++sum;
if(sum==2)
 return true;
else
 return false;
}
void __fastcall TForm1::init_Table()
{
table_first_Value=new int[100];
min1=new int[11];
min2=new int[11];
max1=new int[11];
max2=new int[11];
sum1=new int[11];
sum2=new int[11];
dif1=new float[11];
dif2=new float[11];
dif_m1=new float[11];
dif_m2=new float[11];
aver1=new float[11];
aver2=new float[11];
int i,j,k1,k2;
i=0;
j=2;
do
{
 if(is_First(j))
 {
 table_first_Value[i]=j;
 ++i;
 }
 ++j;

}while(i<100);
table1=new int *[11];
table2=new int *[11];
for(i=0;i<11;i++)
{
 table1[i]=new int[5];
 table2[i]=new int[5];

}
for(i=0;i<11;i++)
{
for(j=0;j<5;j++)
{
k1=rand()%100;
k2=rand()%100;
table1[i][j]=table_first_Value[k1];
table2[i][j]=table_first_Value[k2];
}
}
for(i=0;i<11;i++)
{
sort_Bubble(table1[i],5);
sort_Bubble(table2[i],5);
}
for(i=0;i<11;i++)
{
max1[i]=max_Table(table1[i],5);
max2[i]=max_Table(table2[i],5);
min1[i]=min_Table(table1[i],5);
min2[i]=min_Table(table2[i],5);
sum1[i]=sum_Table(table1[i],5);
sum2[i]=sum_Table(table2[i],5);
dif1[i]=average_Table(table1[i],5);
dif2[i]=average_Table(table2[i],5);
dif_m1[i]=average_MinMax(table1[i],5);
dif_m2[i]=average_MinMax(table2[i],5);
aver1[i]=difference_Average(table1[i],5);
        aver2[i]=difference_Average(table2[i],5);
}


}
void __fastcall TForm1::delete_Table()
{
int i;
delete table_first_Value;
delete min1;
delete min2;
delete max1;
delete max2;
delete sum1;
delete sum2;
delete dif1;
delete dif2;
delete dif_m1;
delete dif_m2;
delete aver1;
delete aver2;
for(i=0;i<11;i++)
{
delete []  table1[i];
delete []  table2[i];
}
delete [] *table1;
delete [] *table2;

}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Random();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
 init_Table();
 int i,j;
 for(i=0;i<5;i++)
 {
for(j=0;j<11;j++)
{
StringGrid1->Cells[i][j]=IntToStr(table1[j][i]);
StringGrid3->Cells[i][j]=IntToStr(table2[j][i]);


}
 }
 for(i=0;i<11;i++)
 {
StringGrid2->Cells[6][i]=IntToStr(i+1);
StringGrid4->Cells[6][i]=IntToStr(i+1);
StringGrid2->Cells[0][i]=IntToStr(max1[i]);
StringGrid4->Cells[0][i]=IntToStr(max2[i]);
StringGrid2->Cells[1][i]=IntToStr(min1[i]);
StringGrid4->Cells[1][i]=IntToStr(min2[i]);
StringGrid2->Cells[2][i]=IntToStr(sum1[i]);
StringGrid4->Cells[2][i]=IntToStr(sum2[i]);
StringGrid2->Cells[3][i]=FloatToStr(dif1[i]);
StringGrid4->Cells[3][i]=FloatToStr(dif2[i]);
StringGrid2->Cells[4][i]=FloatToStr(dif_m1[i]);
StringGrid4->Cells[4][i]=FloatToStr(dif_m2[i]);
StringGrid2->Cells[5][i]=FloatToStr(aver1[i]);
StringGrid4->Cells[5][i]=FloatToStr(aver2[i]);
 }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
 delete_Table();
 Close();
}
//---------------------------------------------------------------------------

środa, 14 czerwca 2017

Example of simple list

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

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


#define max_value 100

using namespace std;

void init_Random()
{
 srand(time(NULL));
}

typedef struct temporary_next
{
 temporary_next *next;
 int value_str;
}position;
bool is_First(int x)
{
 int i,sum;
 sum=0;
 for(i=1;i<x+1;i++)
  if(x%i==0)
   ++sum;
  if(sum==2)
   return true;
  else
   return false;
}

int *first_digit_Array;
class list_Direction
{
 private:
 typedef struct
 {
  position *start;
  position *end;
 }show;
 show info_show;
 public:
 list_Direction()
 {
  info_show.start=info_show.end=NULL;
 }
 void plus_next(int value)
 {
  position *new_pos;
  new_pos=new position;
  new_pos->value_str=value;
  new_pos->next=NULL;
  if(info_show.start==NULL)
   info_show.start=info_show.end=new_pos;
  else
   {
    (info_show.end)->next=new_pos;
    info_show.end=new_pos;
   }

 }
 list_Direction&  operator --()
 {
  if(info_show.start==info_show.end)
  {
   delete info_show.start;
   info_show.start=info_show.end=NULL;

  }
  else
  {
   position *temp_position=info_show.start;
   while((temp_position->next)!=info_show.end)
    info_show.end=temp_position;
   delete temp_position->next;
   temp_position->next=NULL;
  }
  return (*this);
 }
 list_Direction friend& operator +(list_Direction &A, list_Direction &B)
 {
  list_Direction *temp_List=new list_Direction;
  position *C=(A.info_show).start;
  position *D=(B.info_show).start;
  do
  {
   temp_List->plus_next(C->value_str);
   C=C->next;
  }while(C!=NULL);
  do
  {
   temp_List->plus_next(D->value_str);
   D=D->next;
  }while(D!=NULL);
  return (*temp_List);
 }
 bool is_Empty()
 {
  if(info_show.start==NULL)
   return true;
  else
   return false;

 }
 void write_Elements()
 {
  position *temp_pos=info_show.start;
  while(temp_pos!=NULL)
  {
    printf("%d  ",temp_pos->value_str);
    temp_pos=temp_pos->next;
    printf("\n");
  }
  printf("\n");
 }

 ~list_Direction()
 {
  while(!is_Empty())
   (*this)--;
   ;
 }

};
int main(int argc, char* argv[])
{
  first_digit_Array=new int[max_value];
  int i,j;
  i=0;
  j=2;
  do
  {
   if(is_First(j))
   {
    first_digit_Array[i]=j;
    ++i;
   }
   ++j;
  }while(i<max_value);
  list_Direction X,Y,Z;
  for(i=0;i<max_value;i++)
  {
   X.plus_next(first_digit_Array[rand()%max_value]);
   Y.plus_next(first_digit_Array[rand()%max_value]);
  }
  Z=X+Y;
  Z.write_Elements();

  delete first_digit_Array;
 getch();
        return 0;
}

wtorek, 13 czerwca 2017

Encoding data from a file (linux/g++)

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define path_file "/proc/cpuinfo"
#define new_path_file "cpuinfo_coding"

using namespace std;

void init_Random()
{
    srand(time(NULL));
}
char int_to_Ascii(int x)
{
    return char(x);
}
void double_Code(char *test_file)
{
    int value_ascii,i;
    char character;
    value_ascii=rand()%255;
    character=int_to_Ascii(value_ascii);
    for(i=0;test_file[i]!=NULL;i++)
     test_file[i]=test_file[i]^character;
    for(i=0;test_file[i]!=NULL;i++)
     test_file[i]=255-test_file[i];      
}
int main(int argc, char **argv)
{
 init_Random();   
 char *temporary_bufor;
 temporary_bufor=new char[2048];   
 std::fstream test_file( path_file, std::ios::in );
 test_file.read(temporary_bufor,2048);
 test_file.close();
 cout<<"Before coding:\n";
 cout<<temporary_bufor<<endl;
 double_Code(temporary_bufor);
 cout<<"\n\nAfter coding:\n\n";

 cout<<temporary_bufor<<endl;
 int new_file=open(new_path_file,O_WRONLY | O_CREAT | O_APPEND, 0666);
 size_t length_file=strlen(temporary_bufor);
 write(new_file,temporary_bufor,length_file);
 close(new_file);
 delete temporary_bufor;
 return 0;
}

piątek, 2 czerwca 2017

Classes with file creation and adding time values (linux - g++)

#include <iostream>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
using namespace std;


class now_Time
{
    private:
    time_t my_time;
    char *now_time;
    public:
    now_Time()
    {
        my_time=time(NULL);
        now_time=asctime(localtime(&my_time));
    }
    char *give_Time()
    {
        return now_time;
    }
};
class uptime_Time
{
    private:
    FILE *uptime_file;
    char *path;
    double real_time;
    double free_time;
    public:
    uptime_Time()
    {
        path="/proc/uptime";
        uptime_file=fopen(path,"r");
        fscanf(uptime_file,"%1f %1f\n",&real_time,&free_time);
    }
    int give_real_Time()
    {
        return (int) real_time;
    }
    int give_free_Time()
    {
        return (int) free_time;
    }
};

class new_file_with_Infos
{
    private:
    char *name_file;
    size_t size_class_time;
    size_t size_free_time;
    size_t size_real_time;
    int file;
    mode_t mode_value;
    public:
    new_file_with_Infos(char *name)
    {
        name_file=name;
        mode_value=S_IRUSR | S_IROTH | S_IWGRP | S_IRGRP | S_IWUSR;
        file=open(name_file, O_WRONLY | O_EXCL | O_CREAT, mode_value);
        now_Time nT;
        uptime_Time uT;
        size_class_time=strlen(nT.give_Time());
        char *temp_free_time;
        char *temp_real_time;
        snprintf(temp_free_time,sizeof(temp_free_time),"%d",uT.give_free_Time());
        size_free_time=strlen(temp_free_time);
        snprintf(temp_real_time,sizeof(temp_real_time),"%d",uT.give_real_Time());
        size_real_time=strlen(temp_real_time);
   
        write(file,nT.give_Time(),size_class_time);
        write(file,temp_free_time,size_free_time);
        write(file,temp_real_time,size_real_time);
        close(file);
    }
};

czwartek, 1 czerwca 2017

Two useful classes for linux (g++)

#include <iostream>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
using namespace std;


class copy_FILES
{
    private:
    struct stat st_value;
    int file_begin;
    int file_end;
    char *File1;
    char *File2;
    off_t off_value;
    public:
    copy_FILES(char *f1, char *f2)
    {
        off_value=0;
        File1=f1;
        File2=f2;
        file_begin=open(File1,O_RDONLY);
        fstat(file_begin,&st_value);
        file_end=open(File2,O_WRONLY | O_CREAT,st_value.st_mode);
        sendfile(file_end,file_begin,&off_value,st_value.st_size);
        close (file_begin);
        close (file_end);
    }
};
class show_Directory
{
    private:
    size_t path_size;
    DIR *directory;
    char *path_name;
    struct dirent *dirent_value;
    char path_max[PATH_MAX+1];
    public:
    show_Directory(char *path_directory)
    {
        char i='/';
        char j='\0';
       
        path_name=path_directory;
        strncpy(path_max,path_name,sizeof(path_max));
        path_size=strlen(path_name);
        if(path_max[path_size-1]!=i)
        {
            path_max[path_size]=i;
            path_max[path_size+1]=j;
            ++path_size;
        }
    }
    void Write()
    {
        directory=opendir(path_name);
        while((dirent_value=readdir(directory))!=NULL)
        {
            strncpy(path_max+path_size,dirent_value->d_name,
            sizeof(path_max)-path_size);
            cout<<path_max<<endl;
        }
        closedir(directory);
    }
};
int main(int argc, char **argv)
{
   
    show_Directory sD("/proc/");
    sD.Write();
    copy_FILES cF("/proc/cpuinfo","my_new_file_cpu");
    return 0;
}