sobota, 29 lutego 2020

Animation (C++ Builder)



__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 srand(time(NULL));
}
void __fastcall TForm1::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;
 }
}
TColor __fastcall TForm1::draw_Color(int value)
{
 TColor color;
 switch(value)
 {
  case 0: color=clWhite;break;
  case 1: color=clYellow;break;
  case 2: color=clBlue;break;
  case 3: color=clRed;break;
  case 4: color=clSilver;
  default: color=clBlack; break;

 }
 return color;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 TCanvas *tab_Canvas[5];
 tab_Canvas[0]=PaintBox1->Canvas;
 tab_Canvas[1]=PaintBox2->Canvas;
 tab_Canvas[2]=PaintBox3->Canvas;
 tab_Canvas[3]=PaintBox4->Canvas;
 tab_Canvas[4]=PaintBox5->Canvas;
 int i,j,k,l;
 int anime=500;
 int width=PaintBox1->Width;
 int height=PaintBox1->Height;
 int r_width=width/2;
 int r_height=height/2;
 int *r_Eclipse=new int[20];
 for(i=0;i<20;i++)
 {
  r_Eclipse[i]=rand()%(r_height-10)+10;
 }
 Sort(r_Eclipse,20);
 for(i=0;i<anime;i++)
 {
  j=rand()%5;
  tab_Canvas[0]->Brush->Style=bsSolid;
  tab_Canvas[0]->Brush->Color=draw_Color(j);
  tab_Canvas[0]->Rectangle(0,0,width,height);
  tab_Canvas[0]->Brush->Style=bsClear;
  for(k=0;k<20;k++)
  {
   r_Eclipse[k]+=2;
   tab_Canvas[0]->Ellipse(r_width-r_Eclipse[k],r_height-r_Eclipse[k],r_width+r_Eclipse[k],r_height+r_Eclipse[k]);
  }
  for(l=1;l<5;l++)
  {
   tab_Canvas[l]->CopyRect(Rect(0,0,width,height),tab_Canvas[0],Rect(0,0,width,height));
  }
 }

 delete r_Eclipse;

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

poniedziałek, 17 lutego 2020

Shared memory (C++ Linux)

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/shm.h>

#define m_size 10
#define m_mem 10000
using namespace std;



 const string capitals[m_size]={"Warsaw","Berlin","Moscow","Paris","Athens","Kiev",
"Prague","Roma","Lisbon","Amsterdam"};
 const int segments[m_size]={0x1500,0x2500,0x3500,0x4500,0x5500,0x6500,0x7500,
0x8500,0x9500,0x4900};
 const int new_segments[m_size]={0x100000,0x200000,0x300000,0x4000000,0x5000000,
 0x6000000,0x7000000,0x8000000,0x9000000,0x783838};
 int value_Size[10];
 int value_Segm[10];
int main(int argc, char **argv)
{
int i;
char **mem=new char *[m_size];
for(i=0;i<m_size;i++)
mem[i]=new char[m_mem];

struct shmid_ds* mem_Contr=new shmid_ds[m_size];
for(i=0;i<m_size;i++)
{
value_Segm[i]=shmget(IPC_PRIVATE,segments[i],IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
mem[i]=(char*)shmat(value_Segm[i],0,0);
shmctl(value_Segm[i],IPC_STAT,&mem_Contr[i]);
value_Size[i]=mem_Contr[i].shm_segsz;
cout<<"Size memory: "<<value_Size[i]<<endl;
sprintf(mem[i],capitals[i].c_str());
shmdt(mem[i]);
mem[i]=(char*)shmat(value_Segm[i],(void*)new_segments[i],0);
printf("New memory: %p    %s\n",mem[i],mem[i]);
shmdt(mem[i]);
shmctl(value_Segm[i],IPC_RMID,0);
}
for(i=0;i<m_size;i++)
delete [] mem[i];
delete mem;
delete mem_Contr;




return 0;
}

czwartek, 6 lutego 2020

Copying files (C++ Linux)

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

#define size_F 10

using namespace std;

string proc_Date[size_F]={"/proc/dma","/proc/misc","/proc/meminfo","/proc/kcore","/proc/keys","/proc/iomen",
"/proc/slabinfo","/proc/version","/proc/zoneinfo","/proc/cpuinfo"};
string dest_Date[size_F]={"dma1","misc1","meminfo1","kcore1","keys1","iomen1","slabinfo1","version1","zoneinfo1",
"cpuinfo1"};

off_t *o_F;
int *r,*w;
struct stat* run_Stat;

int main(int argc, char **argv)
{
o_F=new off_t[size_F];
run_Stat=new struct stat[size_F];
r=new int[size_F];
w=new int[size_F];
for(int i=0;i<size_F;i++)
{
r[i]=open(proc_Date[i].c_str(),O_RDONLY);
fstat(r[i],&run_Stat[i]);
w[i]=open(dest_Date[i].c_str(),O_WRONLY | O_CREAT,run_Stat[i].st_size);
sendfile(w[i],r[i],&o_F[i],run_Stat[i].st_size);
close(r[i]);
close(w[i]);
}
delete w;
delete r;
delete run_Stat;
delete o_F;

return 0;
}

sobota, 1 lutego 2020

Counting the given text in the file (C++ Builder)


char* __fastcall TForm1::read_Ch(AnsiString name_File, int f_Size)
{
char *znaki=new char[f_Size];

FILE* fp;
fp=fopen(name_File.c_str(),"r");
for(int i=0;i<f_Size;i++)
fscanf(fp,"%c",&znaki[i]);
fclose(fp);
return znaki;

}
void __fastcall TForm1::init_Memo(AnsiString name_File, int f_Size)
{
Memo1->Lines->Append(AnsiString(read_Ch(name_File,f_Size)));
}
int __fastcall TForm1::sum_Txt(char *phrase)
{
int sum=0,i,count=Memo1->Lines->Count;
AnsiString *buff=new AnsiString[count];
for(i=0;i<count;i++)
{
buff[i]=Memo1->Lines->Text[i];
if(strcmp(buff[i].c_str(),phrase))
++sum;
}
delete buff;
return sum;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 AnsiString path;
 path="C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\18.0\\GetItInstall.log";
 init_Memo(path,2048);
  AnsiString temp=Edit1->Text;
 int how_Many=sum_Txt(temp.c_str());
 Label1->Caption=IntToStr(how_Many);
 }
//---------------------------------------------------------------------------

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