niedziela, 19 listopada 2017

Sorting, dynamic arrays, example - GNU GCC Compiler

#include <iostream>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

#define max 20

int **TABLE,*FIRST_VALUES,*SUM_VALUES;
int **MIN_TABLE,**MAX_TABLE;
float *AVERAGE_VALUES;

void sort_Table(int *tab)
{
   int i,j,temp;
   for(i=1;i<max;i++)
    for(j=max-1;j>=i;j--)
    if(tab[j]<tab[j-1])
   {
       temp=tab[j-1];
       tab[j-1]=tab[j];
       tab[j]=temp;
   }

}

int comp_Values(int value1,int value2)
{
    return (value1<value2);
}
void move_Values(int *value1,int *value2)
{
    int temp_value;
    temp_value=*value1;
    *value1=*value2;
    *value2=temp_value;
}
void max_Sort(int *table)
{
    int i,j;
    for(i=0;i<max;i++)
    {
        for(j=0;j<max-1;j++)
        {
            if(comp_Values(table[j],table[j+1]))
                move_Values(&table[j],&table[j+1]);
        }
    }
}

bool 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 sum_Table(int *tab, int size)
{
    int i,result;
    result=0;
    for(i=0;i<size;i++)
        result+=tab[i];
    return result;

}

float average_Table(int *tab, int size)
{

  float result;
  result=(1.0*sum_Table(tab,size))/(1.0*size);
  return result;
}

int main()
{
    srand(time(NULL));
    int i,j,k;
    TABLE=new int *[max];
    MIN_TABLE=new int *[max];
    MAX_TABLE=new int *[max];
    FIRST_VALUES=new int[max];
    SUM_VALUES=new int[max];
    AVERAGE_VALUES=new float [max];
    for(i=0;i<max;i++)
    {

        MIN_TABLE[i]=new int[max];
        MAX_TABLE[i]=new int[max];
        TABLE[i]=new int [max];
    }
    i=0;
    j=2;
    do
    {
        if(is_First(j))
        {

            FIRST_VALUES[i]=j;
            SUM_VALUES[i]=0;
            ++i;
        }
        ++j;

    }while(i<max);

    for(i=0;i<max;i++)
    {


        for(j=0;j<max;j++)
        {


         k=FIRST_VALUES[rand()%20];
         TABLE[i][j]=k;
         MIN_TABLE[i][j]=k;
         MAX_TABLE[i][j]=k;
         SUM_VALUES[i]+=TABLE[i][j];

        }

    }
    for(i=0;i<max;i++)
        AVERAGE_VALUES[i]=(1.0*SUM_VALUES[i])/(1.0*max);

    for(i=0;i<max;i++)
    {

       max_Sort(MAX_TABLE[i]);
      sort_Table(MIN_TABLE[i]);

    }

    cout<<"Original table: \n";
    for(i=0;i<max;i++)
    {
        for(j=0;j<max;j++)
        {
            cout<<TABLE[i][j]<<"  ";
        }
        cout<<"Sum =  "<<SUM_VALUES[i];
        cout<<" Average ="<<AVERAGE_VALUES[i];
        cout<<"\n";
    }

    cout<<"\n\n Sorted in descending order: \n";
    for(i=0;i<max;i++)
    {
        for(j=0;j<max;j++)
        {
            cout<<MIN_TABLE[i][j]<<"  ";
        }
        cout<<"Sum =  "<<SUM_VALUES[i];
        cout<<" Average ="<<AVERAGE_VALUES[i];
        cout<<"\n";
    }

     cout<<"\n\n Sorted ascending: \n";
    for(i=0;i<max;i++)
    {
        for(j=0;j<max;j++)
        {
            cout<<MAX_TABLE[i][j]<<"  ";
        }
        cout<<"Sum =  "<<SUM_VALUES[i];
        cout<<" Average ="<<AVERAGE_VALUES[i];
        cout<<"\n";
    }



    for(i=0;i<max;i++)
    {


        delete [] TABLE[i];
        delete [] MIN_TABLE[i];
        delete [] MAX_TABLE[i];
    }
    delete [] MAX_TABLE;
    delete [] MIN_TABLE;
    delete [] TABLE;
    delete AVERAGE_VALUES;
    delete SUM_VALUES;
    delete FIRST_VALUES;

    getch();
    return 0;
}

poniedziałek, 13 listopada 2017

Example type Binary (Visual C++)



#pragma endregion
private: System::String^ text_Binary(char value)
{
unsigned char test_bin[11]={1,2,4,8,16,32,64,128,256,512,1024};
String^ text;
text="";
System::Int16 bytes;
System::Int16 i;
for(i=10;i>=0;i--)
{
bytes=(test_bin[i]&value);
if(bytes!=0)
text+="1";
else
text+="0";
}
return text;
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
System::Int16 i;
System::Int16 j;
System::Int16 k;
for(i=0;i<1024;i++)
{
listBox1->Items->Add(text_Binary(i));
listBox2->Items->Add(text_Binary(~i));
listBox3->Items->Add(text_Binary(i<<1));
}

niedziela, 12 listopada 2017

Examples type Variant (C++ Builder)

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

#define max 50

void init_Random()
{
srand(time(NULL));
}
bool 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;
}

class test_Variant
{
  private:

   Variant Array;
   typedef int temp_Array[max];
   temp_Array *t_A,*t_B;
   int v_min,v_max;

  public:
   test_Variant()
   {
   int i,j,k;
   init_Random();
   Array=VarArrayCreate(OPENARRAY(int,(0,max)),varInteger);
   t_A=(temp_Array *)VarArrayLock(Array);
   t_B=(temp_Array *)VarArrayLock(Array);
   i=0;
   j=2;
   do
   {
if(is_First(j))
{
(*t_A)[i]=j;
++i;
}
++j;
   }while(i<max);
   v_min=VarArrayLowBound(t_A,VarArrayDimCount(t_A));
   v_max=VarArrayHighBound(t_A,VarArrayDimCount(t_A));
   for(i=v_min;i<v_max;i++)
   {
k=rand()%max;
(*t_B)[i]=(*t_A)[k];
   }
   }
   ~test_Variant()
   {
   VarArrayUnlock(t_A);
   VarArrayUnlock(t_B);
   }
   void Write(TListBox *LB)
   {

int i;
for(i=v_min;i<v_max;i++)
  LB->Items->Add(IntToStr(i+1)+" ) -  "+VarToStr((*t_A)[i])+"  -  "+VarToStr((*t_B)[i]));

   }
};


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 test_Variant *t_V=new class test_Variant;
 t_V->Write(ListBox1);
}

niedziela, 5 listopada 2017

Class reading Shell Folders (C++ Builder)




#include <vcl.h>
#include <shlobj.h>
#include <Registry.hpp>

class read_Directory
{
private:
UnicodeString my_Key;
UnicodeString my_Result;
UnicodeString text_Folders;
public:
read_Directory(UnicodeString tF)
{
text_Folders=tF;
TRegistry *reg=new TRegistry();
reg->Access=KEY_READ;
reg->RootKey=HKEY_CURRENT_USER;
my_Key="\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
if(reg->OpenKey(my_Key,false))
{
  if(reg->ValueExists(text_Folders))
my_Result=reg->ReadString(text_Folders);
  else
my_Result="Value not exists";
}
else
{
  my_Result="Key not exists";
}
delete reg;
}
UnicodeString Write()
{
return my_Result;
}
};



Example:


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 read_Directory r_D1("Personal");
 Edit1->Text=r_D1.Write();
 read_Directory r_D2("History");
 Edit2->Text=r_D2.Write();
 read_Directory r_D3("Cookies");
 Edit3->Text=r_D3.Write();
 read_Directory r_D4("Linux");
 Edit4->Text=r_D4.Write();
}

wtorek, 31 października 2017

Dependencies of prime numbers display and save to file (Visual C++)


In the File:



using namespace System::IO;
#pragma endregion
private:
int *table_first,*sum_t,*sum_max;
float *ms_sum,*ms_first;
private:
System::Boolean is_First(System::Int16 Value)
{
System::Int16 i;
System::Int16 sum;
for(i=1;i<Value+1;i++)
if(Value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
private:
System::Void init_Table()
{
System::Int16 i;
System::Int16 j;
System::Int16 k;
System::Int16 sum;
i=0;
j=2;
do
{
              
 if(is_First(j))
 {
 sum=0;
 table_first[i]=j;
 sum_t[i]=sum_Table(table_first,i+1);
 for(k=1;k<j+1;k++)
 sum+=k;
 sum_max[i]=sum;
 ms_sum[i]=(1.0*sum_max[i])/(sum_t[i]*1.0);
 ms_first[i]=(1.0*sum_t[i])/(1.0*table_first[i]);

 ++i;
 }
 ++j;
}while(i<50);
}
private:
System::Int16 sum_Table(int *table, System::Int16 size)
{
System::Int16 i;
System::Int16 sum;
sum=0;
for(i=0;i<size;i++)
sum+=table[i];
return sum;
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
delete ms_sum;
delete ms_first;
delete table_first;
delete sum_max;
delete sum_t;

 
 Close();
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
table_first=new int[50];
sum_max=new int[50];
sum_t=new int[50];
ms_sum=new float[50];
ms_first=new float[50];
init_Table();
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
System::Int16 i;
System::Int16 sum;
listBox1->Items->Clear();
listBox2->Items->Clear();
listBox3->Items->Clear();
listBox4->Items->Clear();
listBox5->Items->Clear();
for(i=0;i<50;i++)
{
listBox1->Items->Add(table_first[i].ToString());
listBox2->Items->Add(sum_t[i].ToString());
listBox3->Items->Add(sum_max[i].ToString());
listBox4->Items->Add(ms_sum[i].ToString());
listBox5->Items->Add(ms_first[i].ToString());
 
}
StreamWriter^ mFile=gcnew StreamWriter("mFile.txt",0,System::Text::Encoding::Default);
for(i=0;i<50;i++)
{
mFile->WriteLine(table_first[i].ToString());
mFile->WriteLine(sum_t[i].ToString());
mFile->WriteLine(sum_max[i].ToString());
mFile->WriteLine(ms_sum[i].ToString());
mFile->WriteLine(ms_first[i].ToString());
}
mFile->Close();
 
}
};
}



sobota, 28 października 2017

File attributes (C++ Builder)



private:    // User declarations
    HANDLE idFile;
    TSearchRec tempSR;
    UnicodeString f,t;
    DWORD sizeB;
    wchar_t buffer_Memo[8192],s;
    void __fastcall connect_Components();


TForm1 *Form1;
SECURITY_ATTRIBUTES app_Attr={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::connect_Components()
{
    f="FALSE";
    t="TRUE";
    s='.';
    DirectoryListBox1->FileList=FileListBox1;
    DriveComboBox1->DirList=DirectoryListBox1;
    FileListBox1->FileEdit=Edit1;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 connect_Components();
}
//---------------------------------------------------------------------------

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


void __fastcall TForm1::FileListBox1Change(TObject *Sender)
{
 int i;
 tempSR.Name=Edit1->Text;
 tempSR.Attr=FileGetAttr(tempSR.Name);
 if((tempSR.Attr && faArchive)==faArchive)
  Edit2->Text=t;
 else
  Edit2->Text=f;
 if((tempSR.Attr & faSysFile)==faSysFile)
  Edit3->Text=t;
 else
  Edit3->Text=f;
 if((tempSR.Attr & faReadOnly)==faReadOnly)
  Edit4->Text=t;
 else
  Edit4->Text=f;
 if((tempSR.Attr & faHidden)==faHidden)
  Edit5->Text=t;
 else
  Edit5->Text=f;
  memset(buffer_Memo,0,sizeof(buffer_Memo)-1);
  idFile=CreateFile(FileListBox1->FileName.c_str(),GENERIC_READ,FILE_SHARE_READ,
  &app_Attr,OPEN_EXISTING,0,NULL);
  if(idFile!=INVALID_HANDLE_VALUE)
   ReadFile(idFile,&buffer_Memo,sizeof(buffer_Memo),&sizeB,NULL);
  for(i=0;i<sizeof(buffer_Memo)-1;i++)
   if(buffer_Memo[i]==NULL)
    buffer_Memo[i]=s;
  Memo1->Text=buffer_Memo;
  CloseHandle(idFile);
}

piątek, 6 października 2017

Use a queue to generate web addresses with a component dataGridView (Visual C++)




Code class in a separate file:

template <class mainType> class MY
{
private:
System::Int64 start,end,max;
mainType *mT;
public:
MY(System::Int64 value)
{
mT=new mainType[max+1];
start=0;
end=0;
max=value;

}
System::Boolean is_Empty()
{
if(start==end)
return true;
else
return false;
}
System::Int64 order(mainType &value)
{
if(start==end)
return -1;
value=mT[start++];
if(start>max)
start=0;
return 1;

}
System::Void insert(mainType value)
{
mT[end++]=value;
if(end>max)
end=0;
}
};

Main program:

#pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
char **tables,*WWW;
tables=new char*[10];
System::Int64 i;
System::Int64 temp_Bool;
for(i=0;i<10;i++)
tables[i]=new char[20];
tables[0]="www.hpe.com";
tables[1]="www.ibm.com";
tables[2]="www.google.com";
tables[3]="www.cnn.com";
tables[4]="www.yahoo.com";
tables[5]="www.nasa.org";
tables[6]="www.microsoft.com";
tables[7]="www.linux.com";
tables[8]="www.embarcadero.com";
tables[9]="www.java.com";
array<System::String^>^ string_Tables=gcnew array<System::String^>(10);
 
MY<char*> FIFO_values(11);
dataGridView1->ColumnCount=1;
dataGridView1->RowCount=10;
 
for(i=0;i<10;i++)
{
FIFO_values.insert(tables[i]);
System::String^ temp=gcnew System::String(tables[i]);
dataGridView1->Rows[i]->Cells[0]->Value=temp;
 

}
for(i=0;i<11;i++)
{
temp_Bool=FIFO_values.order(WWW);
if(temp_Bool==true)
{
System::String^ temp=gcnew System::String(WWW);
listBox1->Items->Add("You can use - "+temp);
}

}

for(i=0;i<10;i++)
delete [] tables[i];
delete [] *tables;


}
private: System::Void dataGridView1_CellContentClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
System::String^ myWWW;
myWWW="http:/";
myWWW=safe_cast<DataGridView^>(sender)->CurrentCell->Value->ToString();
System::Diagnostics::Process::Start(myWWW);
}
};
}

I tried to use the structure - array<System::String^>^ tables=gcnew array<System::String^>(10),but for costructor class (new[max+1]) type String^ and array showed errors. So i used - char*.