wtorek, 26 września 2017

Dynamically created control (class listBox) with its own function looking for files in the directory (Visual C++)

File Test_Cl.h :


using namespace System::IO;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

ref class files_ListBox : public System::Windows::Forms::ListBox
{
public: System::Void find_Files(System::String^ directory,System::String^ files)
{
System::Int64 i;

DirectoryInfo^ my_directory=gcnew DirectoryInfo(directory);
array<FileInfo^>^ my_files=my_directory->GetFiles(files);
for(i=0;i<my_files->Length;i++)
{
this->Items->Add(my_files[i]->Name+" "+my_files[i]->Length+" "+my_files[i]->CreationTime);
}

}

};

And a fragment of the file - Form1.h : 

#pragma once

#include "test_Cl.h"

private:
/// <summary>
/// Required designer variable.
/// </summary>
files_ListBox test_FLB;
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->test_FLB=(gcnew files_ListBox());
this->SuspendLayout();
this->test_FLB->Name=L"test_FLB";
this->test_FLB->Location=System::Drawing::Point(Form1::Width/2,Form1::Height/2);
this->test_FLB->Size=System::Drawing::Size(200,200);



#pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
test_FLB->find_Files("c:\\Windows","*.txt*");
}
};
}


niedziela, 24 września 2017

Collection of ASCII (class in Visual C++)

class collection_of_ASCII
{
private:
System::Char set_Ascii[256];
public: 
collection_of_ASCII();
System::Boolean is_in_Colection(System::Char value);
collection_of_ASCII& Connect(collection_of_ASCII temp_Collection);
collection_of_ASCII& operator -(System::Char value);
collection_of_ASCII& operator +(System::Char value);
};

collection_of_ASCII::collection_of_ASCII()
{
System::Int64 i;
for(i=0;i<256;i++)
set_Ascii[i]=0;
}
System::Boolean collection_of_ASCII::is_in_Colection(System::Char value)
{
if(set_Ascii[value]==-1)
return true;
else
return false;
}
collection_of_ASCII& collection_of_ASCII::Connect(collection_of_ASCII temp_Collection)
{
System::Int64 i;
for(i=0;i<256;i++)
if(temp_Collection.is_in_Colection(i))
set_Ascii[i]=-1;
return *this;
}
collection_of_ASCII& collection_of_ASCII::operator -(System::Char value)
{
 set_Ascii[value]=0;
 return *this;
}
collection_of_ASCII& collection_of_ASCII::operator +(System::Char value)
{
set_Ascii[value]=-1;
return *this;
}

sobota, 23 września 2017

Example class Thread (C++ Builder)

#include <vcl.h>
#include <math.h>
using namespace std;


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

void write_ListBox(TListBox *LB, int value)
{
LB->Items->Add(IntToStr(value));
}

SECURITY_ATTRIBUTES this_attributes={sizeof(SECURITY_ATTRIBUTES),
    NULL,TRUE};



class example_Thread
{
private:
TListBox *listB;
LONG run_T;


HANDLE my_Event;
public:
example_Thread(TListBox *tempBox);
~example_Thread();
UINT my_ID;
int  next_First(LPVOID values);
void start_Thread();
void suspend_Thread();
void resume_Thread();
};

example_Thread::example_Thread(TListBox *tempBox)
{
run_T=0;
listB=tempBox;

}
example_Thread::~example_Thread()
{
CloseHandle((LPVOID)run_T);
}
int example_Thread::next_First(LPVOID values)
{
int x;
x=1;
for(;;)
{
  if(is_First(x))
write_ListBox(listB,x);
  WaitForSingleObject((LPVOID)run_T,500);
  ++x;
}
}
void example_Thread::start_Thread()
{

run_T=BeginThread(&this_attributes,4096,next_First,this,CREATE_SUSPENDED,my_ID);
if(run_T==(int)INVALID_HANDLE_VALUE)
{
ShowMessage("ERROR THREADS!!!");
}
else
{
ResumeThread((LPVOID)run_T);
my_Event=CreateEvent(NULL,FALSE,FALSE,NULL);
if(my_Event)
{
WaitForSingleObject(my_Event,500);
CloseHandle(my_Event);
}
}
}
void example_Thread::suspend_Thread()
{
SuspendThread((LPVOID)run_T);
}
void example_Thread::resume_Thread()
{
ResumeThread((LPVOID)run_T);
}

piątek, 22 września 2017

Class to draw a tree (C++ Builder)

file - m_tree.h



#ifndef m_treeH
#define m_treeH
//---------------------------------------------------------------------------
#include <stdlib.h>
#include <time.h>
#include <vcl.h>
class draw_Tree
{
  private:
   float x1[5],x2[5],x3[5],x4[5],x5[6],x6[6],x7[6],x8[6],x9[6],
   x10[6],x11[6],x12[6];
   TForm *tf;


  public:
   draw_Tree();
   void init_Date();
   void Draw(TForm *form);
} ;


#endif


file - m_tree.cpp

#include <stdlib.h>
#include <time.h>
#include <vcl.h>
#pragma hdrstop

#include "m_tree.h"

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

#pragma package(smart_init)

#define my_MAX 65535
draw_Tree::draw_Tree()
{
  srand(time(NULL));

}

void draw_Tree::init_Date()
{
 int i,value,temp;
 for(i=0;i<5;i++)
 {
   value=rand()%100+1;
   temp=rand()%7;
   x1[i]=(1.0*value/100.);
   if(temp<4)
    x1[i]*=-1.;
   x2[i]=(1.0*value/100.);
   if(temp<4)
    x2[i]*=-1.;
   x3[i]=(1.0*value/100.);
   if(temp<4)
    x3[i]*=-1.;
   x4[i]=(1.0*value/100.);
   if(temp<4)
   x4[i]*=-1.;
   x5[i]=(1.0*value/100.);
   if(temp<4)
   x5[i]*=-1.;
   x6[i]=(1.0*value/100.);
   if(temp<4)
    x6[i]*=-1.;
   x7[i]=(1.0*value/100.);
   if(temp<4)
   x7[i]*=-1.;
   x8[i]=(1.0*value/100.);
   if(temp<4)
    x8[i]*=-1.;
   x9[i]=(1.0*value/100.);
   if(temp<4)
   x9[i]*=-1.;
   x10[i]=(1.0*value/100.);
   if(temp<4)
    x10[i]*=-1.;
   x11[i]=(1.0*value/100.);
   if(temp<4)
   x11[i]*=-1.;
   x12[i]=(1.0*value/100.);
   if(temp<4)
    x12[i]*=-1.;

 }
}
void draw_Tree::Draw(TForm *form)
{
  int i,j,k;
  float vx_value,vy_value;

  tf=form;
  vx_value=rand()%my_MAX+1;
  vy_value=rand()%my_MAX+1;
  i=0;
  do
  {
   j=rand()%8;
   switch(j)
   {
    case 0:
    vx_value=vx_value*x1[0]+vy_value*x2[0]+x3[0];
    vy_value=vx_value*x4[0]+vy_value*x5[0]+x6[0];
    break;
    case 1:
    vx_value=vx_value*x1[1]+vy_value*x2[1]+x3[1];
    vy_value=vx_value*x4[1]+vy_value*x5[1]+x6[1];
    break;
    case 2:
    vx_value=vx_value*x1[2]+vy_value*x2[2]+x3[2];
    vy_value=vx_value*x4[2]+vy_value*x5[2]+x6[2];
    break;
    case 3:
    vx_value=vx_value*x1[3]+vy_value*x2[3]+x3[3];
    vy_value=vx_value*x4[3]+vy_value*x5[3]+x6[3];
    break;
    case 4:
    vx_value=vx_value*x7[0]+vy_value*x8[0]+x9[0];
    vy_value=vx_value*x10[0]+vy_value*x11[0]+x12[0];
    break;
    case 5:
    vx_value=vx_value*x7[1]+vy_value*x8[1]+x9[1];
    vy_value=vx_value*x10[1]+vy_value*x11[1]+x12[1];
    break;
    case 6:
    vx_value=vx_value*x7[2]+vy_value*x8[2]+x9[2];
    vy_value=vx_value*x10[2]+vy_value*x11[2]+x12[2];
    break;
    case 7:
    vx_value=vx_value*x7[3]+vy_value*x8[3]+x9[3];
    vy_value=vx_value*x10[3]+vy_value*x11[3]+x12[3];
    break;


   }
   tf->Canvas->Pixels[0.5*tf->Width+Floor(5*vx_value)]
   [0.5*tf->Height-Floor(5*vy_value)]=clGreen;
   ++i;
  }while(i<(5.*my_MAX));
}

Use class in the programm : 

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 draw_Tree DRAW;
 DRAW.init_Date();
 DRAW.Draw(Form1);

}


środa, 13 września 2017

Sending graphics from the picurBox to the server FTP (Visual C++)




private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
;
System::Double PI=3.1415926535;
System::Int64 Width=pictureBox1->Width/2;
 
System::Double x,y,z,v,i;
Graphics^ g=pictureBox1->CreateGraphics();
System::Drawing::Color my_color;
Random^ random_value=gcnew Random();
for(i=(-2.*PI);i<(2.*PI);i+=0.03)
{
x=Width*sin(i);
y=Width*cos(i);
v=Width*1.0;
z=(-1.0*Width*sin(i));
my_color=System::Drawing::Color::FromArgb(random_value->Next(255),random_value->Next(255),
random_value->Next(255));
Pen^ my_pen=gcnew Pen(my_color);
g->DrawLine(my_pen,int((1.0*Width)+x),int((1.0*Width)+y),int((1.0*Width)+z),int((1.0*Width)+v));
}


}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
System::String^ file_Name=textBox1->Text;
System::String^ FTP_Name=textBox2->Text;
pictureBox1->Image->Save(file_Name);
Uri^ uri_adress=gcnew Uri("ftp://"+FTP_Name+"/"+Path::GetFileName(file_Name));
FtpWebRequest^ my_req=dynamic_cast<FtpWebRequest^>(WebRequest::Create(uri_adress));
my_req->Credentials=gcnew
NetworkCredential("anonymus","my_name@server.com");
my_req->Method=WebRequestMethods::Ftp::UploadFile;
FileStream^ send_file=gcnew FileStream(file_Name,FileMode::Open);
Stream^ my_stream=my_req->GetRequestStream();
System::Int64 bytes;
array<Byte> ^ table=gcnew array<Byte>(1024);
do
{
bytes=send_file->Read(table,0,1024);
my_stream->Write(table,0,bytes);
}while(bytes!=0);
my_stream->Close();
 



}
};
}

czwartek, 7 września 2017

Effective graphical transformations - Visual C++





I use a Polsih book - "C++ Builder 20 efektownych programów" - Andrzej Stasiewicz. I convert C++ Builder code to Visual C++. Sometimes is not easy. Two environments, a completely different philosophy.

sobota, 26 sierpnia 2017

Transormations prime number and others (Visual C++)





#pragma endregion
System::Boolean 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;
}
System::Void init_Array(int *table, int size)
{
int i,j;
i=0;
j=2;
do
{
              if(is_First(j))
 {
 table[i]=j;
 ++i;
 }
 ++j;
}while(i<size);
}
System::Int32 sum_Table(int *table, int size)
{
int i,sum;
sum=0;
for(i=0;i<size;i++)
sum+=table[i];
return sum;
}
System::Int32 multiplication_Table(int *table, int size)
{
int i,multi;
multi=1;
for(i=0;i<size;i++)
multi*=table[i];
return multi;
}
System::Double multi_div_sum_Table(int *table, int size)
{
int i;
double value;
value=(1.*multiplication_Table(table,size))/(1.*sum_Table(table,size));
return value;
}

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
int first_Table[9],sum_Tab[9],multi_Tab[9],not_first_Table[9],sum_not_first_Table[9],
multi_not_first_Table[9],diff_first[8],diff_not_first[8],diff_first_sum[8],diff_not_first_sum[8],
diff_first_mul[8],diff_not_first_mul[8];
double mul_dev_sum[9],mul_not_dev_sum[9],diff_mul_dev_first[8],diff_not_mul_dev[8];
int first_plus_not[9],not_plus_first[9],sum_div_first[9],sum_div_not_first[9];
double value_mal_first_div[9],value_mal_not_first_div[9];
int i,j;
init_Array(first_Table,9);
i=0;
j=1;

do
{
               if(!is_First(j))
  {
  not_first_Table[i]=j;
  ++i;
  }
  ++j;

}while(i<9);
for(i=0;i<9;i++)
{
listBox1->Items->Add(first_Table[i].ToString());
sum_Tab[i]=sum_Table(first_Table,i+1);
listBox2->Items->Add(sum_Tab[i].ToString());
multi_Tab[i]=multiplication_Table(first_Table,i+1);
listBox3->Items->Add(multi_Tab[i].ToString());
                 mul_dev_sum[i]=multi_div_sum_Table(first_Table,i+1);
listBox4->Items->Add(mul_dev_sum[i].ToString());
listBox5->Items->Add(not_first_Table[i].ToString());
sum_not_first_Table[i]=sum_Table(not_first_Table,i+1);
listBox6->Items->Add(sum_not_first_Table[i].ToString());
multi_not_first_Table[i]=multiplication_Table(not_first_Table,i+1);
listBox7->Items->Add(multi_not_first_Table[i].ToString());
mul_not_dev_sum[i]=multi_div_sum_Table(not_first_Table,i+1);
listBox8->Items->Add(mul_not_dev_sum[i].ToString());
 
}
for(i=0,j=1;i<8;i++,j++)
{
                diff_first[i]=first_Table[j]-first_Table[i];
                diff_not_first[i]=not_first_Table[j]-not_first_Table[i];
listBox9->Items->Add(diff_first[i].ToString());
listBox10->Items->Add(diff_not_first[i].ToString());
diff_first_sum[i]=sum_Tab[j]-sum_not_first_Table[i];
listBox11->Items->Add(diff_first_sum[i].ToString());
   diff_not_first_sum[i]=sum_not_first_Table[j]-sum_Tab[i];
listBox12->Items->Add(diff_not_first_sum[i].ToString());
diff_first_mul[i]=multi_Tab[j]- multi_not_first_Table[i];
listBox13->Items->Add(diff_first_mul[i].ToString());
diff_not_first_mul[i]=multi_not_first_Table[j]-multi_Tab[i];
listBox14->Items->Add(diff_not_first_mul[i].ToString());
diff_mul_dev_first[i]=mul_dev_sum[j]-mul_not_dev_sum[i];
listBox15->Items->Add(diff_mul_dev_first[i].ToString());
diff_not_mul_dev[i]=mul_not_dev_sum[j]-=mul_dev_sum[j];
listBox16->Items->Add(diff_not_mul_dev[i].ToString());


               
}
first_plus_not[0]=first_Table[0];
not_plus_first[0]=not_first_Table[0];
for(i=1,j=0;i<9;i++,j++)
{
first_plus_not[i]=first_Table[i]+diff_not_first_sum[j];
not_plus_first[i]=not_first_Table[i]+diff_first_sum[j];
}
for(i=0;i<9;i++)
{
listBox17->Items->Add(first_plus_not[i].ToString());
listBox18->Items->Add(not_plus_first[i].ToString());
sum_div_first[i]=(1.*sum_Tab[i])/(1.*first_Table[i]);
listBox19->Items->Add(sum_div_first[i].ToString());
sum_div_not_first[i]=(1.*sum_not_first_Table[i])/(1.*not_first_Table[i]);
listBox20->Items->Add(sum_div_not_first[i].ToString());
value_mal_first_div[i]=(1.*multi_Tab[i])/(1.*sum_Tab[i]*first_Table[i]);
listBox21->Items->Add(value_mal_first_div[i].ToString());
value_mal_not_first_div[i]=(1.* multi_not_first_Table[i])/(1.*sum_not_first_Table[i]*not_first_Table[i]);
listBox22->Items->Add(value_mal_not_first_div[i].ToString());
}
int only_first_in_First[9],only_not_first_in_not_First[9];
for(i=0;i<9;i++)
{
               only_first_in_First[i]=0;
  only_not_first_in_not_First[i]=0;

}

i=0;
 
do
{
               j=1;
  do
  {
                if(is_First(j))
{
only_first_in_First[i]+=j;
}
++j;
  }while(j<first_Table[i]);
  ++i;
}while(i<9);

i=0;

do
{
j=1;
do
{
if(!is_First(j))
{
                       only_not_first_in_not_First[i]+=j;
}
++j;
}while(j<not_first_Table[i]);
++i;
}while(i<9);
for(i=0;i<9;i++)
{
listBox23->Items->Add(only_first_in_First[i].ToString());
listBox24->Items->Add(only_not_first_in_not_First[i].ToString());
}
}
};
}