sobota, 24 marca 2018

Ulam spiral - C++ Builder


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::FormCreate(TObject *Sender)
{
 int table[21][21];
 int value;
 value=21*21;
 int i,j;
 int x1,y1,x2,y2;
 x1=0;y1=0;x2=20;y2=20;

 for(i=0;i<21;i++)
  for(j=0;j<21;j++)
   table[i][j]=0;
 while (value>0)
 {
  for(i=x2;i>x1-1;i--)
  {
  table[i][y2]=value;
  value--;
  }


  for(i=y2-1;i>y1-1;i--)
  {
  table[x1][i]=value;
  value--;
  }


  for(i=x1+1;i<x2+1;i++)
  {
  table[i][y1]=value;
  value--;
  }


  for(i=y1+1;i<y2;i++)
  {
  table[x2][i]=value;
  value--;
  }

x2--;
y2--;
x1++;
y1++;

 }
 for(i=0;i<21;i++)
  for(j=0;j<21;j++)
   StringGrid1->Cells[i][j]=IntToStr(table[i][j]);

 for(i=0;i<21;i++)
  for(j=0;j<21;j++)
   if(is_First(table[i][j]))
StringGrid2->Cells[i][j]="1";
   else
    StringGrid2->Cells[i][j]="O";
}

piątek, 23 marca 2018

Magic square - C++ Builder



A01 + A02+  A03 +A04=  X
  +        +         +        +
A05 + A06 + A07 +A08=  X
  +         +        +         + 
A09 + A10 + A11 + A12=  X
   +        +        +         +
A13 + A14 + A15 + A16=  X
   =        =        =         =
   X       X        X        X

And:
 A1+A6+A11+A16=X
 A4+A7+A10+A13=X


private: // User declarations
        int **Table;
        int value;

void __fastcall TForm1::Edit1Change(TObject *Sender)
{
 value=StrToInt(Edit1->Text);
 int temp,i,j;
 temp=value-20;
 Table[0][0]=temp;
 Table[2][1]=temp-1;
 Table[3][2]=temp+2;
 Table[1][3]=temp+1;
 Table[1][0]=1;
 Table[3][0]=7;
 Table[1][1]=8;
 Table[3][1]=2;
 Table[0][2]=5;
 Table[2][2]=3;
 Table[0][3]=4;
 Table[2][3]=6;
 Table[3][3]=9;
 Table[1][2]=10;
 Table[0][1]=11;
 Table[2][0]=12;
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   StringGrid1->Cells[i][j]=IntToStr(Table[i][j]);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 int i;
 Table=new int *[4];
 for(i=0;i<4;i++)
  Table[i]=new int[4];

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
 int i;
 for(i=0;i<4;i++)
  delete [] Table[i];
 delete [] *Table;
}

niedziela, 18 marca 2018

A class that specifies the properties and attributes of the file (C++ Builder)

#include <vcl.h>
#include <System.hpp>
#include <classes.hpp>

class info_File
{
private:
TListBox *tLBox;
TSystemTime time_System1;
TSystemTime time_System2;
UnicodeString file_Name;
UnicodeString directory_Name;
UnicodeString drive_Symbol;
UnicodeString extract_File;
UnicodeString extract_Path;
UnicodeString start_Date;
int size_Byte;
int number_Attr;
UnicodeString last_Modification;
TSearchRec rec_Info;
public:
info_File(TListBox *tempLBox,UnicodeString file)
{
file_Name=file;
tLBox=tempLBox;
}
UnicodeString write_file_Name()
{

return file_Name;
}
UnicodeString write_directory_Name()
{
directory_Name=ExtractFileDir(file_Name);
return directory_Name;
}
UnicodeString write_drive_Symbol()
{
   drive_Symbol=ExtractFileDrive(file_Name);
   return drive_Symbol;
}
UnicodeString write_extract_File()
{
extract_File=ExtractFileExt(file_Name);
return extract_File;
}
UnicodeString write_extract_Path()
{
extract_Path=ExtractRelativePath(GetCurrentDir(),file_Name);
return extract_Path;
}
int write_size_Byte()
{
size_Byte=rec_Info.Size;
return size_Byte;
}

int write_number_Attribute()
{
number_Attr=rec_Info.Attr;
return number_Attr;
}
UnicodeString write_start_Date()
{
FileTimeToSystemTime(&(rec_Info.FindData.ftCreationTime),&time_System1);
SystemTimeToTzSpecificLocalTime(NULL,&time_System1,&time_System2);
start_Date=DateTimeToStr(SystemTimeToDateTime(time_System2));
return start_Date;
}
UnicodeString write_last_Modification()
{
last_Modification=DateTimeToStr(FileDateToDateTime(rec_Info.Time));
return last_Modification;
}
void RAPORT()
{
tLBox->Items->Add("File name: "+write_file_Name());
tLBox->Items->Add("Directory name: "+write_directory_Name());
tLBox->Items->Add("Drive: "+write_drive_Symbol());
tLBox->Items->Add("Extract file: "+write_extract_File());
tLBox->Items->Add("Path: "+write_extract_Path());
tLBox->Items->Add("Bytes: - "+IntToStr(write_size_Byte()));
tLBox->Items->Add("Attributes: "+IntToStr(write_number_Attribute()));
tLBox->Items->Add("Start job: "+write_start_Date());
tLBox->Items->Add("Last modification: "+write_last_Modification());

}




};

wtorek, 13 marca 2018

Sorting using the heap-algorithm (C-gcc)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#define MAX 1000
#define SIZE 50

int 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 1;
    else
     return 0;
}
int *big_table;
int *original_table;
int *sorting_table;
int temp_value;;

 void init_Tables()
 {
    int i,j;
    temp_value=0;
    big_table=malloc(sizeof*big_table*(MAX+1));
    if(!big_table)
    {
        perror("malloc");
        return EXIT_FAILURE;
    }
   
    original_table=malloc(sizeof*original_table*(SIZE+1));
    if(!original_table)
    {
        perror("malloc");
        return EXIT_FAILURE;
    }
    sorting_table=malloc(sizeof*original_table*(SIZE+1));
    {
        if(!sorting_table)
        {
            perror("malloc");
            return EXIT_FAILURE;
        }
    }
    i=0;
    j=2;
    do
    {
        if(is_First(j))
        {
            big_table[i]=j;
            ++i;
        }
        ++j;
    }while(i<MAX);
   
    for(i=0;i<SIZE;i++)
     original_table[i]=big_table[rand()%MAX];
 }
 void go_Up()
 {
     int temp,temp_size;
     temp=sorting_table[temp_value];
     temp_size=temp_value;
     while((temp_size!=1)&&(sorting_table[temp_size/2]<=temp))
     {
         sorting_table[temp_size]=sorting_table[temp_size/2];
         temp_size=temp_size/2;
     }
     sorting_table[temp_size]=temp;
 }

 void go_Down()
 {
     int i,temp_left,temp_change;
     i=1;
     while(1)
     {
         temp_left=(2*i);
         if(temp_left>temp_value)
          break;
         if(temp_left+1<=temp_value)
          if(sorting_table[temp_left]<sorting_table[temp_left+1])
           temp_left++;
         if(sorting_table[i]>=sorting_table[temp_left])
          break;
         temp_change=sorting_table[temp_left];
         sorting_table[temp_left]=sorting_table[i];
         sorting_table[i]=temp_change;
         i=temp_left;
       
     }
 }

 void Insert(int value)
 {
     sorting_table[++temp_value]=value;
     go_Up();
 }
 void free_Tables()
 {
     free(sorting_table);
     free(original_table);
     free(big_table);
 }

 int Cheap()
 {
     int i,temp;
     temp=temp_value-1;
     i=sorting_table[1];
     sorting_table[1]=sorting_table[temp];
     go_Down();
     return i;
 }
int main(int argc, char **argv)
{
    int i,j;
    srand(time(NULL));
    init_Tables();
    printf("Original table:\n");
    for(i=0;i<SIZE;i++)
     printf("%d  ,",original_table[i]);
    for(i=0;i<SIZE;i++)
     Insert(original_table[i]);
    for(i=SIZE;i>0;i--)
     original_table[i-1]=Cheap();
   
    printf("\n\n\n");
    printf("Sorted table:\n");
    for(i=0;i<SIZE;i++)
     printf("%d  ,",original_table[i]);
   
    free_Tables();
    return 0;
}

sobota, 10 marca 2018

Sorting data types with different techniques. (C++ Builder)




FILE - date.h

#include <vcl.h>
#include <string.h>

struct Persons
{
char name[40];
int age;
float income;
};



 bool is_Min (int i,int j) { return (i<j); }


int comp_char(const void *function1,const void *function2)
{
return(strcmp((char*)function1,(char*)function2));
}
void float_Sort(float *table, int size)
{
int i,j;
float temp_float;
for(i=1;i<size;i++)
{
j=i;
temp_float=table[j];
while((j>0) && (table[j-1]>temp_float))
{
table[j]=table[j-1];
j--;
}
table[j]=temp_float;
}
}

The main file:

#include <vcl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include "date.h"

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






void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ListBox1->Items->Clear();
 ListBox2->Items->Clear();
 ListBox3->Items->Clear();
 ListBox4->Items->Clear();
 ListBox5->Items->Clear();
 ListBox6->Items->Clear();
 struct Persons *PP=new struct Persons[10];
 int i,j;
 int *ages=new int[10];
 float *incomes=new float[10];
 char p_date[10][20]={"John Connor","Bob Dylan","Steve Jobs","Michael Jordan","Barack Obama","John Rambo","Ronald Regan",
 "Don Johnson","Mike Tyson","Bruce Lee"};
 for(i=0;i<10;i++)
 {
ages[i]=40+rand()%40;
incomes[i]=1.+rand()%100/1.+rand()%100/1000.;
 }

 for(i=0;i<10;i++)
 {
   strcpy(PP[i].name,p_date[i]);
   PP[i].age=ages[i];
   PP[i].income=incomes[i];
 }
 for(i=0;i<10;i++)
 {
  ListBox1->Items->Add(PP[i].name);
  ListBox2->Items->Add(IntToStr(PP[i].age));
  ListBox3->Items->Add(FloatToStr(PP[i].income)+" mln$");
 }

 float_Sort(incomes,10);
 qsort((void*)p_date,10,sizeof(p_date[0]),comp_char);
 std::vector<int> int_vector(ages,ages+10);
 std::sort (int_vector.begin(), int_vector.end(), is_Min);
 std::vector<int>::iterator temp_iterator;

 for(std::vector<int>::iterator temp_iterator=int_vector.begin();temp_iterator!=int_vector.end();++temp_iterator)
  ListBox5->Items->Add(*temp_iterator);




 for(i=0;i<10;i++)
 {
   strcpy(PP[i].name,p_date[i]);
   PP[i].income=incomes[i];

 }



 for(i=0;i<10;i++)
 {
  ListBox4->Items->Add(PP[i].name);
  ListBox6->Items->Add(FloatToStr(PP[i].income)+" mln$");
 }





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



niedziela, 4 marca 2018

StringGrid and tables to use distance calculations (C++ Builder)

Sometimes you need to create a timetable, or calculate vectors. The tables ares useful. 




private: // User declarations
int **table1,**table2,*value_x,*value_y;
void __fastcall init_Tables();
void __fastcall delete_Tables();
void __fastcall init_Random();


void __fastcall TForm1::init_Tables()
{
int i;
value_x=new int[20];
value_y=new int[20];
table1=new int *[20];
table2=new int *[20];
for(i=0;i<20;i++)
{
table1[i]=new int[20];
table2[i]=new int[20];
}
}
void __fastcall TForm1::delete_Tables()
{
int i;
for(i=0;i<20;i++)
{
delete [] table1[i];
delete [] table2[i];
}
delete [] *table1;
delete [] *table2;
delete value_x;
delete value_y;
}

void __fastcall TForm1::init_Random()
{
  srand(time(NULL));
}



void __fastcall TForm1::Button2Click(TObject *Sender)
{
 delete_Tables();
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Tables();
 init_Random();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int i,j,k;
 value_x[0]=0;
 value_y[0]=0;
 for(i=1;i<20;i++)
 {
value_x[i]=1+rand()%20;
value_y[i]=value_x[i];
 }
 for(i=0;i<20;i++)
 {
StringGrid1->Cells[i][0]=IntToStr(value_x[i]);
StringGrid1->Cells[0][i]=IntToStr(value_y[i]);
StringGrid2->Cells[i][0]=IntToStr(value_x[i]);
StringGrid2->Cells[0][i]=IntToStr(value_y[i]);
table1[i][0]=value_x[i];
table1[0][i]=value_y[i];
table2[i][0]=value_x[i];
table2[0][i]=value_y[i];

 }
 for(i=1;i<20;i++)
 {
for(j=1;j<20;j++)
{
if(i==j)
{
 table1[i][j]=0;
 table2[i][j]=0;
}
else
{
table1[i][j]=table1[i][0]+table1[0][j];
table2[i][j]=table2[i][0]*table2[0][j];
}
}
 }

 for(i=0;i<20;i++)
 {
for(j=0;j<20;j++)
{
StringGrid1->Cells[i][j]=IntToStr(table1[i][j]);
StringGrid2->Cells[i][j]=IntToStr(table2[i][j]);
}
 }

}

sobota, 3 marca 2018

Using malloc-free in a dynamic tables (gcc)


Each number can be shown as the sum of prime numbers.
I used several tables and a primitive algorithm.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 1000
#define TEST 10


void Sort(int *table, int size)
{
    int i,j,tmp;
    for(i=1;i<size;i++)
    {
        j=i;
        tmp=table[j];
        while((j>0) && (table[j-1]>tmp))
        {
            table[j]=table[j-1];
            j--;
        }
        table[j]=tmp;
    }
}
int 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 1;
    else
        return 0;
}
int *table;
int *test_table;
int **result_table;

void init_Table()
{
    int i,j;
    result_table= (int**)malloc(TEST*sizeof(int*));
    for(i=0;i<TEST;i++)
        result_table[i]=(int*)malloc(TEST*sizeof(int));
    test_table=(int*)malloc((TEST+1)*sizeof(int));
    table=(int*)malloc((MAX+1)*sizeof(int));
    i=0;
    j=2;
    do
    {
        if(is_First(j))
        {
            table[i]=j;
            ++i;
        }
        ++j;

    }while(i<MAX);
    for(i=0;i<TEST;i++)
        test_table[i]=TEST*50+rand()%TEST*5;

}
void free_Table()
{

    int i;
    free(table);
    free(test_table);
    for(i=0;i<TEST;i++)
        free(result_table[i]);
    free(result_table);
}
int main()
{
    int i,j,k,sum;
    srand(time(NULL));
    init_Table();
    i=0;
    do
    {
        do
        {
         sum=0;

         for(j=0;j<TEST;j++)
         {
          k=rand()%TEST*5;
          result_table[i][j]=table[k];
          sum+=table[k];
         }
         }while(test_table[i]!=sum);


        ++i;

    }while(i<TEST);

    for(i=0;i<TEST;i++)
    {
        Sort(result_table[i],TEST);
    }

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

        printf("%d  = \n",test_table[i]);
        for(j=0;j<TEST;j++)
        {
            printf("%d,  ",result_table[i][j]);
        }
        printf("\n");
    }

    free_Table();


    getch();
    return 0;
}