quinta-feira, 1 de outubro de 2020

C++ builder - Array e o metodo random_shuffle


Logo no primeiro contato que tivemos
com o array do C++ observamos logo
que o mesmo não é tão diferente do
Array do C, apesar de possuir mais recursos,
e de possuir grande eficiência especialmente
para tamanhos pequenos, como os Arrays do C.
Estes arrays estão entre os vários
contêiner do C++ e possui tamanho fixo
especificado em tempo de execução.
São capazes de suportar iteradores
em acesso aleatórios, e possui alguns
métodos interessantes, que por enquanto
não vamos comentar exceto este que está
sendo usado pelo programa, a saber,
o método std::random_shuffle ( &a [ 0 ], &a [ 100 ] );
o método std::random_shuffle, trabalha organizando
seus elementos em base de troca,
cuidadosamente ele verifica quem vem primeiro
e quem vem depois, baseado no menor e no maior,
vai comparando e organizando aleatoriamente.
Me lembro que no início de nossa formação em C,
passávamos horas criando rotinas para embaralhar
elementos inteiros em Vetores e matrizes bidimensionais,
mas os estudantes de C++ tem o privilégio de usar
este poderoso método em seus programas com muita facilidade.




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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <algorithm>//Para o  random_shuffle
#include <array>
#include <vector>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int x;
bool y = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
: TForm(Owner)
{
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    SetBkColor ( Canvas -> Handle, RGB ( 255, 182, 193 ) );
Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
Canvas -> Font -> Size = 11;
Canvas -> Font -> Name = "Arial";
Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 160, 250, "Por: " );
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 200, 250, "Samuel Lima" );
Canvas -> Font -> Color = clBlue;
Canvas -> TextOut ( 160, 265, "sa_sp10@hotmail.com" );
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
Label1 -> Visible = true;
Label1 -> Font -> Size = 13;
Label1 -> Font -> Name = "Consolas";
Label1 -> Font-> Style = TFontStyles ( ) << fsBold;
Label1 -> Font -> Color = clBlack;
Label1 -> Width = 100;
Label1 -> Height = 70;
Label1 -> Left = 70;
Label1 -> Top = 30 ;

//Cria um botão persomalizado e com evento
//Foi declarado em Unit1.h
BitBtn1 = new TBitBtn ( Form1 );
BitBtn1 -> Parent = Form1;
BitBtn1 -> Font -> Size = 12;
BitBtn1 -> Width = 60;
BitBtn1 -> Height = 25;
BitBtn1 -> Left = 140;
BitBtn1 -> Top = 260;
BitBtn1 -> Font -> Color = clRed;
BitBtn1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
BitBtn1 -> Caption = "shuffle";
BitBtn1 -> OnClick = FormShow;

//Cria mais um botão persomalizado e com evento
//Foi declarado em Unit1.h
BitBtn2 = new TBitBtn ( Form1 );
BitBtn2 -> Parent = Form1;
BitBtn2 -> Font -> Size = 12;
BitBtn2 -> Width = 60;
BitBtn2 -> Height = 25;
BitBtn2 -> Left = 410;
BitBtn2 -> Top = 260;
BitBtn2 -> Font -> Color = clRed;
BitBtn2 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
BitBtn2 -> Caption = "Fechar";
BitBtn2 -> OnClick = Fechar;

Memo1 = new TMemo ( Form1 );
Memo1 -> Parent = Form1;
Memo1 -> Lines -> Clear ( );
Memo1 -> Font -> Name = "Alarm clock";
Memo1 -> Font -> Size = 12;
Memo1 -> Font -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
Memo1 -> Color = static_cast < TColor > ( RGB ( 240, 128, 128 ) );
Memo1 -> Font -> Style = TFontStyles ( ) << fsBold;
Memo1 -> BorderStyle = bsSingle;
Memo1 -> Width = 330;
Memo1 -> Height = 220;
Memo1 -> Enabled = true;
Memo1 -> Left = 140;
Memo1 -> Top = 35;
Memo1 -> ScrollBars = ssVertical;
//Memo1 -> WordWrap = true;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender ) {
//Alterando a cor do form
Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 255, 255, 255 ) );
//Adiciona um fundo nos textos do canvas com RGB

Canvas -> Font -> Size = 14;
Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
Canvas -> Font -> Name = "Arial";
Canvas -> Pen -> Width = 10;
Canvas -> Pen -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
Canvas -> RoundRect ( 05, 05, 595, 295, 25, 25 );
//Colorindo a cor de frente do canvas com RGB
SetTextColor ( Canvas -> Handle, RGB ( 255, 0, 0 ) );
//SetBkColor ( Canvas -> Handle, RGB ( 0, 255, 103 ) );
Canvas -> TextOut ( 135, 10, "C++ - Array e o método random_shuffle" );
   }
//--------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
  Label_Manual ( Sender );

std::array < int, 100 > a; //Array do C++

String str_1;
int i;

String inf_0 ;
String inf_1;
String inf_2;

//Para o array do C ou o std::array do C++
for  ( i = 0; i < 100; i++ ) {
  a [ i ] = i;
}

  x++;

if ( x == 1 ) {
for ( i = 0; i < 30; i++ ) {
inf_0 += " ";
}
Memo1 -> Lines -> Strings [ 0 ] = inf_0;
for ( int val : a ) {
str_1 += " ";
if ( val >= 0 && val < 10 )
str_1 += ( "0" );
str_1 += val;
}
Memo1 -> Text = Memo1 -> Text + AnsiString ( str_1 );
}

if ( x == 2 ) {
    BitBtn1 -> Caption = "Sobre";
//Imita um dos sons do windows
//MessageBeep ( 0 );
//Embaralhando e imprimindo
std::random_shuffle ( &a [ 0 ], &a [ 100 ] );
//std::random_shuffle ( a.begin ( ), a.end ( ) );

for ( int val : a ) {
str_1 += " ";
if ( val >= 0 && val < 10 )
str_1 += ( "0" );
str_1 += val;
}
for ( i = 0; i < 30; i++ ) {
inf_0 += " ";
}
    Memo1 -> Color = static_cast < TColor > ( RGB ( 255, 140, 0 ) );
Memo1 -> Lines -> Strings [ 0 ] = inf_0;//255,140,0
Memo1 -> Text = Memo1 -> Text + AnsiString ( str_1 );
}
if ( x == 3 ) {
    BitBtn1 -> Caption = "Início";
Memo1 -> Font -> Name = "Arial";
Memo1 -> Font -> Size = 12;
Memo1 -> Font -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
Memo1 -> Color = static_cast < TColor > ( RGB ( 0, 128, 128 ) );
Memo1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

for ( i = 0; i < 500; i++ ) {
inf_0 += " ";
}
Memo1 -> Lines -> Strings [ 0 ] = inf_0;

inf_1 = "                  Por: Samuel Lima";
inf_2 = "                  sa_sp10@hotmail.com";

Memo1 -> Lines -> Add ( inf_1 );
Memo1 -> Lines -> Add ( inf_2 );
x = 0;
}
}
//---------------------------------------------------------------------------
 void __fastcall TForm1::Fechar ( TObject *Sender ) {
  Close ( );
 }
//--------------------------------------------------------------------------


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Buttons.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;

void __fastcall Informe ( TObject *Sender );
void __fastcall Label_Manual ( TObject *Sender );
void __fastcall FormPaint ( TObject *Sender );
void __fastcall FormShow ( TObject *Sender );
void __fastcall Fechar ( TObject *Sender );
private: // User declarations
  TBitBtn *BitBtn1;
  TBitBtn *BitBtn2;
   TMemo *Memo1;
public: // User declarations
__fastcall TForm1 ( TComponent* Owner );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



Nenhum comentário:

Postar um comentário

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