tammetekkel
Technical User
hello
a coleage of me give me a builder code. but i do not know
how to inplemented in builder can someone help me with it?
And how i make a header file with this ?
with buttons en boxes i need to instal ?
#pragma hdrstop
#include "sort.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Input1Change(TObject *Sender)
{
Input1->MaxLength = 20;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ReadUserData();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReadUserData()
{
//pre:
//post:OutputData(int& len, int* data) is called if input OK, else: idle
int len = Input1->GetTextLen();
len++; //'\0'
pBuf = new char[len];
if ( pBuf == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
Input1->GetTextBuf(pBuf, len);
if ( len <= 1 )
{
ShowMessage("Enter at least one character,please.");
Input1->SetFocus();
return;
}
FilterData(len, pBuf);
if ( len <= 1 )
{
ShowMessage("There is no low case letter typed.");
Input1->SetFocus();
return;
}
if ( !ConcatenateExtraLetter(len, pBuf) )
{
EditExtraLetter->SetFocus();
return;
}
len++; //for pBufPlus extra letter
int size_array = (len-1);
array = new int[size_array];
if ( array == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
for (int i=0;i<size_array;i++)
{
array = *pBufPlus;
pBufPlus++;
}
Quicksort(0,size_array-1,array);
OutputData( len, array );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FilterData(int& len, char* data)
{
//pre:contains "len" and "data" data
//post:"len" and "data" data changed if input not OK (not low case letters)
for (int i=0;i<len-1;i++)
{
int a = int(data);
if ( a < 97 || a > 122 )
{
for (int j=i;j<len-1;j++)
{
data[j] = data[j+1];
}
len--;
if ( i <= (len-2) )
{
i--;
}
}
}
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::ConcatenateExtraLetter(int len, char* Data)
{
//pre:contains "len" and "Data" data
//post:false is returned,if extra letter notOK,else:ExtraLetter is concatenated
// with Data and true returned
ExtraLetter = new char;
if ( ExtraLetter == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
int LenLetter = EditExtraLetter->GetTextLen();
LenLetter++; //'\0'
EditExtraLetter->GetTextBuf(ExtraLetter, LenLetter);
FilterData( LenLetter, ExtraLetter );
if ( LenLetter == 1 )
{
ShowMessage("The extra character has to be low case letter!");
return false;
}
len++;
pBufPlus = new char[len];
if ( pBufPlus == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
strcpy(pBufPlus,Data);
pBufPlus[len-2] = ExtraLetter[0];
return true;
}
//---------------------------------------------------------------------------
int __fastcall TForm1:artition( int left,int right,int arr[] )
{
//pre: contains left, right, arr[]
//post: elements of arr[] are swapped and/if rm returned
int lm, rm, pivot, temp;
pivot = arr
a coleage of me give me a builder code. but i do not know
how to inplemented in builder can someone help me with it?
And how i make a header file with this ?
with buttons en boxes i need to instal ?
#pragma hdrstop
#include "sort.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Input1Change(TObject *Sender)
{
Input1->MaxLength = 20;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ReadUserData();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReadUserData()
{
//pre:
//post:OutputData(int& len, int* data) is called if input OK, else: idle
int len = Input1->GetTextLen();
len++; //'\0'
pBuf = new char[len];
if ( pBuf == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
Input1->GetTextBuf(pBuf, len);
if ( len <= 1 )
{
ShowMessage("Enter at least one character,please.");
Input1->SetFocus();
return;
}
FilterData(len, pBuf);
if ( len <= 1 )
{
ShowMessage("There is no low case letter typed.");
Input1->SetFocus();
return;
}
if ( !ConcatenateExtraLetter(len, pBuf) )
{
EditExtraLetter->SetFocus();
return;
}
len++; //for pBufPlus extra letter
int size_array = (len-1);
array = new int[size_array];
if ( array == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
for (int i=0;i<size_array;i++)
{
array = *pBufPlus;
pBufPlus++;
}
Quicksort(0,size_array-1,array);
OutputData( len, array );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FilterData(int& len, char* data)
{
//pre:contains "len" and "data" data
//post:"len" and "data" data changed if input not OK (not low case letters)
for (int i=0;i<len-1;i++)
{
int a = int(data);
if ( a < 97 || a > 122 )
{
for (int j=i;j<len-1;j++)
{
data[j] = data[j+1];
}
len--;
if ( i <= (len-2) )
{
i--;
}
}
}
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::ConcatenateExtraLetter(int len, char* Data)
{
//pre:contains "len" and "Data" data
//post:false is returned,if extra letter notOK,else:ExtraLetter is concatenated
// with Data and true returned
ExtraLetter = new char;
if ( ExtraLetter == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
int LenLetter = EditExtraLetter->GetTextLen();
LenLetter++; //'\0'
EditExtraLetter->GetTextBuf(ExtraLetter, LenLetter);
FilterData( LenLetter, ExtraLetter );
if ( LenLetter == 1 )
{
ShowMessage("The extra character has to be low case letter!");
return false;
}
len++;
pBufPlus = new char[len];
if ( pBufPlus == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
strcpy(pBufPlus,Data);
pBufPlus[len-2] = ExtraLetter[0];
return true;
}
//---------------------------------------------------------------------------
int __fastcall TForm1:artition( int left,int right,int arr[] )
{
//pre: contains left, right, arr[]
//post: elements of arr[] are swapped and/if rm returned
int lm, rm, pivot, temp;
pivot = arr
;
lm = left-1;
rm = right+1;
for (;
{
do
rm--;
while (arr[rm] > pivot);
do
lm++;
while( arr[lm] < pivot);
if( lm < rm )
{
swap(arr[lm],arr[rm]);
}
else return rm;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Quicksort( int left, int right, int arr[] )
{
//pre: contains left, right, arr[]
//post: elements of arr[] are swapped if they had to be sorted
if(left < right)
{
int NewPivot = Partition(left, right, arr);
Quicksort( left, NewPivot, arr );
Quicksort( NewPivot+1, right, arr );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::swap( int& left, int& right)
{
//pre: contains left, right
//post: left and right values are swapped
int temp;
temp = left;
left = right;
right = temp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OutputData(int& len, int* data)
{
//pre: contains len, data
//post: Edit fields are changed
char* text = new char[len];
if ( text == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
int LetterNumber;
for (int i=0;i<len-1;i++)
{
text = char(data);
}
text[len-1] = '\0';
Output1->Text = text;
LetterNumber = GetExtraLetterNumber( len, text );
EditExtraLetterNr->Text = LetterNumber;
Input1->SetFocus();
}
//---------------------------------------------------------------------------
int __fastcall TForm1::GetExtraLetterNumber(int len, char * data)
{
//pre: contains len, data
//post: number is returned
int number;
for (number=0;number<len;number++)
{
if ( *data == ExtraLetter[0] )
{
break;
}
data++;
}
number++;
return number;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Input1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if ( Key == 13 )
{
Button1->Click();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditExtraLetterChange(TObject *Sender)
{
EditExtraLetter->MaxLength = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditExtraLetterKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if ( Key == 13 )
{
Button1->Click();
}
}
//----------------------------------------------------------
thank you.
GR T&T
lm = left-1;
rm = right+1;
for (;
{
do
rm--;
while (arr[rm] > pivot);
do
lm++;
while( arr[lm] < pivot);
if( lm < rm )
{
swap(arr[lm],arr[rm]);
}
else return rm;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Quicksort( int left, int right, int arr[] )
{
//pre: contains left, right, arr[]
//post: elements of arr[] are swapped if they had to be sorted
if(left < right)
{
int NewPivot = Partition(left, right, arr);
Quicksort( left, NewPivot, arr );
Quicksort( NewPivot+1, right, arr );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::swap( int& left, int& right)
{
//pre: contains left, right
//post: left and right values are swapped
int temp;
temp = left;
left = right;
right = temp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OutputData(int& len, int* data)
{
//pre: contains len, data
//post: Edit fields are changed
char* text = new char[len];
if ( text == 0 )
{
ShowMessage("Error: memory could not be allocated");
exit(0);
}
int LetterNumber;
for (int i=0;i<len-1;i++)
{
text = char(data);
}
text[len-1] = '\0';
Output1->Text = text;
LetterNumber = GetExtraLetterNumber( len, text );
EditExtraLetterNr->Text = LetterNumber;
Input1->SetFocus();
}
//---------------------------------------------------------------------------
int __fastcall TForm1::GetExtraLetterNumber(int len, char * data)
{
//pre: contains len, data
//post: number is returned
int number;
for (number=0;number<len;number++)
{
if ( *data == ExtraLetter[0] )
{
break;
}
data++;
}
number++;
return number;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Input1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if ( Key == 13 )
{
Button1->Click();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditExtraLetterChange(TObject *Sender)
{
EditExtraLetter->MaxLength = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditExtraLetterKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if ( Key == 13 )
{
Button1->Click();
}
}
//----------------------------------------------------------
thank you.
GR T&T