sobota, 2 listopada 2024

Retrieving data from a file into C++ Builder components.



File: top_Population.txt (location in directory Project C++ Builder)

India 1450935791 17.78
Pakistan 251269164 3.08
China 1419321278 17.39
United_States 345426571 4.23
Nigeria 232679478 2.85
Indonesia 283487931 3.47





struct Date
{
char name_Country[64];
float population_Country;
float procent_World;
};

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 StringGrid1->Cells[0][0]="Country";
 StringGrid1->Cells[1][0]="Population";
 StringGrid1->Cells[2][0]="World Share";
 Button1->Caption="Open";
 Button2->Caption="Close";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 struct Date* population_Info=new struct Date[6];
 FILE* my_File=fopen("top_Population.txt","r");
 if(!my_File)
 {
ShowMessage("Can't open file!");
 }
 else
 {
  for(int i=0;i<6;i++)
  {
fscanf(my_File,"%s %f %f\n",population_Info[i].name_Country,
&population_Info[i].population_Country,&population_Info[i].procent_World);
  }
  fclose(my_File);
  for(int i=0;i<6;i++)
  {
StringGrid1->Cells[0][i+1]=UnicodeString(population_Info[i].name_Country);
StringGrid1->Cells[1][i+1]=FloatToStr(population_Info[i].population_Country);
StringGrid1->Cells[2][i+1]=FloatToStrF(population_Info[i].procent_World,ffGeneral,7,1)+"%";
  }

 }

 delete [] population_Info;
}


 

wtorek, 22 października 2024

Ascending and descending sorting - Example - class C++ Builder

 










New Unit:


File Class_Sorting.h


class multi_Sort

{

 private:

  int* tab_Date;

  int* min_Tab;

  int* max_Tab;

  int length;


 public:


multi_Sort(int size);

~multi_Sort();

void min_Sort(int *tab, int s);

void max_Sort(int* tab, int s);

void Load(int *tab);

int *min_Result();

     int *max_Result();

};

File Class_Sorting.cpp

#include "class_Sorting.h"

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

#pragma package(smart_init)

multi_Sort::multi_Sort(int size)

{

length=size;

tab_Date=new int[length];

min_Tab=new int[length];

max_Tab=new int[length];

}


multi_Sort::~multi_Sort()

{

delete max_Tab;

delete min_Tab;

delete tab_Date;

}


void multi_Sort::min_Sort(int *tab, int s)

{

int i,j,k;

for(i=1;i<s;i++)

{

j=i;

k=tab[j];

while((j>0) && (tab[j-1]>k))

{

tab[j]=tab[j-1];

j--;

}

tab[j]=k;

}

}


void multi_Sort::max_Sort(int* tab, int s)

{

int i,j,k;

for(i=1;i<s;i++)

{

j=i;

k=tab[j];

while((j>0) && (tab[j-1]<k))

{

tab[j]=tab[j-1];

j--;

}

tab[j]=k;

}

}

void multi_Sort::Load(int *tab)

{

for(int i=0;i<length;i++)

{

tab_Date[i]=min_Tab[i]=max_Tab[i]=tab[i];

}


}

int* multi_Sort::min_Result()

{

  min_Sort(min_Tab,length);

  return min_Tab;

}

int* multi_Sort::max_Result()

{

  max_Sort(max_Tab,length);

  return max_Tab;

}

Main Project:


#include <vcl.h>

#include <stdlib.h>

#include <time.h>

#pragma hdrstop


#include "p_Example.h"

#include "class_Sorting.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

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

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

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

void __fastcall TForm1::FormCreate(TObject *Sender)

{

 srand(time(NULL));

 Button1->Caption="&RUN";

}

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

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  #define ssize 10

  #define mmax 100

  int j;

  int* main_Date=new int[ssize];

  int* min_Date=new int[ssize];

  int* max_Date=new int[ssize];

  for(int i=0;i<ssize;i++)

  {


  j=1+rand()%mmax;

  main_Date[i]=j;

  }

  multi_Sort my_Class(ssize);

  my_Class.Load(main_Date);

  min_Date=my_Class.min_Result();

  max_Date=my_Class.max_Result();

  for(int i=0;i<ssize;i++)

  {

  StringGrid1->Cells[i][0]=IntToStr(main_Date[i]);

  StringGrid1->Cells[i][1]=IntToStr(min_Date[i]);

      StringGrid1->Cells[i][2]=IntToStr(max_Date[i]);

  }

  delete max_Date;

  delete min_Date;

  delete main_Date;

}



wtorek, 27 sierpnia 2024

Scalar product - C++ Builder

 




__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
 srand(time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

 int i,j,k,width,height;
 width=5; height=2;
 float tab[width][height],r,d=10.0,scalar_Product;
 for(i=0;i<width;i++)
 {
for(j=0;j<height;j++)
{
k=1+rand()%100;
r=(1.00*k)/d;
tab[i][j]=r;
StringGrid1->Cells[i][j]=FloatToStrF(tab[i][j],ffGeneral,4,4);
}
 }

 std::valarray<float> x {tab[0][0],tab[0][1], tab[0][2], tab[0][3], tab[0][4]};
 std::valarray<float> y {tab[1][0],tab[1][1], tab[1][2], tab[1][3], tab[1][4]};
 scalar_Product=(x*y).sum();
 Label1->Caption="SCALAR PRODUCT = "+FloatToStr(scalar_Product);


}


czwartek, 22 sierpnia 2024

Negation and bit shift - C++ Builder


 

void __fastcall TForm1::write_Bits(char x, TListBox *Tl)

{

 unsigned char date[11]={1,2,4,8,16,32,64,128,256,512,1024};

 UnicodeString result;

 int i,y;

 result="";

 for(i=10;i>=0;i--)

 {

  y=(date[i] & x);

  if(y!=0)

   result+="1";

  else

   result+="0";

 }

 Tl->Items->Add(result);


}

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

void __fastcall TForm1::Button2Click(TObject *Sender)

{

 Close();

}

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

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 int a1,a2,a3,i;

 for(i=0;i<128;i++)

 {

a1=i;

a2=~a1;

a3=a1<<1;

write_Bits(a1,ListBox1);

write_Bits(a2,ListBox2);

write_Bits(a3,ListBox3);

 }

}

czwartek, 4 lipca 2024

Draw simulation based on the example of a lottery - C (gcc)

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

#define yy 6
#define xx 1000
#define zz 49
int *main_Lotto;
int **date_Lotto;
int *Temp;
int **double_Temp;


void Sort(int* tab, int w)
{
    int i,j,k;
    for(i=1;i<w;i++)
    {
        j=i;
        k=tab[j];
        while((j>0) && (tab[j-1]>k))
        {
            tab[j]=tab[j-1];
            j--;
        }
        tab[j]=k;
    }
}
int is_Zero(int *tab1,int *tab2, int w)
{
    int i,sum;
    sum=0;
    for(i=0;i<w;i++)
    {
        if(tab1[i]==tab2[i])
        ++sum;
    }
    if(sum==0)
     return 1;
    else
     return 0;
}
int is_One(int *tab1,int *tab2, int w)
{
    int i,sum;
    sum=0;
    for(i=0;i<w;i++)
    {
        if(tab1[i]==tab2[i])
        ++sum;
    }
    if(sum==1)
     return 1;
    else
     return 0;
}
int is_Two(int *tab1,int *tab2, int w)
{
    int i,sum;
    sum=0;
    for(i=0;i<w;i++)
    {
        if(tab1[i]==tab2[i])
        ++sum;
    }
    if(sum==2)
     return 1;
    else
     return 0;
}

int is_Three(int *tab1,int *tab2, int w)
{
    int i,sum;
    sum=0;
    for(i=0;i<w;i++)
    {
        if(tab1[i]==tab2[i])
        ++sum;
    }
    if(sum==3)
     return 1;
    else
     return 0;
}
int is_Four(int *tab1,int *tab2, int w)
{
    int i,sum;
    sum=0;
    for(i=0;i<w;i++)
    {
        if(tab1[i]==tab2[i])
        ++sum;
    }
    if(sum==4)
     return 1;
    else
     return 0;
}
int is_Five(int *tab1,int *tab2, int w)
{
    int i,sum;
    sum=0;
    for(i=0;i<w;i++)
    {
        if(tab1[i]==tab2[i])
        ++sum;
    }
    if(sum==5)
     return 1;
    else
     return 0;
}
int is_Six(int *tab1,int *tab2, int w)
{
    int i,sum;
    sum=0;
    for(i=0;i<w;i++)
    {
        if(tab1[i]==tab2[i])
        ++sum;
    }
    if(sum==6)
     return 1;
    else
     return 0;
}



int main(int argc, char **argv)
{
    int i,j,k;
    int x0,x1,x2,x3,x4,x5,x6;
    srand(time(NULL));
    x0=x1=x2=x3=x4=x5=x6=0;
    main_Lotto=malloc(yy*sizeof(int));
    Temp=malloc(zz*sizeof(int));
    date_Lotto=malloc(xx*sizeof(int*));
    double_Temp=malloc(xx*sizeof(int*));
    for(i=0;i<xx;i++)
    {
        date_Lotto[i]=malloc(yy*sizeof(int));
        double_Temp[i]=malloc(zz*sizeof(int));
    
    }
    for(i=0;i<zz;i++)
     Temp[i]=1;
    for(i=0;i<xx;i++)
     for(j=0;j<zz;j++)
      double_Temp[i][j]=1;
    i=0;
    do
    {
        do
        {
            j=rand()%zz;
        }while(Temp[j]!=1);
        Temp[j]=0;
        main_Lotto[i]=j+1;
        ++i;
    }while(i<yy);
    Sort(main_Lotto,yy);
    for(i=0;i<xx;i++)
    {
        j=0;
        do
        {
            do
            {
                k=rand()%zz;
            }while(double_Temp[i][k]!=1);
            double_Temp[i][k]=0;
            date_Lotto[i][j]=k+1;
            ++j;
        }while(j<yy);
    }
    for(i=0;i<xx;i++)
    Sort(date_Lotto[i],yy);
    for(i=0;i<xx;i++)
    {
        if(is_Zero(date_Lotto[i],main_Lotto,yy))
         ++x0;
        if(is_One(date_Lotto[i],main_Lotto,yy))
        ++x1;
        if(is_Two(date_Lotto[i],main_Lotto,yy))
        ++x2;
        if(is_Three(date_Lotto[i],main_Lotto,yy))
        ++x3;
        if(is_Four(date_Lotto[i],main_Lotto,yy))
        ++x4;
        if(is_Five(date_Lotto[i],main_Lotto,yy))
        ++x5;
        if(is_Six(date_Lotto[i],main_Lotto,yy))
        ++x6;
    }
    
    printf("Random coupon: (6/49) : \n\n");
    for(i=0;i<yy;i++)
    {
        printf("%d  ",main_Lotto[i]);
    }
    printf("\n\n\n");
    printf("After simulating a thausan draws. Results : \n\n");
    printf("0: %d\n",x0);
    printf("1: %d\n",x1);
    printf("2: %d\n",x2);
    printf("3:  %d\n",x3);
    printf("4:  %d\n",x4);
    printf("5:  %d\n",x5);
    printf("6:  %d\n",x6);
    
    free(main_Lotto);
    free(Temp);
    for(i=0;i<xx;i++)
    {
        free(date_Lotto[i]);
        free(double_Temp[i]);
    }
    free(date_Lotto);
    free(double_Temp);
    
    return 0;
}

niedziela, 24 marca 2024

Calculator - C++ Builder


class Calculator:

File - calc_unit.h 

#ifndef calc_unitH
#define calc_unitH
//---------------------------------------------------------------------------


class Calculator
{
private:
float x1,x2,result;
public:
Calculator();
void Read(float a, float b);
float Plus();
float Minus();
float Multiplication();
float Division();
float SQRT();
float Inverse();
float EXP();
};
#endif

File - calc_unit.cpp

#pragma hdrstop

#include "calc_unit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

Calculator::Calculator()
{
;
}
void Calculator::Read(float a, float b)
{
x1=a;
x2=b;
}
float Calculator::Plus()
{
result=x1+x2;
return result;
}

float Calculator::Minus()
{
result=x1-x2;
return result;
}
float Calculator::Multiplication()
{
result=x1*x2;
return result;
}
float Calculator::Division()
{
   result=x1/x2;
   return result;
}
float Calculator::SQRT()
{
result=sqrt(x1);
return result;
}
float Calculator::Inverse()
{
result=(1.00/x1);
return result;
}
float Calculator::EXP()
{
  int i,temp=int(x2);
  result=x1;
  for(i=0;i<temp-1;i++)
   result*=x1;
  return result;
}

Project C++ Builder:

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "p_Proj_Calc.h"
#include "calc_unit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Calculator cl;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Plus());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
  Edit3->Text=FloatToStr(cl.Minus());

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Multiplication());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Division());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Inverse());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.SQRT());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.EXP());
}






 

niedziela, 5 listopada 2023

std::tuple C++ Builder


void __fastcall TForm1::info_File(UnicodeString name, int size, UnicodeString date_Modification)
{
Edit1->Text=name;
Edit2->Text=IntToStr(size);
Edit3->Text=date_Modification;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
using INFO=tuple<UnicodeString,int,UnicodeString>;
TSearchRec info_Sys;
AnsiString date,name_File;
int bytes;
 name_File=Application->ExeName;
 if(FindFirst(name_File,faAnyFile,info_Sys)!=0)
 {
ShowMessage("I can't open file");
Close();
 }
 bytes=info_Sys.Size;
 date=DateTimeToStr(FileDateToDateTime(info_Sys.Time));
 INFO application_exe={name_File,bytes,date};
 const auto &[name,size,date_Modification]=application_exe;
 info_File(name,size,date_Modification);
}