?
// ***** wwww.lernprogramme-technik.de *********
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-----------------------------------------------------------------------------
struct Tiere { int Nummer;
char Name[15];
};
struct Tiere Tier[10] ={
{ 2, "Affe" },
{ 5, "Elefant" },
{ 8, "Pferd" },
{ 4, "Aal" },
{ 10, "Hase" },
{ 8, "Llama" },
{ 1, "Hund" },
{ 6, "Katze" },
{ 3, "Zebra" },
{ 7, "Giraffe" } };
//---------------------------------------------------------------------------
int compare_Nummer(void const *Element1, void const *Element2)
{ return ((struct Tiere *)Element1)->Nummer -
((struct Tiere *)Element2)->Nummer;
}
int compare_Namen(const void *Element1, const void *Element2)
{ return( strcmp((char*)((struct Tiere*) Element1)->Name,
(char*)(((struct Tiere*)Element2)->Name)));
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ randomize();
Form1->Caption="Demo: qsort mit struct! www.lernprogramme-technik.de";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Zeige_ButtonClick(TObject *Sender)
{ int n,l;
Zuffalszahlen_ListBox->Clear();
Sortiert_ListBox->Items->Clear();
{ for(n=0; n<=9; n++)
{ Zuffalszahlen_ListBox->Items->Add(IntToStr(Tier[n].Nummer)+
": " + Tier[n].Name);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Sort_ButtonClick(TObject *Sender)
{ int n;
Sortiert_ListBox->Items->Clear();
qsort((void *)&Tier,10,sizeof(struct Tiere),compare_Nummer);
Sortiert_ListBox->Items->Add("Sortiert nach Nummer:");
for(n=0; n<=9; n++)
{ Sortiert_ListBox->Items->Add(IntToStr(Tier[n].Nummer)+
": " + Tier[n].Name);
}
qsort((void &Tier,10,sizeof(struct Tiere),compare_Namen);
Sortiert_ListBox->Items->Add("");
Sortiert_ListBox->Items->Add("Sortiert nach Name:");
for(n=0; n<=9; n++)
{ Sortiert_ListBox->Items->Add(IntToStr(Tier[n].Nummer)+
": " + Tier[n].Name);
}
}
//---------------------------------------------------------------------------