niedziela, 29 stycznia 2017

Use of the queue in C++ Builder


Fille - date.h


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

struct cities
{
char *name;
int residents;
};

template <class Queue_cities> class CT
{
  private:
   Queue_cities *temporary;
   int begin,end,value;
  public:
   CT(int many)
   {
  value=many;
  temporary=new Queue_cities[value+1];
  end=0;
  begin=0;

   }
   void Insert(Queue_cities temp)
   {
  temporary[end++]=temp;
  if(end>value)
end=0;
   }
   int Work(Queue_cities &temp)
   {
  if(begin==end)
return -1;
  temp=temporary[begin++];
  if(begin>value)
begin=0;
  return 1;
   }
   bool is_Empty()
   {
  if(begin!=end)
return false;
  else
return true;
   }
};

C++ Builder:

#include <vcl.h>

#pragma hdrstop

#include "m_next02.h"
#include "date.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

private: // User declarations
  struct cities *usa_cities;

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 usa_cities=new struct cities[10];
 usa_cities[0].name="New York";
 usa_cities[0].residents=8300000;
 usa_cities[1].name="Houston";
 usa_cities[1].residents=2200000;
 usa_cities[2].name="Phoenix";
 usa_cities[2].residents=1500000;
 usa_cities[3].name="Dallas";
 usa_cities[3].residents=1200000;
 usa_cities[4].name="Detroit";
 usa_cities[4].residents=700000;
 usa_cities[5].name="Chicago";
 usa_cities[5].residents=2700000;
 usa_cities[6].name="San Jose";
 usa_cities[6].residents=900000;
 usa_cities[7].name="San Francisco";
 usa_cities[7].residents=800000;
 usa_cities[8].name="Philadelphia";
 usa_cities[8].residents=1500000;
 usa_cities[9].name="Charlotte";
 usa_cities[9].residents=700000;
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 int i;
 CT<char *> name_cities(10);
 CT<int> resident_cities(10);
 for(i=0;i<10;i++)
 {
name_cities.Insert(usa_cities[i].name);
resident_cities.Insert(usa_cities[i].residents);
 }
 for(i=0;i<11;i++)
 {
char *temporary_cities;
int temporary_residents;
int temp1,temp2;
temp1=name_cities.Work(temporary_cities);
temp2=resident_cities.Work(temporary_residents);
if(temp1==1 && temp2==1)
{
ListBox1->Items->Add("City  -   "+AnsiString(temporary_cities)+" has "+IntToStr(temporary_residents)+" residents");
}

 }
  if(name_cities.is_Empty() && resident_cities.is_Empty());
 ListBox1->Items->Add("Queue is empty");
}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
 if(SaveDialog1->Execute())
 {
ListBox1->Items->SaveToFile(SaveDialog1->FileName);
 }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 delete usa_cities;
 Close();
}



czwartek, 26 stycznia 2017

Connect with MS Office (C++ Builder (variable Variant))



private: // User declarations
Variant my_Word;
Variant my_Excel;

TForm1 *Form1;

PropertySet Connect_Word("Visible");
PropertySet Connect_Excel("Visible");
Procedure Disconnect_Word("Quit");
Procedure Disconnect_Excel("Quit");
//---------------------------------------------------------------------------




__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}



//---------------------------------------------------------------------------



void __fastcall TForm1::Button1Click(TObject *Sender)
{
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 try
 {
  my_Word=Variant::CreateObject("Word.Application");
  Connect_Word<<true;
  my_Word.Exec(Connect_Word);
 }
 catch(...)
 {
ShowMessage("Word is not installed");
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
  try
  {
 my_Excel=Variant::CreateObject("Excel.Application");
 Connect_Excel<<true;
 my_Excel.Exec(Connect_Excel);
  }
  catch(...)
  {
 ShowMessage("Excel is not installed");
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 try
 {
   Disconnect_Word<<false;
   my_Word.Exec(Disconnect_Word);
   my_Word=Unassigned;
 }
 catch(...)
 {
ShowMessage("Word is not installed");
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
 try
 {
Disconnect_Excel<<false;
my_Excel.Exec(Disconnect_Excel);
my_Excel=Unassigned;
 }
 catch(...)
 {
ShowMessage("Excell is not installed");
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Label1->Caption=DateToStr(Date());
 Label2->Caption = TimeToStr(Time());
}

niedziela, 22 stycznia 2017

Creating a number of cells StringGrids (C++ Builder)


private: // User declarations
void __fastcall init_Random();
int *first_v, *tenth_v, *hundreth_v, *thausend_v,*new_values;
void __fastcall create_Values();
void __fastcall write_Values();

void __fastcall TForm1::init_Random()
{
srand(time(NULL));
}
void __fastcall TForm1::create_Values()
{
int i,j;
first_v=new int[1000];
tenth_v=new int[1000];
hundreth_v=new int[1000];
thausend_v=new int[1000];
    new_values=new int[1000];
for(i=0;i<100;i++)
{
first_v[i]=rand()%10;
tenth_v[i]=rand()%10;
hundreth_v[i]=rand()%10;
thausend_v[i]=rand()%10;
new_values[i]=(1000*thausend_v[i])+(100*hundreth_v[i])+
(10*tenth_v[i])+first_v[i];

}
}
void __fastcall TForm1::write_Values()
{
int i,j,k;
k=0;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
StringGrid1->Cells[j][i]=IntToStr(thausend_v[k]);
StringGrid2->Cells[j][i]=IntToStr(hundreth_v[k]);
StringGrid3->Cells[j][i]=IntToStr(tenth_v[k]);
StringGrid4->Cells[j][i]=IntToStr(first_v[k]);
ListBox1->Items->Add(IntToStr(k+1)+" )  - "+IntToStr(new_values[k]));
k++;
}
}
}


void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Random();
 create_Values();
 write_Values();
}
//---------------------------------------------------------------------------



void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
 delete thausend_v;
 delete hundreth_v;
 delete tenth_v;
 delete first_v;
 delete new_values;
}

Affine transformation (C++ Builder)



private: // User declarations
  float x1[5],y1[5],x2[5],y2[5],x3[5],y3[5];
  void __fastcall init_Random();
  float __fastcall is_Minus(float value);
  void __fastcall init_Arrays();

void __fastcall TForm1::init_Random()
{
srand(time(NULL));
}
float __fastcall TForm1::is_Minus(float value)
{
int x;
x=rand()%12;
if(x==0)
value*=-1.;
else if(x==1)
value=0.;
else if(x==2)
value=0.;
else if(x==3)
value*=-1.;

return value;
}
void __fastcall TForm1::init_Arrays()
{
int i;
for(i=0;i<5;i++)
{
x1[i]=is_Minus((1.*(rand()%100))/100.);
x2[i]=is_Minus((1.*(rand()%100))/100.);
x3[i]=is_Minus((1.*(rand()%100))/100.);
y1[i]=is_Minus((1.*(rand()%100))/100.);
y2[i]=is_Minus((1.*(rand()%100))/100.);
y3[i]=is_Minus((1.*(rand()%100))/100.);

}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Random();
 init_Arrays();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
 int i,j;
 float a,b;
 a=1.*(rand()%65535);
 b=1.*(rand()%65535);
 i=0;
 do
 {
  j=rand()%5;
  switch(j)
  {
 case 0: a=a*x1[0]+b*y1[0]+x2[0]; b=a*y2[0]+b*x3[0]+y3[0];break;
 case 1: a=a*x1[1]+b*y1[1]+x2[1]; b=a*y2[1]+b*x3[1]+y3[1];break;
 case 2: a=a*x1[2]+b*y1[2]+x2[2]; b=a*y2[2]+b*x3[2]+y3[2];break;
 case 3: a=a*x1[3]+b*y1[3]+x2[3]; b=a*y2[3]+b*x3[3]+y3[3];break;
 case 4: a=a*x1[4]+b*y1[4]+x2[4]; b=a*y2[4]+b*x3[4]+y3[4];break;
  }
  Canvas->Pixels[Form1->Width/2+Floor(40*a)][Form1->Height/2-Floor(40*b)]=RGB(rand()%255,rand()%255,rand()%255);
  i++;
}while(i<9000000);
}

środa, 18 stycznia 2017

Writing binary data to a file



private: // User declarations
int delta,a,b,c;
float x1,x2;
int more_Delta[20][3];
int less_Delta[20][3];
int is_Delta[20][3];
void __fastcall init_Random();
int __fastcall is_Minus(int value);

#define min 1
#define max_value 10
#define max 20
TForm6 *Form6;
//---------------------------------------------------------------------------
__fastcall TForm6::TForm6(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
 void __fastcall TForm6::init_Random()
 {
srand(time(NULL));
 }
 int __fastcall TForm6::is_Minus(int value)
 {
int x;
x=rand()%2;
if(x==0)
 value*=-1;
return value;
 }
void __fastcall TForm6::FormCreate(TObject *Sender)
{
 init_Random();
 int i,sum,j;

 i=0;
 do
 {
  do{
   a=rand()%max_value+min;
   b=rand()%max_value+min;
   c=rand()%max_value+min;
   a=is_Minus(a);
   b=is_Minus(b);
   c=is_Minus(c);
   delta=(b*b)-(4*a*c);
   }while(delta<0);
   more_Delta[i][0]=a;
   more_Delta[i][1]=b;
   more_Delta[i][2]=c;
   i++;
 }while(i<max);
 i=0;
 do
 {
  do{
   a=rand()%max_value+min;
   b=rand()%max_value+min;
   c=rand()%max_value+min;
   a=is_Minus(a);
   b=is_Minus(b);
   c=is_Minus(c);
   delta=(b*b)-(4*a*c);
   }while(delta>0);
   less_Delta[i][0]=a;
   less_Delta[i][1]=b;
   less_Delta[i][2]=c;
   i++;
 }while(i<max);
 i=0;
 do
 {
  do{
   a=rand()%max_value+min;
   b=rand()%max_value+min;
   c=rand()%max_value+min;
   a=is_Minus(a);
   b=is_Minus(b);
   c=is_Minus(c);
   delta=(b*b)-(4*a*c);
   }while(delta!=0);
   is_Delta[i][0]=a;
   is_Delta[i][1]=b;
   is_Delta[i][2]=c;
   i++;
 }while(i<max);
 for(i=0;i<max;i++)
 {
StringGrid1->Cells[0][i]=IntToStr(more_Delta[i][0]);
StringGrid1->Cells[1][i]=IntToStr(more_Delta[i][1]);
StringGrid1->Cells[2][i]=IntToStr(more_Delta[i][2]);
StringGrid2->Cells[0][i]=IntToStr(less_Delta[i][0]);
StringGrid2->Cells[1][i]=IntToStr(less_Delta[i][1]);
StringGrid2->Cells[2][i]=IntToStr(less_Delta[i][2]);
StringGrid3->Cells[0][i]=IntToStr(is_Delta[i][0]);
StringGrid3->Cells[1][i]=IntToStr(is_Delta[i][1]);
StringGrid3->Cells[2][i]=IntToStr(is_Delta[i][2]);
 }
}
//---------------------------------------------------------------------------

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

void __fastcall TForm6::Button1Click(TObject *Sender)
{
 FILE *my_file;
 int i;
 my_file=fopen("equal.txt","w");
 fprintf(my_file,"%s","delta>0");
 fprintf(my_file,"\n");
 for(i=0;i<max;i++)
 {
fprintf(my_file,"%d ",more_Delta[i][0]);
fprintf(my_file,"%d ",more_Delta[i][1]);
fprintf(my_file,"%d ",more_Delta[i][2]);
fprintf(my_file,"\n");
 }
 fprintf(my_file,"%s","delta<0");
 fprintf(my_file,"\n");
 for(i=0;i<max;i++)
 {
fprintf(my_file,"%d ",less_Delta[i][0]);
fprintf(my_file,"%d ",less_Delta[i][1]);
fprintf(my_file,"%d ",less_Delta[i][2]);
fprintf(my_file,"\n");
 }
 fprintf(my_file,"%s","delta=0");
 fprintf(my_file,"\n");
 for(i=0;i<max;i++)
 {
fprintf(my_file,"%d ",is_Delta[i][0]);
fprintf(my_file,"%d ",is_Delta[i][1]);
fprintf(my_file,"%d ",is_Delta[i][2]);
fprintf(my_file,"\n");
 }
 fclose(my_file);
}

Result:


niedziela, 15 stycznia 2017

Using function BeginThread in C++ Builder




TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
HANDLE my_Event;
SECURITY_ATTRIBUTES my_attributes={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};
UINT my_ID;
LONG my_Thread=0;
 bool __fastcall 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;
 }
int __fastcall writeln_First(LPVOID value)
{
 int i;
 i=1;
 for(;;)
 {
  if(is_First(i))
   Form1->ListBox1->Items->Add(IntToStr(i));
  WaitForSingleObject((LPVOID)my_Thread,100);
  ++i;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 ListBox1->Items->Clear();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
       ;
 my_Thread=BeginThread(&my_attributes,4096,writeln_First,this,CREATE_SUSPENDED,my_ID);
 if(my_Thread==(int)INVALID_HANDLE_VALUE)
 {
  ShowMessage("Thread error!");
 }
 else
 {
  ResumeThread((LPVOID)my_Thread);
  if(my_Event)
  {
   WaitForSingleObject(my_Event,150);
   CloseHandle(my_Event);
   Button1->Enabled=FALSE;
  }

 }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 SuspendThread((LPVOID)my_Thread);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 ResumeThread((LPVOID)my_Thread);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
 CloseHandle((LPVOID)my_Thread);
 Close();
}

sobota, 14 stycznia 2017

Save the date encoded file (gcc/g++)

Result:

Write file name:
 my_normal_with_date_file1
Write code file name:
 my_code_with_date_file1


------------------
(program exited with code: 0)
Press return to continue

cat my_normal_with_date_file1
Sat Jan 14 11:38:50 2017

cat my_code_with_date_file1
Qzu;Qzu;*(!*();*+)*;

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
using namespace std;

void init_Random()
{
    srand(time(NULL));
}
char *my_xor(char *text, char key)
{
    int i,j;
    char *temp_text=new char[48];
    j=strlen(text);
    for(i=0;i<j+1;i++)
     temp_text[i]=text[i]^key;
    return temp_text;
}
char *plus_text(char *text)
{
    int i,j;
    char *temp_text=new char[48];
    j=strlen(text);
    for(i=0;i<j+1;j++)
     temp_text[i]=255+text[i];
    return temp_text;
}
char *minus_text(char *text)
{
    int i,j;
    char *temp_text=new char[48];
    j=strlen(text);
    for(i=0;i<j+1;i++)
     temp_text[i]=255-text[i];
    return temp_text;
   
   
}
char *random_text(char *text)
{
    int i,j;
    i=rand()%3;
    char *temp_text;
    switch(i)
    {
        case 0: j=rand()%255; temp_text=my_xor(text,j);break;
        case 1: temp_text=plus_text(text);break;
        case 2: temp_text=minus_text(text);break;
    }
  return temp_text;
   
}
char *now_time()
{
    time_t n_time=time(NULL);
    return asctime(localtime(&n_time));
}
int main(int argc, char **argv)
{
    init_Random();
    char file_name[48],code_name[48];
    printf("Write file name:\n ");
    scanf("%s",file_name);
    printf("Write code file name:\n ");
    scanf("%s",code_name);
    char *time_text;
    char *code_text;
    time_text=now_time();
    code_text=random_text(time_text);
   
    int file_open;
    size_t file_length;
    file_length=strlen(time_text);
    file_open=open(file_name,O_WRONLY | O_CREAT | O_APPEND, 0666);
    write(file_open,time_text,file_length);
    close(file_open);
    FILE *next_file;
    next_file=fopen(code_name,"w");
    fprintf(next_file,"%s",code_text);
    fclose(next_file);
   

    return 0;
}

 

środa, 11 stycznia 2017

Tree data structure (Visual C++)


My file where is my structure - date.h 

#include <stdio.h>


struct Base
{
struct Base *left;
struct Base *right;
System::Int32 value;
};

and main program:

#pragma endregion
int *first_Value;
System::Void init_Random()
{
srand(time(NULL));
}
System::Boolean is_First(int x)
{
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
if(x%i==0)
 ++sum;
if(sum==2)
 return true;
else
 return false;
}
System::Void itemsAdd(struct Base *base)
{

if(base!=NULL)
{
itemsAdd(base->left);
listBox1->Items->Add(base->value.ToString());
itemsAdd(base->right);
}
}
struct Base *my_Tree(struct Base *base, int my_value)
{
int temp;
temp=sizeof(struct Base);
if(base==NULL)
{
base=(struct Base*) malloc(temp);
base->value=my_value;
base->left=NULL;
                base->right=NULL;
}
else
if(base->value>my_value)
 base->left=my_Tree(base->left,my_value);
else
 base->right=my_Tree(base->right,my_value);
return (base);
}


private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
 init_Random();
 first_Value=new int[1000];
 int i,j;
 i=0;
 j=1;
 do
 {
if(is_First(j))
{
first_Value[i]=j;
i++;
j++;
}
else
j++;

 }while(i<1000);
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
delete first_Value;
                 Close();
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
int random_value,i;
                 struct Base *down;
                 down=NULL;
for(i=0;i<4;i++)
{
                 random_value=rand()%1000;
                 down=(struct Base*) my_Tree(down,first_Value[random_value]);
                 itemsAdd(down);
}
}
};
}