domingo, 3 de fevereiro de 2019

FMX - ordenando um vetor de struct

Aqui está um exemplo de como ordenar
um vetor de struct usando a função
qsort(); do C.
A parte lógica deste código pode ser
aproveitada po qualquer iniciante
de linguagem C, mas quem já conhece
o Embarcadero Rad Studio pode aproveitar
todo o código, para estudo.






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

#include <fmx.h>
#pragma hdrstop

#include "OnShow.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
#define tam 20
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner ) :
  TForm ( Owner ) {
}

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

typedef int ( *comp_unid ) ( const void* , const void* );
struct Nomes {
     int num;
char nomes [ 16 ];
};
struct Nomes vet_str [ 10 ] ={{10, "Jaqueline Vega" },
  { 5, "Eder Costa    " },
  { 8, "Humberto Gomes" },
  { 4, "Dijalma Lacerda"},
  { 3, "Caroline Silva" },
  { 9, "Igor Goncalves" },
  { 2, "Bruna Carla   " },
  { 6, "Fabio Quadros " },
  { 1, "Ana Celia     " },
  { 7, "Geany Barros " }};
int Qsort_ord ( struct Nomes *comp , struct Nomes *co_mp ) {
if ( comp -> num < co_mp -> num )
return -1;
else if ( comp -> num > co_mp -> num )
return 1;
else return 0;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::OnShow ( TObject *Sender ) {
Text1 -> Text = "C++ BUILDER - ORDENANDO UM VETOR DE STRUCT";
String str_1 = "";
String str_2 = "";
int i;
for ( i = 0; i < 10 ; i++ ) {
if ( i % 1 == 0 )
str_1 += "\n";
str_1 += vet_str [ i ].num;
str_1 += "   ";
str_1 += vet_str [ i ].nomes;
}
 Memo1 -> Text = Memo1 -> Text + AnsiString ( str_1 );
 Memo1 -> SelStart = 0;
 ShowMessage ( "Pressione o botão OK para\n ordenar o vetor de struct" );

 /*===========================================================================*/
  qsort ( ( void * ) &vet_str, 10, sizeof ( struct Nomes ), ( comp_unid ) Qsort_ord );
for ( i = 0; i < 10 ; i++ ) {
if ( i % 1 == 0 )
str_2 += "\n";
str_2 += vet_str [ i ].num;
str_2 += "   ";
str_2 += vet_str [ i ].nomes;
}
 Memo2 -> Text = Memo2 -> Text + AnsiString ( str_2 );
 Memo2 -> SelStart = 0;
Text2 -> Text = "Por: Samuel Lima";
}


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

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

#ifndef OnShowH
#define OnShowH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Objects.hpp>
#include <FMX.Types.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.Memo.hpp>
#include <FMX.ScrollBox.hpp>
#include <FMX.StdCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
 TMemo *Memo1;
TMemo *Memo2;
TText *Text1;
TText *Text2;
TRectangle *Rectangle1;
TRectangle *Rectangle2;
TRectangle *Rectangle3;
 void __fastcall OnShow(TObject *Sender);


private: // User declarations
public:  // User declarations
 __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------


#endif


object Form1: TForm1
  Left = 341
  Top = 42
  Caption = 'C++ BUILDER - ORDENANDO UM VETOR DE STRUCT'
  ClientHeight = 300
  ClientWidth = 600
  Position = Designed
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnShow = OnShow
  Left = 341
  Top = 42
  DesignerMasterStyle = 0
  object Memo1: TMemo
    Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
    Caret.Color = claTurquoise
    DataDetectorTypes = []
    StyledSettings = [Family, FontColor]
    TextSettings.Font.Size = 14.000000000000000000
    TextSettings.Font.StyleExt = {00070000000200000004000000}
    TextSettings.WordWrap = True
    Position.X = 48.000000000000000000
    Position.Y = 40.000000000000000000
    Size.Width = 193.000000000000000000
    Size.Height = 225.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 3
    Viewport.Width = 189.000000000000000000
    Viewport.Height = 221.000000000000000000
  end
  object Memo2: TMemo
    Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
    DataDetectorTypes = []
    StyledSettings = [Family]
    TextSettings.Font.Size = 14.000000000000000000
    TextSettings.Font.StyleExt = {00070000000200000004000000}
    TextSettings.FontColor = claDarkblue
    TextSettings.WordWrap = True
    Position.X = 360.000000000000000000
    Position.Y = 40.000000000000000000
    Size.Width = 193.000000000000000000
    Size.Height = 225.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 2
    Viewport.Width = 189.000000000000000000
    Viewport.Height = 221.000000000000000000
  end
  object Text1: TText
    Position.X = 56.000000000000000000
    Position.Y = 8.000000000000000000
    Size.Width = 497.000000000000000000
    Size.Height = 25.000000000000000000
    Size.PlatformDefault = False
    TextSettings.Font.Size = 20.000000000000000000
    TextSettings.Font.StyleExt = {00040000000200000004000000}
    TextSettings.FontColor = claRed
  end
  object Text2: TText
    Position.X = 136.000000000000000000
    Position.Y = 264.000000000000000000
    Size.Width = 313.000000000000000000
    Size.Height = 25.000000000000000000
    Size.PlatformDefault = False
    TextSettings.Font.Size = 22.000000000000000000
    TextSettings.Font.StyleExt = {00040000000200000004000000}
    TextSettings.FontColor = claBlue
  end
  object Rectangle1: TRectangle
    Fill.Color = claNull
    Size.Width = 600.000000000000000000
    Size.Height = 300.000000000000000000
    Size.PlatformDefault = False
    Stroke.Color = claBlue
    Stroke.Thickness = 10.000000000000000000
  end
  object Rectangle2: TRectangle
    Fill.Color = claNull
    Position.X = 352.000000000000000000
    Position.Y = 40.000000000000000000
    Size.Width = 201.000000000000000000
    Size.Height = 225.000000000000000000
    Size.PlatformDefault = False
    Stroke.Color = claBlueviolet
    Stroke.Thickness = 5.000000000000000000
  end
  object Rectangle3: TRectangle
    Fill.Color = claNull
    Position.X = 40.000000000000000000
    Position.Y = 40.000000000000000000
    Size.Width = 193.000000000000000000
    Size.Height = 225.000000000000000000
    Size.PlatformDefault = False
    Stroke.Color = claDarkviolet
    Stroke.Thickness = 5.000000000000000000
  end
end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.