Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ builder 6

Status
Not open for further replies.

tammetekkel

Technical User
Mar 10, 2009
1
NL
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::partition( 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
 
In Builder, you have components and each component has some properties and events. When you create a Form, Builder creates a blank GUI form and c++ code and header for you. When you save the project, you will have an option to save the form, code, and project to a name of your choice.

It looks like after the person created the form, they dragged and dropped the following components: and input, a button, and an edit box.

On the button, the programmer using the click event. for the input and edit, the programmer is using the change and keydown events. If the user is using Builder's properties to create these, the header is automatically undated as is the source. The programmer can then put in his/her code in the proper event.

For the other functions, the programmer has created his/her own functions. In these cases, the programmer must edit both the source code and the header file manually. By default, the source code is shown on screen although it is hidden behind the form. Pressing F12 will cycle you through the different builder screens until you get to the editor.

On the editor, there will be tabs at the bottom of it. One for the source code and one for the header. Since this a class, you should put your created functions on either the private or public section.

Look at the FAQ for books and web sites for how to use builder.


James P. Cottingham
I'm number 1,229!
I'm number 1,229!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top