niedziela, 15 lipca 2018

Point density. StringGrid. C++ Builder



__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 srand(time(NULL));
}
//---------------------------------------------------------------------------


void __fastcall TForm1::init_first_Table()
{
 int i,j,x;
 for(i=0;i<30;i++)
 {
   for(j=0;j<30;j++)
   {
     x=rand()%100+1;
     if(x%2==0)
     {
      tab1[i][j]=1;
      StringGrid1->Cells[i][j]="1";
     }
     else
     {
      tab1[i][j]=0;
      StringGrid1->Cells[i][j]="O";

     }
   }
 }
}
void __fastcall TForm1::init_second_Table()
{
 int i,j;
 for(i=0;i<30;i++)
  for(j=0;j<30;j++)
   tab2[i][j]=0;

  for(i=0;i<30;i++)
  {
   for(j=0;j<30;j++)
   {
    if((i-1>-1) && tab1[i-1][j]==1)

     tab2[i][j]++;

    if((i-1>-1) && (j-1>=0))
     if(tab1[i-1][j-1]==1)
      tab2[i][j]++;
    if((j-1>-1) && (tab1[i][j-1]==1))
     tab2[i][j]++;
    if((i+1<=30) && (j-1>-1))
     if(tab1[i+1][j-1]==1)
      tab2[i][j]++;
    if((i+1<=30) && (tab1[i+1][j]==1))
      tab2[i][j]++;
    if((i+1<30) && (j+1<30))
     if(tab1[i+1][j+1]==1)
      tab2[i][j]++;
    if((j+1<30) && (tab1[i][j+1]==1))
     tab2[i][j]++;
    if((i-1>-1) && (j+1<30))
     if(tab1[i-1][j+1]==1)
      tab2[i][j]++;
    if(tab1[i][j]==1)
     tab2[i][j]++;

   }
  }

  for(i=0;i<30;i++)
   for(j=0;j<30;j++)
    StringGrid2->Cells[i][j]=IntToStr(tab2[i][j]);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 init_first_Table();
 init_second_Table();
}
//---------------------------------------------------------------------------

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


niedziela, 17 czerwca 2018

Sorting different types and connecting to records (C++ Builder)



void Change(char*text1,char*text2)
{
char temp[40]; 
strcpy(temp, text1);
strcpy(text1,text2);
strcpy(text2,temp);
}
void sort_Char(char **text, int size)
{
 int i,j;
 for(int i=0;i<size;i++)
 {
  for (int j=i;j<size;j++)
  {
   if(strcmp(text[i],text[j])>0)
   Change(text[i],text[j]);
  }
 }
}


void double_Sort(double *table, int size)
{
 int i,j;
 double temp;
 for(i=1;i<size;i++)
  for(j=size-1;j>=i;j--)
   if(table[j]<table[j-1])
   {
    temp=table[j-1];
    table[j-1]=table[j];
    table[j]=temp;
   }

}

void int_Sort(int *table, int size)
{
 int i,j;
 int temp;
 for(i=1;i<size;i++)
  for(j=size-1;j>=i;j--)
   if(table[j]<table[j-1])
   {
    temp=table[j-1];
    table[j-1]=table[j];
    table[j]=temp;
   }

}
struct Planet
{
char name[20];
int radius;
double mass_Earth;
};
struct Planet *m_Planet;

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  int i,j;
  m_Planet=new struct Planet[8];
 strcpy(m_Planet[0].name,"Mercury");
 m_Planet[0].radius=2440;
 m_Planet[0].mass_Earth=0.055;
 strcpy(m_Planet[1].name,"Venus");
 m_Planet[1].radius=6052;
 m_Planet[1].mass_Earth=0.815;
 strcpy(m_Planet[2].name,"Earth");
 m_Planet[2].radius=6371;
 m_Planet[2].mass_Earth=1.00;
 strcpy(m_Planet[3].name,"Mars");
 m_Planet[3].radius=3390;
 m_Planet[3].mass_Earth=0.107;
 strcpy(m_Planet[4].name,"Jupiter");
 m_Planet[4].radius=69911;
 m_Planet[4].mass_Earth=317.8;
 strcpy(m_Planet[5].name,"Saturn");
 m_Planet[5].radius=58232;
 m_Planet[5].mass_Earth=95.16;
 strcpy(m_Planet[6].name,"Uranium");
 m_Planet[6].radius=25362;
 m_Planet[6].mass_Earth=14.54;
 strcpy(m_Planet[7].name,"Neptune");
 m_Planet[7].radius=24622;
 m_Planet[7].mass_Earth=17.15;
 for(i=0;i<8;i++)
 {
  ListBox1->Items->Add(m_Planet[i].name);
  ListBox2->Items->Add(IntToStr(m_Planet[i].radius));
  ListBox3->Items->Add(FloatToStr(m_Planet[i].mass_Earth));
 }
 int temp_Radius[8];
 double temp_Mass[8];
 char **temp_Name;
 temp_Name=new char*[40];
 for(i=0;i<40;i++)
  temp_Name[i]=new char[8];
 for(i=0;i<8;i++)
 {
  temp_Radius[i]=m_Planet[i].radius;
  temp_Mass[i]=m_Planet[i].mass_Earth;
  strcpy(temp_Name[i],m_Planet[i].name);
 }
 double_Sort(temp_Mass,8);
 int_Sort(temp_Radius,8);
 sort_Char(temp_Name,8);

 for(i=0;i<8;i++)
 {
  ListBox4->Items->Add(temp_Name[i]);
  for(j=0;j<8;j++)
  {
   if(temp_Radius[i]==m_Planet[j].radius)
   {
    ListBox5->Items->Add(IntToStr(temp_Radius[i])+"  -  "+m_Planet[j].name);
    break;
   }

  }
  for(j=0;j<8;j++)
  {
   if(temp_Mass[i]==m_Planet[j].mass_Earth)
   {
    ListBox6->Items->Add(FloatToStr(temp_Mass[i])+"  -  "+m_Planet[j].name);
    break;
   }
  }
 }



}

czwartek, 7 czerwca 2018

Computer name, user and IP. C++ Builder






#include <vcl.h>
#include <winsock.h>
#define SIZE_NAME 48
#define SIZE_USER 36
class INFO
{
public:
UnicodeString computer_Name();
UnicodeString user_Name();
UnicodeString my_IP();


};

UnicodeString INFO::computer_Name()
{

  DWORD value_Size=SIZE_NAME;
  UnicodeString value_Name;
  wchar_t *tab_Name;
  tab_Name=new wchar_t[SIZE_NAME];
  tab_Name[0]='\0';
  GetComputerName(tab_Name,&value_Size);
  value_Name=UnicodeString(tab_Name);
  delete tab_Name;
  return value_Name;

}
UnicodeString INFO::user_Name()
{
DWORD value_Size=SIZE_USER;
UnicodeString value_Name;
wchar_t *tab_Name;
tab_Name=new wchar_t[SIZE_USER];
tab_Name[0]='\0';
GetUserName(tab_Name,&value_Size);
value_Name=UnicodeString(tab_Name);
return value_Name;

}

UnicodeString INFO::my_IP()
{
hostent* temp_Info;
bool begin=true;
in_addr *temp_Address;
UnicodeString comp_Name;
AnsiString comp_String;
AnsiString value_Ansi;
UnicodeString value_Unicode;
comp_Name=computer_Name();
comp_String=comp_Name;
if(begin)
{
 WORD temp_Version=MAKEWORD(1,0);
 WSADATA temp_Data;
 WSAStartup(temp_Version,&temp_Data);
}
temp_Info=gethostbyname(comp_String.c_str());
comp_String=(AnsiString)(temp_Info->h_name);
temp_Address=(in_addr*)(*(temp_Info->h_addr_list));
value_Ansi=(AnsiString)inet_ntoa(*temp_Address);
value_Unicode=value_Ansi;
WSACleanup();
    return value_Unicode;


}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 INFO iF;
 Edit1->Text=iF.user_Name();
 Edit2->Text=iF.computer_Name();
 Edit3->Text=iF.my_IP();
}

sobota, 2 czerwca 2018

Calendar 2018 C++ Builder




private: // User declarations
int months[12];
UnicodeString days[7];
UnicodeString months_name[12];
bool __fastcall is_Monday(int value);
bool __fastcall is_Tuesday(int value);
bool __fastcall is_Wednesday(int value);
bool __fastcall is_Thursday(int value);
bool __fastcall is_Friday(int value);
bool __fastcall is_Saturday(int value);
bool __fastcall is_Sunday(int value);
UnicodeString __fastcall write_Day(int value);
UnicodeString __fastcall write_Month(int value);
int __fastcall write_Nr(int value);


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
 months[0]=31;
 months[1]=28;
 months[2]=31;
 months[3]=30;
 months[4]=31;
 months[5]=30;
 months[6]=31;
 months[7]=31;
 months[8]=30;
 months[9]=31;
 months[10]=30;
 months[11]=31;
 days[0]="Monday";
 days[1]="Tuesday";
 days[2]="Wednesday";
 days[3]="Thursday";
 days[4]="Friday";
 days[5]="Saturday";
 days[6]="Sunday";
 months_name[0]="January";
 months_name[1]="Februar";
 months_name[2]="March";
 months_name[3]="April";
 months_name[4]="May";
 months_name[5]="June";
 months_name[6]="July";
 months_name[7]="August";
 months_name[8]="September";
 months_name[9]="October";
 months_name[10]="November";
 months_name[11]="December";
}
//---------------------------------------------------------------------------

bool __fastcall TForm1::is_Monday(int value)
{
int i;
bool temp;
temp=false;
for(i=1;i<365+1;i+=7)
if(i==value)
  temp=true;
return temp;
}
bool __fastcall TForm1::is_Tuesday(int value)
{
int i;
bool temp;
temp=false;
for(i=2;i<365+1;i+=7)
if(i==value)
  temp=true;
return temp;
}
bool __fastcall TForm1::is_Wednesday(int value)
{
int i;
bool temp;
temp=false;
for(i=3;i<365+1;i+=7)
if(i==value)
  temp=true;
return temp;
}

bool __fastcall TForm1::is_Thursday(int value)
{
int i;
bool temp;
temp=false;
for(i=4;i<365+1;i+=7)
if(i==value)
  temp=true;
return temp;
}

bool __fastcall TForm1::is_Friday(int value)
{

   int i;
bool temp;
temp=false;
for(i=5;i<365+1;i+=7)
if(i==value)
  temp=true;
return temp;

}
bool __fastcall TForm1::is_Saturday(int value)
{
   int i;
bool temp;
temp=false;
for(i=6;i<365+1;i+=7)
if(i==value)
  temp=true;
return temp;
}

bool __fastcall TForm1::is_Sunday(int value)
{
  int i;
bool temp;
temp=false;
for(i=7;i<365+1;i+=7)
if(i==value)
  temp=true;
return temp;
}


 UnicodeString __fastcall TForm1::write_Day(int value)
 {
UnicodeString value_d;
if(is_Monday(value))
  value_d=days[0];
else if(is_Tuesday(value))
  value_d=days[1];
else if(is_Wednesday(value))
  value_d=days[2];
else if(is_Thursday(value))
  value_d=days[3];
else if(is_Friday(value))
  value_d=days[4];
else if(is_Saturday(value))
  value_d=days[5];
else
  value_d=days[6];
return value_d;
 }
 UnicodeString __fastcall TForm1::write_Month(int value)
 {
UnicodeString value_m;
int temp_min[12],i,sum;
sum=0;
for(i=0;i<12;i++)
  temp_min[i]=0;
for(i=0;i<12;i++)
{
  sum+=months[i];
  temp_min[i]+=sum;
}
if(value<=temp_min[0])
  value_m=months_name[0];
if(value> temp_min[0] && value <=temp_min[1])
  value_m=months_name[1];
if(value>temp_min[1] && value<=temp_min[2])
  value_m=months_name[2];
if(value>temp_min[2] && value<=temp_min[3])
  value_m=months_name[3];
if(value>temp_min[3] && value<=temp_min[4])
  value_m=months_name[4];
if(value>temp_min[4] && value<=temp_min[5])
  value_m=months_name[5];
if(value>temp_min[5] && value<=temp_min[6])
  value_m=months_name[6];
if(value>temp_min[6] && value<=temp_min[7])
  value_m=months_name[7];
if(value>temp_min[7] && value<=temp_min[8])
  value_m=months_name[8];
if(value>temp_min[8] && value<=temp_min[9])
  value_m=months_name[9];
if(value>temp_min[9] && value<=temp_min[10])
  value_m=months_name[10];
if(value>temp_min[10] && value<=temp_min[11])
  value_m=months_name[11];
return value_m;

 }
 int __fastcall TForm1::write_Nr(int value)
 {
int  value_nr;
int temp_min[12],i,sum;
sum=0;
for(i=0;i<12;i++)
  temp_min[i]=0;
for(i=0;i<12;i++)
{
  sum+=months[i];
  temp_min[i]+=sum;
}
if(value<=temp_min[0])
  value_nr=value;
if(value> temp_min[0] && value <=temp_min[1])
  value_nr=value-temp_min[0];
if(value>temp_min[1] && value<=temp_min[2])
  value_nr=value-temp_min[1];
if(value>temp_min[2] && value<=temp_min[3])
  value_nr=value-temp_min[2];
if(value>temp_min[3] && value<=temp_min[4])
  value_nr=value-temp_min[3];
if(value>temp_min[4] && value<=temp_min[5])
  value_nr=value-temp_min[4];
if(value>temp_min[5] && value<=temp_min[6])
  value_nr=value-temp_min[5];
if(value>temp_min[6] && value<=temp_min[7])
  value_nr=value-temp_min[6];
if(value>temp_min[7] && value<=temp_min[8])
  value_nr=value-temp_min[7];
if(value>temp_min[8] && value<=temp_min[9])
  value_nr=value-temp_min[8];
if(value>temp_min[9] && value<=temp_min[10])
  value_nr=value-temp_min[9];
if(value>temp_min[10] && value<=temp_min[11])
  value_nr=value-temp_min[10];
return value_nr;


 }
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int value,nr_day;
 UnicodeString day,month;
 value=StrToInt(Edit1->Text);
 day=write_Day(value);
 month=write_Month(value);
 nr_day=write_Nr(value);
 Label1->Caption=day;
 Label2->Caption=month;
 Label4->Caption=IntToStr(nr_day);
}

wtorek, 29 maja 2018

Bit operations. C++ Builder




__fastcall TForm1(TComponent* Owner);
bool __fastcall is_First(int value);
int first[50];
void __fastcall init_First();
UnicodeString bits[50];
UnicodeString negations[50];
UnicodeString lefts[50];
int neg_value[50];
int left_value[50];
UnicodeString __fastcall write_Bits(char value);
void __fastcall init_Bits();
int __fastcall write_Ints(wchar_t *value);
void __fastcall init_Ints();




  __fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
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::init_First()
{
int i,j;
i=0;
j=1;
do
{
 if(is_First(j))
 {
 first[i]=j;
 ++i;
 }
 ++j;

}while(i<50);
}
UnicodeString __fastcall TForm1::write_Bits(char value)
{
int i,j;
UnicodeString y="1";
UnicodeString n="0";
UnicodeString b_value="";
int bits_value[9]={1,2,4,8,16,32,64,128,256};
for(i=8;i>=0;i--)
{
j=(bits_value[i]&value);


if(j!=0)
b_value+=y;
else
b_value+=n;
}
return b_value;
}
void __fastcall TForm1::init_Bits()
{
int i;
for(i=0;i<50;i++)
{
bits[i]=write_Bits(char(first[i]));
negations[i]=write_Bits(char(~first[i]));
        lefts[i]=write_Bits(char(first[i]<<1));
}
}

int __fastcall TForm1::write_Ints(wchar_t *value)
{
   int int_value,i;
wchar_t y='1';
wchar_t n='0';
int_value=0;
   int bits_value[9]={256,128,64,32,16,8,4,2,1};
   for(i=8;i>=0;i--)
   {
if(value[i]==y)
int_value+=bits_value[i];
   }
   return int_value;
}
void __fastcall TForm1::init_Ints()
{
int i;
for(i=0;i<50;i++)
{
neg_value[i]=write_Ints(negations[i].w_str());
        left_value[i]=write_Ints(lefts[i].w_str());
}
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 int i;
 init_First();
 init_Bits();
 init_Ints();
 for(i=0;i<50;i++)
 {
ListBox1->Items->Add(IntToStr(i+1)+")  "+IntToStr(first[i]));
ListBox2->Items->Add(bits[i]);
ListBox3->Items->Add(negations[i]+"     "+IntToStr(neg_value[i]));
ListBox4->Items->Add(lefts[i]+"     "+IntToStr(left_value[i]));
 }
}

środa, 23 maja 2018

Copy from StringGrid to ListBox. Use ostringstream. C++ Builder



void __fastcall TForm1::FormCreate(TObject *Sender)
{
 int i,j;
  for(i=0;i<10;i++)
   for(j=0;j<10;j++)
    StringGrid1->Cells[i][j]=IntToStr((i+1)*(j+1));
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{

 std::ostringstream test[10];
 int i,j;
 for(i=0;i<10;i++)
 {
  for(j=0;j<10;j++)
  {
   test[i]<<StrToInt(StringGrid1->Cells[i][j])<<" ";

  }

 }

 for(i=0;i<10;i++)
 ListBox1->Items->Add(test[i].str().c_str());

}