niedziela, 27 października 2019

OS version (linux C++)

#include <iostream>
#include <stdio.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>

using namespace std;

class info_Name
{
private:
char *my_File;
char *os_Machine,*os_Version,*os_Release,*os_Name;
double free_Ram,total_Ram;
struct utsname my_Os;
struct sysinfo my_RAM;
public:
info_Name(char *file_Name)
{
my_File=file_Name;
uname(&my_Os);
sysinfo(&my_RAM);
os_Name=my_Os.sysname;
os_Release=my_Os.release;
os_Version=my_Os.version;
os_Machine=my_Os.machine;
total_Ram=my_RAM.totalram/(1024.*1024.);
free_Ram=my_RAM.freeram/(1024.*1024.);
}
void Save()
{
FILE* save_File;
save_File=fopen(my_File,"wt");
fprintf(save_File,"%f\n",total_Ram);
fprintf(save_File,"%f\n",free_Ram);
fprintf(save_File,"%s\n",os_Name);
fprintf(save_File,"%s\n",os_Release);
fprintf(save_File,"%s\n",os_Version);
fprintf(save_File,"%s\n",os_Machine);
fclose(save_File);
}
};

int main(int argc, char **argv)
{
info_Name my_Version("version.txt");
my_Version.Save();
return 0;
}

piątek, 18 października 2019

Array manipulation (C++ Builder)



private: // User declarations
        int **A,**B,**C,**D, random_Col;
        void __fastcall init_Arrays();
        void __fastcall destroy_Arrays();
        int* __fastcall change_Position(int *tab, int size_Tab);
        void __fastcall max_Sort(int *tab, int tab_Size);
        void __fastcall min_Sort(int *tab, int tab_Size);


int* __fastcall TForm1::change_Position(int *tab, int size_Tab)
{
 int *result_Tab=new int[size_Tab];
 int i;
 for(i=0;i<size_Tab;i++)
 {
  result_Tab[i]=tab[(size_Tab-1)-i];

 }
 return result_Tab;
}
void __fastcall TForm1::max_Sort(int *tab, int tab_Size)
{
  int i,j,k;
    for(i=1;i<tab_Size;i++)
    {
        j=i;
        k=tab[j];
        while((j>0) && (tab[j-1]<k))
        {
            tab[j]=tab[j-1];
            j--;
        }
        tab[j]=k;
    }
}
void __fastcall TForm1::min_Sort(int *tab, int tab_Size)
{
  int i,j,k;
    for(i=1;i<tab_Size;i++)
    {

        j=i;
        k=tab[j];
        while((j>0) && (tab[j-1]>k))
        {
            tab[j]=tab[j-1];
            j--;
        }
        tab[j]=k;
    }
}

void __fastcall TForm1::init_Arrays()
{
 int i,j,k,sum;
 srand(time(NULL));
 A=new int *[5];
 B=new int *[5];
 C=new int *[5];
 D=new int *[5];
 for(i=0;i<5;i++)
 {
  A[i]=new int[10];
  B[i]=new int[10];
  C[i]=new int[10];
  D[i]=new int[10];
 }

 random_Col=50+rand()%50;
 Edit1->Text=IntToStr(random_Col);
 i=0;
 do
 {
  do
  {
    sum=0;
   for(j=0;j<10;j++)
   {
    k=rand()%10+1;
    sum+=k;
    A[i][j]=k;
   }
   }while(sum!=random_Col);
   ++i;

 }while(i<5);
  for(i=0;i<5;i++)
 {
  B[i]=change_Position(A[i],10);

 }
 for(i=0;i<5;i++)
 {
  for(j=0;j<10;j++)
  {
   C[i][j]=A[i][j];
   D[i][j]=B[i][j];
  }
 }
 for(i=0;i<5;i++)
 {
  min_Sort(C[i],10);
  max_Sort(D[i],10);
 }


  for(i=0;i<5;i++)
  {
   for(j=0;j<10;j++)
   {
    StringGrid1->Cells[j][i]=IntToStr(A[i][j]);
    StringGrid2->Cells[j][i]=IntToStr(B[i][j]);
    StringGrid3->Cells[j][i]=IntToStr(C[i][j]);
    StringGrid4->Cells[j][i]=IntToStr(D[i][j]);
   }
  }



}
void __fastcall TForm1::destroy_Arrays()
{
 int i;
 for(i=0;i<5;i++)
 {
  delete [] D[i];
  delete [] C[i];
  delete [] B[i];
  delete [] A[i];
 }
 delete [] A;
 delete [] B;
 delete [] C;
 delete [] D;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Arrays();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
 destroy_Arrays();
}
        

czwartek, 17 października 2019

Data and file (C++ Builder)

struct DATE_
{
int year;
UnicodeString month_name;
int number_month;
int number_day;
UnicodeString day_name;
}my_Date;

UnicodeString __fastcall TForm1::day_of_Week(int day)
{
UnicodeString result;
switch (day)
{
case 1: result="Sunday"; break;
case 2: result="Monday"; break;
case 3: result="Tuesday"; break;
case 4: result="Wednesday";break;
case 5: result="Thursday"; break;
case 6: result="Friday";break;
case 7: result="Saturday"; break;


default: result="error";break;
}
return result;

}
UnicodeString __fastcall TForm1::month_of_Year(int month)
{
UnicodeString result;
switch (month)
{
case 1: result="January"; break;
case 2: result="February"; break;
case 3: result="March"; break;
case 4: result="April"; break;
case 5: result="May";break;
case 6: result="June";break;
case 7: result="July";break;
case 8: result="August";break;
case 9: result="September"; break;
case 10: result="Oktober"; break;
case 11: result="November"; break;
case 12: result="December"; break;
default: result="error";break;
}
return result;
}
wchar_t* __fastcall TForm1::read_from_Datefile()
{
ifstream file_Date;
wchar_t *buf=new wchar_t[128];
file_Date.open("today_date.txt");
for(int i=0;(i<128) && (file_Date);i++)
{
buf[i]=file_Date.get();
}
file_Date.close();
    return buf;

}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  unsigned short a[4];
  DecodeDateFully(Now(),a[0],a[1],a[2],a[3]);
  my_Date.year=int(a[0]);
  my_Date.month_name=month_of_Year(int(a[1]));
  my_Date.number_month=int(a[1]);
  my_Date.number_day=int(a[2]);
  my_Date.day_name=day_of_Week(int(a[3]));
  FILE *save_date_project;
  save_date_project=fopen("today_date.txt","w");
  fprintf(save_date_project,"%d\n",my_Date.year);
  fprintf(save_date_project,"%s\n",my_Date.month_name.w_str());
  fprintf(save_date_project,"%d\n",my_Date.number_day);
  fprintf(save_date_project,"%s\n",my_Date.day_name.w_str());
  fclose(save_date_project);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ShowMessage(UnicodeString(read_from_Datefile()));
}

wtorek, 15 października 2019

Copying a group of files (C++ Linux)

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



using namespace std;



void COPY(string* files1, string *files2, int how_Many)
{
 int i;
 struct stat* buf_Files;
 int *from_F,*to_F;
 off_t *set;
 buf_Files=new struct stat[how_Many];
 from_F=new int[how_Many];
 to_F=new int[how_Many];
 set=new off_t[how_Many];
 for(i=0;i<how_Many;i++)
 {
from_F[i]=open(files1[i].c_str(),O_RDONLY);
fstat(from_F[i],&buf_Files[i]);
to_F[i]=open(files2[i].c_str(),O_WRONLY | O_CREAT, buf_Files[i].st_mode);
sendfile(to_F[i],from_F[i],&set[i],buf_Files[i].st_size);
close(from_F[i]);
close(to_F[i]);
 }
 delete set;
 delete to_F;
 delete from_F;
 delete buf_Files;
}

int main(int argc, char **argv)
{
 string to_Files[5]={"home/file1","home/file2","home/file3","home/file4","home/file5"};
 string from_Files[5]={"proc/zoneinfo","proc/vstat","proc/version","proc/uptime","proc/stat"};
 COPY(from_Files,to_Files,5);

return 0;
}

niedziela, 13 października 2019

Data from /proc/meminfo (Linux C++)


#include <iostream>
#include <string.h>
#include <stdio.h>
#define size_B 1024
using namespace std;

struct mem_I
{
double Slab;
double SReclaimable;
double SUnreclaim;
double KernelStack;
double PageTables;

};
char mem_I_Val1[5][20]={"Slab","Sreclaimable","SUnreclaim","KernelStack","PageTables"};
char mem_I_Val2[5][20]={"Slab : %f","Sreclaimable : %f","SUnreclaim %f","KernelStack %f","PageTables %f"};

float write_Value(char *text1, char *text2)
{
size_t read_Size;
float result;
char  buff[size_B];
char *temp;
FILE *test_File;
test_File=fopen("/proc/meminfo","r");
read_Size=fread(buff,1,sizeof(buff),test_File);
fclose(test_File);
buff[read_Size]='\0';
temp=strstr(buff,text1);
if(temp==NULL)
return 0;
sscanf(temp,text2,&result);
return result;


}

int main(int argc, char **argv)
{
int i;
float results[5];
for(i=0;i<5;i++)
{
results[i]=write_Value(mem_I_Val1[i],mem_I_Val2[i]);
}
for(i=0;i<5;i++)
{
cout<<mem_I_Val1[i]<<" - "<<results[i]<<" kB"<<endl;
}
return 0;
}

piątek, 11 października 2019

fprintf, fscanf - example

#define SIZE_TAB 20
#define MAX_VALUE SIZE_TAB*SIZE_TAB


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

#include <iostream>
using namespace std;


double *finish_Result;
int main_Tab[SIZE_TAB][SIZE_TAB];
int first_Values[MAX_VALUE];
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_Arrays()
{
int i,j,k;
i=0,j=2;
do
{
if(is_First(j))
{
first_Values[i]=j;
++i;
}
++j;
}while(i<MAX_VALUE);
for(i=0;i<SIZE_TAB;i++)
{
for(j=0;j<SIZE_TAB;j++)
{
k=rand()%MAX_VALUE;
main_Tab[i][j]=first_Values[k];
}
}

}

int min_Element(int *tab, int size)
{
int min_E,i;
min_E=tab[0];
for(i=1;i<size;i++)
if(min_E>tab[i])
  min_E=tab[i];
return min_E;
}
int max_Element(int *tab, int size)
{
int max_E,i;
max_E=tab[0];
for(i=1;i<size;i++)
if(max_E<tab[i])
  max_E=tab[i];
return max_E;
}
int sum_Array(int *tab, int size)
{
 int i,sum;
 sum=0;
 for(i=0;i<size;i++)
  sum+=tab[i];
 return sum;
}

double aver_Array(int *tab, int size)
{
double aver;
aver=(1.0*sum_Array(tab,size))/(1.0*size);
return aver;
}

double func_A(int a, int b, int c, double d)
{
double func;
func=(c*1.0)-((1.0*a)+(1.0*b))*d;
return func;
}

struct info_Array
{
int a_Min;
int a_Max;
int a_Sum;
double a_Aver;
};
 int main()
{
 int i,j;
 FILE *base_File,*result_File,*read_Date;
 srand(time(NULL));
 finish_Result=new double[SIZE_TAB];
 struct info_Array *i_Array=new struct info_Array[SIZE_TAB];
 struct info_Array *j_Array=new struct info_Array[SIZE_TAB];
 init_Arrays();
 for(i=0;i<SIZE_TAB;i++)
 {
i_Array[i].a_Min=min_Element(main_Tab[i],SIZE_TAB);
i_Array[i].a_Max=max_Element(main_Tab[i],SIZE_TAB);
i_Array[i].a_Sum=sum_Array(main_Tab[i],SIZE_TAB);
i_Array[i].a_Aver=aver_Array(main_Tab[i],SIZE_TAB);
 }

 base_File=fopen("base.txt","w");
 for(i=0;i<SIZE_TAB;i++)
 {
for(j=0;j<SIZE_TAB;j++)
{
fprintf(base_File,"%d  ",main_Tab[i][j]);

}
fprintf(base_File,"\n");
 }

 fclose(base_File);
 result_File=fopen("result.txt","w");
 for(i=0;i<SIZE_TAB;i++)
 {
fprintf(result_File,"%d %d %d %f ",i_Array[i].a_Min,i_Array[i].a_Max,
i_Array[i].a_Sum,i_Array[i].a_Aver);
fprintf(result_File,"\n");
 }
 fclose(result_File);
 read_Date=fopen("result.txt","r");
 for(i=0;i<SIZE_TAB;i++)
 {
fscanf(read_Date,"%d %d %d %f ",&j_Array[i].a_Min,&j_Array[i].a_Max,
&j_Array[i].a_Sum,&j_Array[i].a_Aver);
 }
 fclose(read_Date);
 for(i=0;i<SIZE_TAB;i++)
 {
  finish_Result[i]=func_A(j_Array[i].a_Min,j_Array[i].a_Max,j_Array[i].a_Sum,
  j_Array[i].a_Aver);
 }
 for(i=0;i<SIZE_TAB;i++)
 {
     cout<<i+1<<") "<<finish_Result[i]<<endl;
 }
 delete i_Array;
 delete j_Array;
 delete finish_Result;

 getch();
return 0;
}

poniedziałek, 7 października 2019

Encrypting text files (C++ Linux)

#include <iostream>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define max_F 65535
#define max_Char 256
#define file_Size 1024
using namespace std;

class pad_Files
{
private:
size_t* length_Buffer;
char *old_file,*new_file;
char *base;
struct stat file_Value;
void rand_Char(char *text1)
{
int i;
for(i=0;i<max_F;i++)
{
text1[i]=(char)rand()%max_Char+1;
text1[i]=text1[i]^(char)rand()%max_Char+1;
}
}
char *read_File(char *filename)
{
int file;
char *result;
file=open(filename,O_RDONLY);
fstat(file,&file_Value);
*length_Buffer=file_Value.st_size;
result=new char[*length_Buffer];
read(file,result,*length_Buffer);
close(file);
return result;
}
public:
pad_Files(char *file1,char *file2)
{
int file;
memset(base,0,max_F);
old_file=file1;
new_file=file2;
base=read_File(old_file);
rand_Char(base);
file=open(new_file,O_WRONLY | O_CREAT);
write(file,(char*)&length_Buffer,sizeof(length_Buffer));
write(file,base,*length_Buffer);
close(file);

}
};


int main(int argc, char **argv)
{
 srand(time(NULL));
 pad_Files p_File("file1.txt","file2.txt");
 
return 0;
}