sobota, 2 listopada 2024
Retrieving data from a file into C++ Builder components.
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
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;
}