środa, 3 kwietnia 2019

The use of fork and exec together (gcc)

vim test_First.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int 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 1;
 else
  return 0;
}
int main(int argc, char *argv[])
{
 int i,j,k,result,sqrt_result;
 int how_many=atoi(argv[1]);
 i=0;
 j=1;
 do
 {
  if(is_First(j))
  {
   ++i;
   k=j;
  }
  ++j;
 }while(i<how_many);
 result=k;
 sqrt_result=sqrt(1.0*result);
 printf("%d,  (sqrt) = %f",result,sqrt(result));
 printf("\n");

 return 0;

}

gcc -o test_First test_First.c -lm

vim text_Opposite.c

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
 int i,j,size_Text;
 char *result;
 char *test;
 size_Text=strlen(argv[1]);
 test=malloc(sizeof(char)*size_Text);
 strcpy(test,argv[1]);
 test[size_Text+1]='\0';
 result=malloc(sizeof(char)*size_Text);

 result[size_Text]='\0';
 for(i=0;i<size_Text+1;i++)
  result[i]=test[i];
 for(i=0;i<size_Text+1;i++)
  result[i]=test[size_Text-(i+1)];
 printf("%s ",test);
 printf("\n"); 
 printf("%s ",result);
 printf("\n");
 free(test);
 free(result);
 printf("\n");


}

gcc -o text_Opposite text_Opposite.c

vim function.h

#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>


int test_FE(char *text, char **arg)
{
 pid_t child_P;
 child_P=fork();
 if(child_P!=0)
  return child_P;
 else
  {
   execvp(text,arg);
   fprintf(stderr,"execvp error\n");
   abort();
  }

}

vim main_prog.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include "function.h"


int main()
{

 char *ARG1[]={
 "./test_First","10","/",NULL};
 char *ARG2[]={
 "./text_Opposite","Example","/",NULL};
 test_FE("./test_First",ARG1);
 test_FE("./text_Opposite",ARG2);
 return 0;

}

gcc -o main_prog main_prog.c
./main_prog

środa, 13 marca 2019

Wriiting class with an argument - writev (Linux/g++)

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



class WRITE_ARGS
{
private:
char *file_args;
char *if_Exists;
char *if_Readable;
char *which_Type;
char *now_Date;
time_t temp_Time;
public:
WRITE_ARGS(char *file_test, char *save_Info)
{
int temp_rval;


temp_Time=time(NULL);
now_Date=asctime(localtime(&temp_Time));
file_args=save_Info;
temp_rval=access(file_test,R_OK);
if(temp_rval==0)
if_Readable="writable";
else if(errno==EACCES)
if_Readable="not access";
else if(errno==EROFS)
if_Readable="read only";
temp_rval=access(file_test,F_OK);
if(temp_rval==0)
{
if_Exists="file exists";
struct stat stat_Value;
lstat(file_test,&stat_Value);
if(S_ISDIR(stat_Value.st_mode))
 which_Type="directory";
else if(S_ISSOCK(stat_Value.st_mode))
 which_Type="socket";
else if(S_ISFIFO(stat_Value.st_mode))
 which_Type="queue FIFO";
else if(S_ISCHR(stat_Value.st_mode))
 which_Type="character device";
else if(S_ISBLK(stat_Value.st_mode))
 which_Type="block device";
else if(S_ISREG(stat_Value.st_mode))
 which_Type="normal file";
else
 which_Type="unknown type";

}
else
{
if(errno==EACCES)
if_Exists="is not access";
if(errno==ENOENT)
{
if_Exists="not exists";
which_Type="not exists";
  } 
}


}  
void save_Values()
{
int i,write_file;
char *info_Date[4];
struct iovec* io_value;
struct iovec* io_next;
char end='\n';
info_Date[0]=now_Date;
info_Date[1]=if_Exists;
info_Date[2]=if_Readable;
info_Date[3]=which_Type;
io_value=(struct iovec*)malloc(8*sizeof(struct iovec));
io_next=io_value;
for(i=0;i<4;i++)
{
io_next->iov_base=info_Date[i];
io_next->iov_len=strlen(info_Date[i]);
++io_next;
io_next->iov_base=&end;
io_next->iov_len=1;
++io_next;
}
write_file=open(file_args,O_WRONLY | O_CREAT);
writev(write_file,io_value,8);
close(write_file);
free(io_value);

}


};

int main(int argc, char **argv)
{
WRITE_ARGS wr_ARGS("/proc/version","my_report");
WRITE_ARGS wr_ARGS2("mm_cpu.c","report_cpu");
wr_ARGS.save_Values();
wr_ARGS2.save_Values();
return 0;
}

środa, 6 lutego 2019

class Lottery (C++)

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

using namespace std;


class Lottery
{
    private:
    void Sort(int *tab, int size);
    int **bet_Table;
    int **temp_Table;
    int how_m_games;
    int how_m_result;
    int max_value;
    int min_value;
    float pay;
    public:
    Lottery(int min, int max, int how_many_result, int how_many_games,float how_much_bet);
    void bet_Games();
    void print_Report();
    ~Lottery();
};
void Lottery::Sort(int *tab, int size)
{
    int i,j,k;
    for(i=1;i<size;i++)
     for(j=size-1;j>=i;j--)
      if(tab[j]<tab[j-1])
      {
          k=tab[j-1];
          tab[j-1]=tab[j];
          tab[j]=k;
      }
}
Lottery::Lottery(int min,int max, int how_many_result, int how_many_games, float how_much_bet)
{
    int i;
    srand(time(NULL));
    pay=(1.0*how_many_games)*how_much_bet;
    how_m_games=how_many_games;
    how_m_result=how_many_result;
    min_value=min;
    max_value=max;
    bet_Table=new int*[how_m_games];
    temp_Table=new int*[how_m_games];
    for(i=0;i<how_m_games;i++)
    {
     bet_Table[i]=new int[how_m_result];
     temp_Table[i]=new int[max_value];
    }
   
}
void Lottery::bet_Games()
{
    int i,j,k;
    for(i=0;i<how_m_games;i++)
     for(j=0;j<max_value;j++)
      temp_Table[i][j]=0;
    i=0;
    do
    {
     j=0;
     do
     {   
      do
      {   
       k=rand()%max_value;
      }while(temp_Table[i][k]!=0);
     temp_Table[i][k]=1;
     bet_Table[i][j]=k+1;
     ++j;
     }while(j<how_m_result);   
     ++i;   
    }while(i<how_m_games);
   
    for(i=0;i<how_m_games;i++)
    {
        Sort(bet_Table[i],how_m_result);
    }
}
void Lottery::print_Report()
{
    int i,j;
    for(i=0;i<how_m_games;i++)
    {
        for(j=0;j<how_m_result;j++)
        {
            cout<<bet_Table[i][j]<<"  ";
        }
        cout<<endl;
    }
    cout<<endl;
    cout<<"------------------------------------------------\n";
    cout<<"To pay: "<<pay<<endl;
}
Lottery::~Lottery()
{
    int i;
    for(i=0;i<how_m_games;i++)
    {
     delete temp_Table[i];       
     delete bet_Table[i];
    }
    delete [] bet_Table;
    delete [] temp_Table;
}

int main(int argc, char **argv)
{
    Lottery my_Game(1,49,6,10,4.0); //polish "Duzy Lotek Plus"
    my_Game.bet_Games();
    my_Game.print_Report();
    Lottery next_Game(1,42,5,3,1.25); //polis "Mini Lotto"
    next_Game.bet_Games();
    next_Game.print_Report();
    return 0;
}

środa, 30 stycznia 2019

The class has sent information about printers to the email (class C++ Builder)

#include <Printers.hpp>
#include <mapi.h>
#include <string.h>

class email_Printers
{
private:
DEVMODE info_Mode;
        HANDLE m_Handle;
        LHANDLE l_Handle;
        LPMAPILOGON m_LPMAPILOGON;
        LPMAPILOGOFF m_LPMAPILOGOFF;
        LHANDLE m_handle_Session;
        LPMAPISENDMAIL m_LPMAPISENDMAIL;
AnsiString names_Printers;

char* Title;
        char *email;
char Note[1024];
public:
email_Printers(char* email_Adress)
{
                strcpy(email,email_Adress);
names_Printers=String(*(info_Mode.dmDeviceName));
Title="Information about Printers";
m_Handle = LoadLibrary("MAPI32.DLL");
                m_LPMAPILOGON = (LPMAPILOGON)GetProcAddress(m_Handle, "MAPILogon");
m_LPMAPISENDMAIL = (LPMAPISENDMAIL)GetProcAddress(m_Handle, "MAPISendMail");
m_LPMAPILOGOFF = (LPMAPILOGOFF)GetProcAddress(m_Handle, "MAPILogoff");

(*m_LPMAPILOGON)(0, NULL, NULL, 0, 0, &l_Handle);
MapiRecipDesc m_Send = {0, MAPI_TO, NULL, email_Adress, 0, NULL};
                AnsiString temp_String=Printer()->Printers->Text;
                strcpy(Note,temp_String.c_str());
                MapiRecipDesc m_Desc = {0, MAPI_TO, NULL, email_Adress, 0, NULL};
                MapiMessage Message = {0, Title,Note, NULL, NULL, NULL, 0, NULL, 1, &m_Desc, 0, NULL};



}
        ~email_Printers()
        {
          FreeLibrary(m_Handle);
        }
        void Send()
        {

         MapiRecipDesc m_Desc = {0, MAPI_TO, NULL, email, 0, NULL};
         MapiMessage Message = {0, Title,Note, NULL, NULL, NULL, 0, NULL, 1, &m_Desc, 0, NULL};

         (*m_LPMAPILOGON)(0,NULL,NULL,0,0,&m_handle_Session);
         (m_LPMAPISENDMAIL)(m_handle_Session,0,&Message,0,0);
         (*m_LPMAPILOGOFF)(m_handle_Session,0,0,0);

        }


};

środa, 16 stycznia 2019

Encrypting text using the Josephus list (class C++)

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

class ciphere_Text
{
    private:
     struct base_Josephus
     {
         int x_J;
         base_Josephus *next_Base;
         base_Josephus(int temp_Jos,base_Josephus *t_J)
         {
             x_J=temp_Jos;
             next_Base=t_J;
         }
     };
   
     int result_Josephus(char x,int y)
     {
         int i,j,k;
         int p1,p2;
         p1=int_Value(x);p2=y;
         typedef base_Josephus *main_Jos;
         main_Jos temp_Main1=new base_Josephus(1,0);
         temp_Main1->next_Base=temp_Main1;
         main_Jos temp_Main2=temp_Main1;
         for(i=2;i<=p1;i++)
          temp_Main2=(temp_Main2->next_Base=new base_Josephus(i,temp_Main2));
         while(temp_Main2!=temp_Main2->next_Base)
         {
             for(j=1;j<p2;j++)
              temp_Main2=temp_Main2->next_Base;
             temp_Main2->next_Base=temp_Main2->next_Base->next_Base;
         }
         k=temp_Main2->x_J;
         return k;
       
     }
     char *c_Text;
     int size_Text;
     int int_Value(char x)
     {
         int i,j;
         char a;
         char m_Text[26],p_Text[26];
         j=0;
         for(i=0,a='a';a<'z';a++,i++)
          m_Text[i]=a;
         for(i=0,a='A';a<'Z';a++,i++)
          p_Text[i]=a;
         for(i=0;i<26;i++)
         {
             if(m_Text[i] || p_Text[i]==x)
              j=i+1;
         }
         return j;
         
     }
     bool M_or_P()
     {
         int a;
         a=rand()%10+1;
         if(a%2==0)
          return true;
         else
          return false;
     }
     char char_Value(int x)
     {
         int i;
         bool k;
         char a,j;
         char m_Text[26],p_Text[26];
         j=0;
         for(i=0,a='a';a<'z';a++,i++)
          m_Text[i]=a;
         for(i=0,a='A';a<'Z';a++,i++)
          p_Text[i]=a;
         k=M_or_P();
         if(k)
         {
             j=m_Text[x];
         }
         else
         {
             j=p_Text[x];
         }
         return j;
     }
     public:
      ciphere_Text(char *date)
      {
          int i,random_Value;
          size_Text=strlen(date);
          int *temp_Array=new int[size_Text];
          c_Text=new char[size_Text];
          random_Value=1+rand()%25;
         
          for(i=0;i<size_Text;i++)
          {
            temp_Array[i]=result_Josephus(date[i],random_Value); 
          }
          for(i=0;i<size_Text;i++)
          {
              c_Text[i]=char_Value(temp_Array[i]);
          }
      }
      char *give_Result()
      {
          return c_Text;
      }
      ~ciphere_Text()
      {
          delete c_Text;
      }
};

niedziela, 6 stycznia 2019

Development of the Floyd algorithm (class C++)

class Floyd_Alg
{
    private:
    int m_size;
    int m_min;
    int m_max;
    int m_use;
    int **floyd_Array,**temp_Array;
    int min_T(int a1, int a2);
    public:
    Floyd_Alg(int size, int min, int max, int use);
    void init_Array();
    void init_Floyd();
    void Report();
    ~Floyd_Alg();
   
};
int Floyd_Alg::min_T(int a1, int a2)
{
    int result;
    if(a1<=a2)
     result=a1;
    else
     result=a2;
    return result;
}
Floyd_Alg::Floyd_Alg(int size, int min, int max, int use)
{
    int i;
    m_size=size;
    m_min=min;
    m_max=max;
    m_use=use;
    floyd_Array=new int*[m_size];
    temp_Array=new int*[m_size];
    for(i=0;i<m_size;i++)
    {
     floyd_Array[i]=new int[m_size];
     temp_Array[i]=new int[m_size];
    }
}
void Floyd_Alg::init_Array()
{
    int i,j,k1,k2;
    for(i=0;i<m_size;i++)
    {
     for(j=0;j<m_size;j++)
     {
      temp_Array[i][j]=-1;
      floyd_Array[i][j]=-1;
     }
    }
    i=0;
    do
    {
     do
     {
        k1=rand()%m_size;
        k2=rand()%m_size;
     }while(temp_Array[k1][k2]!=-1);
     temp_Array[k1][k2]=0;
     ++i;
    }while(i<m_use);
    for(i=0;i<m_size;i++)
    {
        for(j=0;j<m_size;j++)
        {
            if(temp_Array[i][j]==0)
             floyd_Array[i][j]=m_min+rand()%(m_max-m_min);
        }
    }
}
void Floyd_Alg::init_Floyd()
{
    int i,j,k;
    for(i=0;i<m_size;i++)
     for(j=0;j<m_size;j++)
      for(k=0;k<m_size;k++)
       floyd_Array[j][k]=min_T(floyd_Array[j][k],floyd_Array[j][k]+floyd_Array[i][k]);
}
void Floyd_Alg::Report()
{
    int i,j;
    for(i=0;i<m_size;i++)
    {
        for(j=0;j<m_size;j++)
        {
            if(floyd_Array[i][j]==-1)
             cout<<i<<"*---->>"<<j<<"[not possible]\n";
            else if(i!=j)
             cout<<i<<"*---->>"<<j<<"="<<floyd_Array[i][j]<<endl;
        }
    }
}
Floyd_Alg::~Floyd_Alg()
{
    int i;
    for(i=0;i<m_size;i++)
    {
     delete[] floyd_Array[i];
     delete[] temp_Array[i];
    }
    delete [] floyd_Array;
    delete [] temp_Array;
}

środa, 2 stycznia 2019

System statistic (class C++ (g++))

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <linux/kernel.h>
using namespace std;

class system_Statistic
{
    private:
    struct utsname uname_Info;
    struct sysinfo sys_Info;
    struct rusage cpu_Times;
   
    char *sysname;
    char *version;
    char *machine;
    float free_RAM;
    int process;
    float total_RAM;
    int time_Process1;
    int time_Process2;
    public:
    system_Statistic()
    {
        uname(&uname_Info);
        sysinfo(&sys_Info);
        getrusage(RUSAGE_SELF,&cpu_Times);
        sysname=uname_Info.sysname;
        version=uname_Info.version;
        machine=uname_Info.machine;
        process=sys_Info.procs;
        free_RAM=sys_Info.freeram/(1024.0*1024.0);
        total_RAM=sys_Info.totalram/(1024.0*1024.0);
        time_Process1=cpu_Times.ru_utime.tv_sec;
        time_Process2=cpu_Times.ru_utime.tv_usec;
    }
    void Report()
    {
        cout<<"Sysname: "<<sysname<<endl;
        cout<<"Version: "<<version<<endl;
        cout<<"Machine: "<<machine<<endl;
        cout<<"Number of processes: "<<process<<endl;
        cout<<"Total RAM: "<<total_RAM<<endl;
        cout<<"Free RAM: "<<free_RAM<<endl;
        cout<<"Time of the process1: "<<time_Process1<<endl;
        cout<<"Time of the process2: "<<time_Process2<<endl;
       
    }
   
};

int main(int argc, char **argv)
{
    system_Statistic Info;
    Info.Report();
    return 0;
}