wtorek, 30 sierpnia 2016

Sums modulo 2 and first numbers

#include <iostream>
#define max 10
#define two 2
using namespace std;

class Byte_First
{
private:
unsigned char tab[max];
unsigned char tab_traditional[max];
int *first_tab;
int *first_tab_traditional;
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;
}
public:
Byte_First()
{
int i,j;
first_tab=new int[max];
first_tab_traditional=new int[max];
i=0;j=1;
do
{
if(is_first(j))
{
first_tab[i]=j;
++i;
}
++j;
}while(i<max);
j=1;
for(i=0;i<max;i++)
{
tab[i]=first_tab[i];
first_tab_traditional[i]=j;
j*=two;
tab_traditional[i]=first_tab_traditional[i];
 
}

    }
    ~Byte_First()
    {
delete first_tab_traditional;
delete first_tab;
}
    void Write_Byte(int next_first)
    {
int i,j,my_byte;
for(i=max-1;i>=0;i--)
{
my_byte=(tab[i]&next_first);
if(my_byte!=0)
cout<<'1';
else
cout<<'0';
}

}
void Write_Byte_Traditional(int next_first)
{
int i,j,my_byte;
for(i=max-1;i>=0;i--)
{
my_byte=(tab_traditional[i]&next_first);
if(my_byte!=0)
cout<<'1';
else
cout<<'0';
}
}
void Show()
{
int i,j;
for(i=1;i<max+1;i++)
{
j=~i;
Write_Byte_Traditional(i); cout<<"\t"; Write_Byte(i);cout<<" negation ";Write_Byte_Traditional(j);cout<<"\t";Write_Byte(j);cout<<endl;
}
}

};

int main(int argc, char** argv)
{
Byte_First FF;
FF.Show();

return 0;
}

piątek, 26 sierpnia 2016

Fitrst numbers in ListBox C++ Builder



private: // User declarations
        bool __fastcall is_First(int x);
        int *tab;
        int *difference_value;
        int *value_plus_one;
        void __fastcall init_Tab();
        void __fastcall destroyed_Tab();

#include <vcl.h>
#pragma hdrstop

#include "m_first01.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define max 5000
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

bool __fastcall TForm1::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;
}
void __fastcall TForm1::init_Tab()
{
 tab=new int[max];
 difference_value=new int[max-1];
 value_plus_one=new int[max-1];
}
void __fastcall TForm1::destroyed_Tab()
{
 delete difference_value;
 delete value_plus_one;
 delete tab;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 destroyed_Tab();
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Tab();
 int i,j;
 i=0;j=0;
 do
 {
  if(is_First(j))
  {
   tab[i]=j;
   ++i;
  }
  ++j;
 }while(i<max);

 for(i=-1,j=i+1;i<max-1;i++,j++)
 {
  difference_value[i+1]=tab[j+1]-tab[i+1];
  value_plus_one[i+1]=difference_value[i+1]+1;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 int i;
 ListBox1->Items->Clear();
 for(i=0;i<max;i++)
 {
  ListBox1->Items->Add(IntToStr(i+1)+")   "+IntToStr(tab[i]));
 }
 ListBox2->Items->Add("--------");
 for(i=0;i<max-1;i++)
 {
  ListBox2->Items->Add(IntToStr(i+1)+")   "+IntToStr(tab[i])+"   -   "+IntToStr(difference_value[i])+"   -   "+IntToStr(value_plus_one[i]));
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 int i;
 ListBox3->Items->Clear();
 i=0;
 do
 {
   if((!is_First(difference_value[i])) && (!is_First(value_plus_one[i])))
   {
    ListBox3->Items->Add(IntToStr(tab[i])+"  -  "+IntToStr(difference_value[i])+"  - "+IntToStr(value_plus_one[i]));
   }
   ++i;
 }while(i<max-1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
 int i;
 ListBox4->Items->Clear();
 i=0;
 do
 {
   if((is_First(difference_value[i])) && (is_First(value_plus_one[i])))
   {
    ListBox4->Items->Add(IntToStr(tab[i])+"  -  "+IntToStr(difference_value[i])+"  - "+IntToStr(value_plus_one[i]));
   }
   ++i;
 }while(i<max-1);


}
//---------------------------------------------------------------------------


niedziela, 21 sierpnia 2016

Sysinfo os in class

#include <iostream>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;

class System_Info
{
private:
struct sysinfo s_info;
struct utsname u_name;
struct Version
{
char *a;
char *b;
char *c;
char *d;
};
uid_t my_uid;
gid_t my_gid;
public:
System_Info()
{
uname(&u_name);
sysinfo(&s_info);
my_uid=geteuid();
my_gid=getegid();
 
}
void write_Info()
{
struct Version My_Info;
My_Info.a=u_name.sysname;
My_Info.b=u_name.release;
My_Info.c=u_name.version;
My_Info.d=u_name.machine;
cout<<"How many process : "<<s_info.procs<<endl;
cout<<"Total RAM: "<<s_info.totalram/(1024*1024)<<endl;
cout<<"Free RAM : "<<s_info.freeram/(1024*1024)<<endl;
cout<<"Information of os system:\n";
cout<<My_Info.a<<endl;
cout<<My_Info.b<<endl;
cout<<My_Info.c<<endl;
cout<<My_Info.d<<endl;
cout<<"Number identification user and group:\n";
cout<<int(my_uid)<<"    "<<int(my_gid)<<endl;

}
 
};

int main(int argc, char** argv)
{
System_Info My_Os;
My_Os.write_Info();
return 0;
}

sobota, 20 sierpnia 2016

first numbers and structs


#include <iostream>

#include <stdio.h>
#define max 100

using namespace std;



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 how_many_first(int first)
{
    int i,sum;
    sum=0;
    for(i=1;i<first;i++)
     if(is_first(i))
      ++sum;
    return sum;
}
int sum_first(int first)
{
    int i,sum;
    sum=0;
    for(i=1;i<first;i++)
     if(is_first(i))
      sum+=i;
    return sum;
}
double down_zero(double x, double y)
{
    double z;
    if(x<y)
     z=x/y;
    else
     z=y/x;
    return z;
   
}
double up_zero(double x, double y)
{
    double z;
    if(x>y)
    z=x/y;
    else
    z=y/x;
    return z;
}
double div_value(double x, double y)
{
    return x/y;
}
double one_value(double x, double y)
{
    return up_zero(x,y)*down_zero(x,y);
}
int *tab_first;
int *temp_tab;
struct first_functions
{
    int size;
    int  div_sum;
    double up_first;
    double down_first;
};
struct result
{
    double one_zero;
    int how_many;
    int m_sum;
};

int main(int argc, char **argv)
{
   
    tab_first=new int[max];
    temp_tab=new int[max];
    struct first_functions *first_fun=new struct first_functions[max];
    struct result *first_result=new struct result[max];
    int i,j;
    i=0;
    j=0;
    do
    {
        if(is_first(j))
        {
            tab_first[i]=j;
            ++i;
        }
        ++j;
    }while(i<max);
    for(i=0;i<max;i++)
    {
        first_fun[i].size=how_many_first(tab_first[i]);
        first_fun[i].div_sum=sum_first(tab_first[i]);
       
       
    }
    double temp1[max];
    for(j=0;j<max;j++)
    {
        temp1[j]=1.0*tab_first[j];
    }
    for(i=1;i<max+1;i++)
    {
        first_fun[i-1].down_first=down_zero(temp1[i-1],temp1[i]);
        first_fun[i-1].up_first=up_zero(temp1[i-1],temp1[i]);
    }
   
    for(i=0;i<max;i++)
    {
        first_result[i].one_zero=one_value(first_fun[i].down_first,first_fun[i].up_first)*
        one_value(1.*first_fun[i].div_sum,div_value(1.,first_fun[i].div_sum))*
        one_value(1.*first_fun[i].size,div_value(1.,first_fun[i].size))*
        tab_first[i];
        first_result[i].how_many=first_fun[i].size;
        first_result[i].m_sum=first_fun[i].div_sum;
        cout<<first_result[i].one_zero<<"    "<<first_result[i].how_many<<"   "<<first_result[i].m_sum<<endl;       
       
    }
   
   
    delete temp_tab;
    delete first_result;
    delete first_fun;
    delete tab_first;
    return 0;
}

niedziela, 14 sierpnia 2016

sqrt and random

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define max 20
#define m_first 20
#define value 5
using namespace std;

void init_random()
{
    srand(time(NULL));
}
struct Point
{
    double x;
    double y;
    double z;
    double v;
   
};
double unresult_a(double x,double y)
{
    return (1./x)/(1./y);
}
double unresult_b(double x, double y)
{
   
    return (x/unresult_a(x,y));
}
double unresult_c(double x, double y)
{
    return (x/unresult_b(x ,y));
}
double unresult_d(double x, double y)
{
    return (x/unresult_c(x,y));
}
double over_zero(double x, double y)
{
    if(x<y)
     return (y-x);
    else
     return (x-y);
}
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 main(int argc, char **argv)
{
    double *sqrt_table=new double[m_first];
    double *sum_pointer=new double[value];
    double *random_sum_pointer=new double[value];
    double *my_result=new double[value];
    struct Point *Pointer=new struct Point[max];
    struct Point *random_Pointer=new struct Point[max];
   
    init_random();
    int i,j;
    i=0;
    j=1;
    do
    {
        if(is_first(j))
        {
            sqrt_table[i]=sqrt(1.0*j);
            ++i;
        }
        ++j;
    }while(i<m_first);
    for(i=0;i<value;i++)
    {
        sum_pointer[i]=0.0;
        random_sum_pointer[i]=0.0;
        my_result[i]=0.0;
    }
    for(i=0;i<max;i++)
    {
        Pointer[i].x=unresult_a(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[0]+=Pointer[i].x;
        random_Pointer[i].x=unresult_a(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[0]+=random_Pointer[i].x;
        my_result[0]+=over_zero(sum_pointer[0],random_sum_pointer[0]);
        Pointer[i].y=unresult_b(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[1]+=Pointer[i].y;
        random_Pointer[i].y=unresult_b(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[1]+=random_Pointer[i].y;
        my_result[1]+=over_zero(sum_pointer[1],random_sum_pointer[1]);
        Pointer[i].z=unresult_c(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[2]+=Pointer[i].z;
        random_Pointer[i].z=unresult_c(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[2]+=random_Pointer[i].z;
        my_result[2]+=over_zero(sum_pointer[2],random_sum_pointer[2]);
        Pointer[i].v=unresult_d(sqrt_table[i],sqrt_table[max-(i+1)]);
        sum_pointer[3]+=Pointer[i].v;
        random_Pointer[i].v=unresult_d(sqrt_table[rand()%max],sqrt_table[rand()%max]);
        random_sum_pointer[3]+=random_Pointer[i].v;
        my_result[3]+=over_zero(sum_pointer[3],random_sum_pointer[3]);
        sum_pointer[4]+=((sum_pointer[0]+sum_pointer[1]+sum_pointer[2]+sum_pointer[3])/4);
        random_sum_pointer[4]+=((random_sum_pointer[0]+random_sum_pointer[1]+random_sum_pointer[2]+
        random_sum_pointer[3])/4.);
        my_result[4]+=over_zero(sum_pointer[4],random_sum_pointer[4]);
       
    }
    for(i=0;i<value;i++)
    {
        cout<<sum_pointer[i]<<"   "<<random_sum_pointer[i]<<"   "<<my_result[i]<<endl;
    }
    delete random_Pointer;
    delete Pointer;
    delete random_sum_pointer;
    delete my_result;
    delete sum_pointer;
    delete sqrt_table;
   
    return 0;
}

środa, 10 sierpnia 2016

Creat new files in class and next copy to files

vim new_files.cpp



#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <fstream>
#define min 3
#define max 7
#define max_files 100
using namespace std;


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


class New_files_copy
{
private:
char array_files[max_files][max];
char upper_letters[26];
char lower_letters[26];
int how_many;
int files[max_files];

public:
New_files_copy(char *main_file, int how_many_new_files)
{
char c,d;
int i,j,size,temp,letters;
how_many=how_many_new_files;


         

for(i=0,c='A',d='a';i<26;i++,c++,d++)
{
upper_letters[i]=c;
lower_letters[i]=d;
}
for(i=0;i<how_many;i++)
{
size=rand()%max+min;
for(j=0;j<size;j++)
{
temp=rand()%2;
letters=rand()%26;
if(temp==0)
 array_files[i][j]=upper_letters[letters];
else
 array_files[i][j]=lower_letters[letters];
 
}
array_files[i][size]='\0';
}
for(i=0;i<how_many;i++)
{
files[i]=open(array_files[i],O_RDONLY | O_CREAT | O_EXCL);
}
for(i=0;i<how_many;i++)
{
close(files[i]);
}
for(i=0;i<how_many;i++)
{
files[i]=open(array_files[i],O_WRONLY);
fstream file_class;
file_class.open(main_file);
while(!file_class.eof())
{
string tekst;
getline(file_class,tekst);
write(files[i],tekst.c_str(),1024);

}
}
for(i=0;i<how_many;i++)
{
close(files[i]);
}


printf("My new files:\n");
for(i=0;i<how_many;i++)
{
printf("%s",array_files[i]);
printf("\n");

}
}

};
int main(int argc, char** argv)
{
init_random();
New_files_copy Combat("new_files.cpp",10);

return 0;
}

środa, 3 sierpnia 2016

C++ Builder, StringGrid and table

My file with date - "date.h"

#include <iostream>
#define size 10
using namespace std;
int **main_tab;

int **tab_temp2;
int **tab_temp3;



void initialization_Tab()
{
 int i;
 //use in functions FormCreate
 main_tab=new int *[size];

 tab_temp2=new int *[size];
 tab_temp3=new int *[size];


 for(i=0;i<size;i++)
 {
  main_tab[i]=new int[size];

  tab_temp2[i]=new int[size];
  tab_temp3[i]=new int[size];

 }
}

void deleyted_Tab()
{
 //use in function click button Close
 int i;
 for(i=0;i<size;i++)
 {
  delete [] main_tab[i];
  delete tab_temp2[i];
  delete tab_temp3[i];

 }
 delete [] *main_tab;
 delete [] *tab_temp2;
 delete [] *tab_temp3;

}

My functions and libraries:

#include <vcl.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>

#include "date.h"

void __fastcall TForm1::init_Random()
{
 srand(time(NULL));
}
AnsiString __fastcall TForm1:: char_for_Value(int x)
{
 if(x>0 && x<3)
  return "A";
 else if(x>2 && x<5)
  return "B";
 else if(x>4 && x<7)
  return "C";
 else if(x>6 && x<9)
  return "D";
 else return "E";


}

My form in C++ Builder:

Code: 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int i,j;
  for(i=0;i<size;i++)
  {
   for(j=0;j<size;j++)
   {
    main_tab[i][j]=rand()%10+1;
    StringGrid1->Cells[i][j]=IntToStr(main_tab[i][j]);
    if(main_tab[i][j]%2==0)
     tab_temp2[i][j]=0;
    else
     tab_temp2[i][j]=1;
    StringGrid2->Cells[i][j]=IntToStr(tab_temp2[i][j]);
   }
  }
  for(i=0;i<size;i++)
  {
   for(j=0;j<size;j++)
   {
    tab_temp3[i][j]=main_tab[i][j];
    if(i-1>0)
     tab_temp3[i][j]+=main_tab[i-1][j];
    if((i-1)>0 && (j-1)>0)
     tab_temp3[i][j]+=main_tab[i-1][j-1];
    if(j-1>0)
     tab_temp3[i][j]+=main_tab[i][j-1];
    if((i+1)<size && (j-1)>0)
     tab_temp3[i][j]+=main_tab[i+1][j-1];
    if(i+1<size)
     tab_temp3[i][j]+=main_tab[i+1][j];
    if((i+1)<size && (j+1)<size)
     tab_temp3[i][j]+=main_tab[i+1][j+1];
    if(j+1<size)
     tab_temp3[i][j]+=main_tab[i][j+1];
    if((i-1)>0 && (j+1)<size)
     tab_temp3[i][j]+=main_tab[i-1][j+1];
    StringGrid3->Cells[i][j]=IntToStr(tab_temp3[i][j]);
    StringGrid4->Cells[i][j]=char_for_Value(main_tab[i][j]);
   }
  }


}
And program running: