sábado, 2 de fevereiro de 2019

C++ builder - ordenando matriz de string

Declaramos e inicializamos uma matriz
bidimensional de string,
porém a mesma está toda desordenada,
Mas isto não é nenhum problema para
quem conhece a função Qsort () do C,
com apenas um click no botão da
caixa de de mensagem do C++ builder,
é possível ordená-la totalmente.








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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
#include <time.h>
#define tam 10
int Qsort_Ord ( const void *a, const void *b ) {
return ( strcmp ( ( char * ) a, ( char * ) b ) );
}

//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender ) {
char Nomes [ 10 ] [ 16 ] =
   {{"Jaqueline Vega "},
{"Eder Costa     "},
{"Humberto Gomes "},
{"Dijalma Lacerda"},
{"Caroline Silva "},
{"Igor Goncalves "},
{"Bruna Carla    "},
{"Fabio Quadros  "},
{"Ana Celia      "},
{"Geany Barros   "}};
Canvas -> Font -> Size = 16;
Canvas -> Font -> Name = "Arial";
Canvas->Brush->Color = static_cast <TColor> ( RGB ( 0, 191, 255 ) );
Canvas -> Pen -> Width = 10;
Canvas -> Rectangle ( 05, 05, 595, 295 );
SetTextColor ( Canvas->Handle, RGB ( 255, 25, 2 ) );
Canvas -> TextOut ( 60, 10, "C++ BUILDER - ORDENANDO MATRIZ DE STRING" );
int i;
Canvas -> Font -> Size = 12;
Canvas -> Font->Color = clBlack;
Canvas -> TextOut ( 60, 40, "Matriz desordenada" );
for ( i = 0; i < 10 ; i++ ) {
ListBox1 -> Items->Add ( Nomes [ i ] );
}
MessageDlgPos( L"Pressione o botão OK Para ordenar\n"
"A matriz bidimensional de string",
mtInformation, TMsgDlgButtons() << mbOK, 0, 290, 430 );
Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 400, 40, "Matriz ordenada" );
Canvas -> Font -> Color = clRed;
qsort ( ( void * ) Nomes , 10 , sizeof ( Nomes [ 0 ] ) , Qsort_Ord );
for ( i = 0; i < 10 ; i++ ) {
ListBox2 -> Items->Add ( Nomes [ i ] );
}
Canvas -> Font -> Size = 14;
Canvas -> Font -> Style = TFontStyles() << fsBold << fsUnderline << fsItalic ;
Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 240, 260, "Por: Samuel Lima " );
}


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
  TListBox *ListBox1;
  TListBox *ListBox2;
void __fastcall OnPaint(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'C++ BUILDER - ORDENANDO VETORES'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object ListBox1: TListBox
    Left = 24
    Top = 64
    Width = 217
    Height = 193
    Color = clAqua
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsItalic]
    ItemHeight = 19
    ParentFont = False
    TabOrder = 0
  end
  object ListBox2: TListBox
    Left = 352
    Top = 64
    Width = 217
    Height = 193
    Color = clLime
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsItalic]
    ItemHeight = 19
    ParentFont = False
    TabOrder = 1
  end
end

Nenhum comentário:

Postar um comentário

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