ś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;
}

czwartek, 27 grudnia 2018

Reading the parameter of the file (class C++)

#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

class read_Value
{
 private:
   float float_Result;
   char *text_Result;
   char *file_Name;
   char *dest_Name;
   bool test_Buffer(size_t s_read, char *buff);
   bool is_Null(char *buff);    
   float read_float_Value(char *file_Name, char *float_Value);
   char* read_text_Value(char *file_Name,char *text_Value);
 public:
   read_Value(char *file,char *ex_result); 
   char *write_Text();
   float write_Float();   
        


};
read_Value::read_Value(char *file,char *ex_result)
{
 file_Name=file;
 dest_Name=ex_result;
          
}
bool read_Value::test_Buffer(size_t s_read, char *buff)
{
  if(s_read==0 || s_read==sizeof(buff))
return true;
else
     return false;

}
bool read_Value::is_Null(char *buff)
{
 if(buff==NULL)
return true;
else
     return false;    
}
char* read_Value::read_text_Value(char *file_Name,char *text_Value)
{
  char *result;
char temp[2048],*match_buff,*sscan_buff;
strcpy(sscan_buff,text_Value);
strcat(sscan_buff," : %s");
size_t byt_read;
FILE *f_File;
f_File=fopen(file_Name,"r");
byt_read=fread(temp,1,sizeof(temp),f_File);
fclose(f_File);
if(test_Buffer(byt_read,temp))
return 0;
temp[byt_read]='\0';
match_buff=strstr(temp,text_Value);
if(is_Null(match_buff))
return 0;
sscanf(match_buff,sscan_buff,result);
return result;     

float read_Value::read_float_Value(char *file_Name, char *float_Value)
{
  float result;
char temp[2048],*match_buff,*sscan_buff;
strcpy(sscan_buff,float_Value);
strcat(sscan_buff," : %f");
size_t byt_read;
FILE *f_File;
f_File=fopen(file_Name,"r");
byt_read=fread(temp,1,sizeof(temp),f_File);
fclose(f_File);
if(test_Buffer(byt_read,temp))
return 0;
temp[byt_read]='\0';
match_buff=strstr(temp,float_Value);
if(is_Null(match_buff))
return 0;
sscanf(match_buff,sscan_buff,&result);
return result;     
}
char * read_Value::write_Text() 
{
  text_Result=read_text_Value(file_Name,dest_Name);
  return text_Result;    
}
float read_Value::write_Float()
{
  float_Result=read_float_Value(file_Name,dest_Name);
  return float_Result;     
}
int main(int argc, char *argv[])
{
    char *file_Char;
    float file_Float;
    read_Value r_Value("C:\Nowy folder\test_File.txt","test");
    file_Char=r_Value.write_Text();
    cout<<file_Char<<endl;
    read_Value f_Value("C:\Nowy folder\float_File.txt","test");
    file_Float=f_Value.write_Float();
    cout<<file_Float<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

sobota, 22 grudnia 2018

Use /proc/meminfo (gcc)

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



int main(int argc, char **argv)
{
    int i;
    char *file;
    char end;
    FILE *m_File;
    char buffer_F[5][1024];
    size_t read_Buff[5];
    unsigned int mem_Total,mem_Free,mem_Shared,swap_Total,swap_Free;
    char *info_mem_Total,*info_mem_Free,*info_mem_Shared,*info_swap_Total,
    *info_swap_Free;
    file="/proc/meminfo";
    end='\0';
    m_File=fopen(file,"r");
    for(i=0;i<5;i++)
    {
     read_Buff[i]=fread(buffer_F[i],1,sizeof(buffer_F[i]),m_File);
    }
    fclose(m_File);
    for(i=0;i<5;i++)
    {
     buffer_F[i][read_Buff[i]]=end;
    }
    info_mem_Total=strstr(buffer_F[0],"MemTotal");
    sscanf(info_mem_Total,"MemTotal : %u",&mem_Total);
    info_mem_Free=strstr(buffer_F[1],"MemFree");
    sscanf(info_mem_Free,"MemFree : %u",&mem_Free);
    info_mem_Shared=strstr(buffer_F[2],"MemShared");
    sscanf(info_mem_Shared,"MemShared : %u",&mem_Shared);
    info_swap_Total=strstr(buffer_F[3],"SwapTotal");
    sscanf(info_swap_Total,"SwapTotal: %u",&swap_Total);
    info_swap_Free=strstr(buffer_F[4],"SwapFree");
    sscanf(info_swap_Free,"SwapFree: %u",&swap_Free);
    printf("\nMemTotal: %u",mem_Total);   
    printf("\nMemFree : %u",mem_Free);
    printf("\nMemShared : %u",mem_Shared);
    printf("\nSwapTotal: %u",swap_Total);
    printf("\nswap_Free: %u",swap_Free);
   
   
    return 0;
}