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

czwartek, 20 grudnia 2018

Use "/proc/cpuinfo" C++ (g++)

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

class CPU
{
    private:
    FILE *fp1,*fp2,*fp3;
    char buff1[2048],buff2[2048],buff3[2048];
    char *conv1,*conv2,*conv3,*name;
    float speed_cpu,memory_cache;
    size_t file1_read,file2_read,file3_read;
    public:
   
    void open_Files();
    void name_Info();
    void speed_Info();
    void cache_size_Info();
};
void CPU::open_Files()
{

    fp1=fopen("/proc/cpuinfo","r");
    file1_read=fread(buff1,1,sizeof(buff1),fp1);
    fclose(fp1);
    fp2=fopen("/proc/cpuinfo","r");
    file2_read=fread(buff2,1,sizeof(buff2),fp2);
    fclose(fp2);
    fp3=fopen("/proc/cpuinfo","r");
    file3_read=fread(buff3,1,sizeof(buff3),fp3);
    fclose(fp3);
   
    buff1[file1_read]='\0';
    buff2[file2_read]='\0';
    buff3[file3_read]='\0';
   
   
}
void CPU::name_Info()
{
    conv1=strstr(buff1,"model name");
    sscanf(conv1,"model name : %s",name);
    cout<<"Model name: "<<name<<endl;
}
void CPU::speed_Info()
{
    conv2=strstr(buff2,"cpu MHz");
    sscanf(conv2,"cpu MHz : %f",&speed_cpu);
    cout<<"cpu MHz: "<<speed_cpu<<endl;
}
void CPU::cache_size_Info()
{
    conv3=strstr(buff3,"cache size");
    sscanf(conv3,"cache size: %f",&memory_cache);
    cout<<"Cache size (KB): "<<memory_cache<<endl;
}


int main(int argc, char **argv)
{
    CPU *info_CPU=new class CPU;
    info_CPU->open_Files();
    info_CPU->name_Info();
    info_CPU->speed_Info();
    info_CPU->cache_size_Info();
    delete info_CPU;
    return 0;
}

niedziela, 16 grudnia 2018

Greedy algorithm. Creating nad selected sum and sorting from prime numbers.


private: // User declarations
       void __fastcall i_Sort(int *tab, int size);
       void __fastcall m_Sort(int *tab, int size);
       bool __fastcall is_First(int value);
       void __fastcall is_Sum(int *tab, int size,int value);
       int *tab_First,**data_First1,**data_First2,*test_Values;
       void __fastcall init_Arrays();
       void __fastcall destroy_Arrays();

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 srand(time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::i_Sort(int *tab, int size)
{
 int i,j,k;
 for(i=1;i<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::m_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;
   }
}
bool __fastcall TForm1::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 __fastcall TForm1::is_Sum(int *tab, int size,int value)
{
 int i,j,sum;

 do
 {
  sum=0;
  for(i=0;i<size;i++)
  {
   j=rand()%500;
   sum+=tab_First[j];
   tab[i]=tab_First[j];
  }
 }while(sum!=value);

}

void __fastcall TForm1::init_Arrays()
{
 int i,j;
 tab_First=new int[100];
 test_Values=new int[10];
 data_First1=new int *[10];
 data_First2=new int *[10];
 for(i=0;i<10;i++)
 {
  data_First1[i]=new int[10];
  data_First2[i]=new int[10];
 }
 i=0;j=2;
 do
 {
   if(is_First(j))
   {
    tab_First[i]=j;
    ++i;
   }
   ++j;
 }while(i<100);


}
void __fastcall TForm1::destroy_Arrays()
{
 int i;
 delete tab_First;
 delete test_Values;
 for(i=0;i<10;i++)
 {
  delete [] data_First2[i];
  delete [] data_First1[i];
 }
 delete data_First1;
 delete data_First2;

}

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

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int i,j,min,max;
 min=800;
 max=2000;
 for(i=0;i<10;i++)
 {
  test_Values[i]=min+rand()%(max-min);
  StringGrid3->Cells[0][i]=IntToStr(test_Values[i]);
 }
 for(i=0;i<10;i++)
 {
  is_Sum(data_First1[i],10,test_Values[i]);
  is_Sum(data_First2[i],10,test_Values[i]);
 }
 for(i=0;i<10;i++)
 {
  i_Sort(data_First1[i],10);
  m_Sort(data_First2[i],10);
 }
 for(i=0;i<10;i++)
 {
  for(j=0;j<10;j++)
  {
   StringGrid1->Cells[i][j]=IntToStr(data_First1[j][i]);
   StringGrid2->Cells[i][j]=IntToStr(data_First2[j][i]);
  }
 }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 destroy_Arrays();
 Close();
}