segunda-feira, 18 de novembro de 2019

C++ builder - Pesquisando string em matriz III

Este programa permite uma eficiente pesquisa
nos dados de uma matriz de string.
O programa destaca todas as linhas onde se
encontram a palavra pesquisada numa cor
diferente das demais linhas da matriz,
informando ainda a quantidade de ocorrências
da palavra encontrada dentro da matriz.
Quer saber como fiz isto, me siga no meu perfil,
e adicione o link do blog em seus favoritos.





Veja abaixo os códigos do programa:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
 //---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 280, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 280, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 300, "sa_sp10@hotmail.com" );
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
    char matriz [ 64 ] [ 45 ] =  {
                    "                                            ",
                    "Debaixo dumas mui formosas tamareiras,      ",
                    "Estando já Berseba na escuridão.            ",
                    "As aves escutando, entre as roseiras,       ",
                    "Se vê andar o patriarca Abraão.             ",
                    "Seu coração perante Deus está aflito,       ",
                    "Pois quer que O sirvamos sem murmuração;    ",
                    "E por amor pergunta ao Senhor bendito:      ",
                    "O meu amado filho queres Tu, então?         ",
                    "                                            ",
                    "A voz de Jeová potente é ouvida:            ",
                    "O teu Isaque oferece para Mim,              ",
                    "Embora fiques com tu'alma dolorida,         ",
                    "Pois te abençoo se fizeres tu assim.        ",
                    "De abatido Abraão se torna forte            ",
                    "E Canta hinos, pois com fé medita já:       ",
                    "Deus pode o meu filho libertar da morte!    ",
                    "E não temendo, segue para Moriá.            ",
                    "                                            ",
                    "Ao pé do monte do supremo sacrifício,       ",
                    "Profunda duvida entrou em Abraão:           ",
                    "Irei perder da minha vida o beneficio?      ",
                    "E triste começou subir com lentidão,        ",
                    "Pois ia dar, do coração a esperança         ",
                    "- No seu outono, sacrifício duma flor,      ",
                    "Assim levou o seu cordeiro à matança,       ",
                    "Em obediência ao mandato do Senhor.         ",
                    "                                            ",
                    "Isaque com a lenha, presto vai na frente,   ",
                    "Oh! Quanto é formoso para Abraão!           ",
                    "Mas eis que volta p'ra seu pai suavemente   ",
                    "E lhe dirige esta interrogação:             ",
                    "O fogo e a lenha estou vendo que trouxemos, ",
                    "Mas o cordeiro d'holocausto onde está?      ",
                    "E a resposta de Abraão na Bíblia temos:     ",
                    "Meu filho, Deus pra Si, Cordeiro proverá    ",
                    "                                            ",
                    "Chegando Abraão aonde Deus mandara,         ",
                    "Fez um altar e nele a lenha arrumou:        ",
                    "E a seu filho, que já dantes amarrara,      ",
                    "Tomando nos seus braços sobre o altar deitou",
                    "Mas quando Abraão foi para imolá-lo,        ",
                    "O Anjo do Senhor bradou-lhe desde os céus:  ",
                    "A tua mão, ó não estendas p'ra matá-lo;     ",
                    "Porquanto eu agora sei que temes Deus.      ",
                    "                                            ",
                    "Erguendo Abraão seus olhos de repente,      ",
                    "Vê um cordeiro, que no mato preso está,     ",
                    "E o tomando, oferece-o alegremente;         ",
                    "Assim No monte do Senhor se proverá.        ",
                    "A voz do Anjo é ouvida novamente;           ",
                    "Diz o Senhor: Porque fizeste esta ação,     ",
                    "Deveras, Eu abençoarei a tua semente,       ",
                    "E nela, as nações benditas se farão.        ",
                    "                                            ",
                    "O nosso Isaque oferecemos com firmeza       ",
                    "No Moriá onde finda o ideal,                ",
                    "Pois foi ali que alcançaram fortaleza,      ",
                    "Os vencedores, sob canto angelical;         ",
                    "Ali, o nosso Deus jurou fidelidade,         ",
                    "Também os santos se encheram de valor,      ",
                    "E só teremos a perfeita santidade,          ",
                    "Depois que formos para o monte do Senhor.   ",
                    "                                            "};
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {

     Edit1 -> SetFocus ( );
     Edit1 -> Font -> Size = 10;
     Edit1 -> Width = 90;
     Edit1 -> Height = 20;
     Edit1 -> Left = 260;
     Edit1 -> Top = 40;

    Label1 -> Font -> Size = 12;
    Label1 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label1 -> Font -> Color = clBlack;
    Label1 -> Width = 130;
    Label1 -> Height = 20;
    Label1 -> Left = 40;
    Label1 -> Top = -15;

    Label2 -> Font -> Size = 12;
    //Label2 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label2 -> Font -> Color = clBlue;
    Label2 -> Width = 130;
    Label2 -> Height = 20;
    Label2 -> Left = 140;
    Label2 -> Top = 240;

    Label4 -> Font -> Size = 12;
    Label4 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label4 -> Font -> Color = clRed;
    Label4 -> Width = 130;
    Label4 -> Height = 20;
    Label4 -> Left = 40;
    Label4 -> Top = -15;

    BitBtn1 -> Font -> Size = 12;
    BitBtn1 -> Font -> Color = clBlue;
    BitBtn1    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn1 -> Top = 40;
    BitBtn1 -> Left = 370;
    BitBtn1 -> Height = 23;
    BitBtn1 -> Width = 30;

    ScrollBox1 -> Font -> Color = clRed;
    ScrollBox1 -> HorzScrollBar -> Tracking = True;
    ScrollBox1 -> VertScrollBar -> Tracking = True;
    ScrollBox1 -> Width = 400;
    ScrollBox1 -> Height = 150;
    ScrollBox1 -> Left = 100;
    ScrollBox1 -> Top = 68;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cria_Matriz ( TObject *Sender ) {
    int i;
 for ( i = 0; i < 64; i++ ) {
  str_1 += "\n";
  str_3 += "\n";
  str_1 += matriz [ i ];
 }
  Label_Manual ( Sender );
  Label1 -> Caption = str_1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
 Canvas -> Font -> Size = 14;
 Canvas -> Font -> Name = "Arial";
 Canvas -> Pen -> Width = 10;
 Canvas -> Pen -> Color = clBlue;
 Canvas -> Rectangle ( 05, 05, 595, 335 );
 SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
 Canvas -> TextOut ( 130, 10, "PESQUISANDO STRING EM MATRIZ III" );
 SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
 Canvas -> TextOut ( 180, 40, "Palavra: " );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
 cria_Matriz ( Sender );
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
      str_2 = " ";
      str_2 += Edit1 -> Text;
    bool x = false;
    int i, j = 0;
    #define SIZE 50
    char st [ SIZE ];
    wcstombs ( st, str_2.c_str ( ), SIZE );
    //buffer dinâmico
    char *busca;
    busca = new char [ str_2.Length ( ) + 1 ];
    wcstombs ( busca, str_2.c_str ( ), str_2.Length ( ) + 1 );

    str_3 = " ";
    for ( i = 0; i < 64; i++ ){
        str_3 += "\n";
              if ( strstr ( matriz [ i ] , busca ) != NULL ){
                     str_3 += matriz [ i ];
                 j++;
                 x = true;
              }
    }
    if ( j == 1 ) {
     Label2 -> Caption = "Encontramos uma ocorrência da palavra ";
     Label2 -> Caption = Label2 -> Caption + busca;
    }

    if ( j > 1 && x == true ) {
    Label2 -> Caption = "Encontramos ";
    Label2 -> Caption = Label2 -> Caption + StrToInt ( j );
    Label2 -> Caption = Label2 -> Caption + " ocorrências da palavra ";
    Label2 -> Caption = Label2 -> Caption + busca;

    }
     if ( x == false ) {
     Label2 -> Caption = "Nenhuma ocorrência da palavra ";
     Label2 -> Caption = Label2 -> Caption + busca;
    }
    Informe ( Sender );
    Label4 -> Caption = str_3;
    Edit1 -> Clear ( );
    Edit1 -> SetFocus ( );
}
//---------------------------------------------------------------------------
 


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

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

    TPaintBox *PaintBox1;
    TLabel *Label1;
    TEdit *Edit1;
    TBitBtn *BitBtn1;
    TLabel *Label4;
    TScrollBox *ScrollBox1;
    TLabel *Label2;
    void __fastcall PaintBox1Paint ( TObject *Sender );
    void __fastcall FormShow ( TObject *Sender );
    void __fastcall cria_Matriz ( TObject *Sender );
    void __fastcall BitBtn1Click(TObject *Sender);
    void __fastcall Informe ( TObject *Sender );
private: // User declarations

String str_1;
String str_2;
String str_3;

void __fastcall Label_Manual ( TObject *Sender );
public:  // User declarations
 __fastcall TForm1 ( TComponent* Owner );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



object Form1: TForm1
  Left = 413
  Top = 118
  Caption = 'PESQUISANDO STRING EM MATRIZ III'
  ClientHeight = 340
  ClientWidth = 600
  Color = clCream
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 536
    Top = 248
    Width = 33
    Height = 25
    OnPaint = PaintBox1Paint
  end
  object Label2: TLabel
    Left = 528
    Top = 24
    Width = 3
    Height = 13
  end
  object Edit1: TEdit
    Left = 536
    Top = 205
    Width = 17
    Height = 21
    TabOrder = 0
  end
  object BitBtn1: TBitBtn
    Left = 536
    Top = 80
    Width = 33
    Height = 25
    Caption = 'OK'
    TabOrder = 1
    OnClick = BitBtn1Click
  end
  object ScrollBox1: TScrollBox
    Left = 527
    Top = 128
    Width = 42
    Height = 41
    TabOrder = 2
    object Label1: TLabel
      Left = 18
      Top = 7
      Width = 3
      Height = 13
    end
    object Label4: TLabel
      Left = 18
      Top = 7
      Width = 3
      Height = 13
    end
  end
end

terça-feira, 12 de novembro de 2019

Lendo arquivo de texto - objeto TScrollBox

Aqui está um exemplo de como usar o componente
TScrollBox para apresentar os dados de um arquivo.


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

#include <vcl.h>
#pragma hdrstop
#define SI_ZE 450
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner ) :
        TForm ( Owner ) {
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender ) {
    Canvas -> Font -> Size = 16;
    Canvas -> Font -> Name = "Arial";
    Canvas -> Pen -> Color = clRed;
    Canvas -> Rectangle ( 5, 5, 590, 290 );
    Canvas -> Pen -> Width = 10;
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 60, 12, "C++ BUILDER - LENDO UM ARQUIVO DE TEXTO" );
    String str_1 = " ";
    FILE *arq;
    char **no_me;
    no_me = ( char** ) malloc ( SI_ZE * sizeof ( char* ) );
    int c = 0, i;
    //C:\Users\ManMachine\Downloads\Lendo arquivo de texto  (ScrollBox)
        arq = fopen ( "C:\\Users\\ManMachine\\Downloads\\Lendo arquivo de texto  (ScrollBox)\\"
                "Poesias famosas.txt", "r+b" );

    if ( arq == NULL ) {
        printf ( "Problemas na abertura do arquivo " );
    }
    for ( i = 0; i < SI_ZE; i++ ) {
        no_me [ i ] = ( char* ) malloc ( SI_ZE * sizeof(char*) / 3 );
        fgets ( no_me [ i ], 80, arq );
        str_1 += i + 1;
        str_1 += "  ";
        str_1 += no_me [ i ];
    }
     Label1 -> Caption = AnsiString ( str_1 );
     free ( no_me );
     fclose ( arq );
}
//---------------------------------------------------------------------------


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

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



object Form1: TForm1
  Left = 470
  Top = 177
  HorzScrollBar.ThumbSize = 3
  HorzScrollBar.Tracking = True
  Caption = 'C++ BUILDER - COMPONENTE TScrollBox'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  Visible = True
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object ScrollBox1: TScrollBox
    Left = 24
    Top = 40
    Width = 553
    Height = 241
    HorzScrollBar.Tracking = True
    VertScrollBar.Tracking = True
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clFuchsia
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    object Label1: TLabel
      Left = 24
      Top = 3
      Width = 5
      Height = 19
      Caption = ' '
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -16
      Font.Name = 'Tahoma'
      Font.Style = [fsItalic]
      ParentFont = False
    end
  end
end

sábado, 9 de novembro de 2019

Preenchendo uma matriz de string com TEdit III

Aqui está um exemplo de como preencher
uma matriz de string usando o objeto TEdit
do C++ builder.
Como acrescentei vários detalhes para
melhorar a visualização, o código
ficou um pouco extenso.




//Preenchendo uma matriz de string com TEdit III
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 190, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 190, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 210, "sa_sp10@hotmail.com" );
    Canvas -> Font -> Name = "Garamond";
    BitBtn2 -> Visible = true;
}

//---------------------------------------------------------------------------
 int __fastcall TForm1::Contador ( TObject *Sender ) {
     static int n = t;
     return n + 2;
}
//---------------------------------------------------------------------------
 int __fastcall TForm1::Contador_Negativo ( TObject *Sender ) {
     static int n = t;
     return n--;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Button_Manual ( TObject *Sender ) {
    Lbl -> Parent = Form1;
    Lbl -> Font -> Size = 12;
    Lbl -> Font -> Color = clBlue;
    Lbl -> Width = 130;
    Lbl -> Height = 20;
    Lbl -> Left = 164;
    Lbl -> Top = 82;
    if ( x == false )
    Lbl -> Caption = "Total de palavras";
    Lbl -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FuncAtual ( TObject *Sender ) {
       if ( i == u ) {
           i = i - 2;
       }
       Lbl -> Left = 164;
       Lbl -> Caption = StrToInt ( i );
       Lbl -> Caption = Lbl -> Caption + "ª";
       Lbl -> Caption = Lbl -> Caption + " Palavra ";
    if ( i == 1 ) {
     MessageBeep ( 0 );
     Label_Button_Manual ( Sender );
     Form1 -> Edit1 -> Width = 80;
     Form1 -> Edit1 -> Height = 20;
     Form1 -> Edit1 -> Left = 250;
     Form1 -> Edit1 -> Top = 82;
       Lbl -> Left = 164;
       Lbl -> Caption = StrToInt ( i );
       Lbl -> Caption = Lbl -> Caption + "ª";
       Lbl -> Caption = Lbl -> Caption + " Palavra ";
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint ( TObject *Sender )
{
    BitBtn2 -> Visible = false;
    Label_Button_Manual ( Sender );
    if ( x == false ) {
     Edit1 -> Width = 30;
     Edit1 -> Height = 20;
     Edit1 -> Left = 300;
     Edit1 -> Top = 82;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender )
{
    i++;
    int u;
    String str;
    String st;
    do {
    t = Edit1 -> Text.ToIntDef ( 0 );
    u = Contador ( Sender );
    *nomes [ i ] = Edit1 -> Text;
   if ( i == u - 1 ) {
     y = true;
   Beep ( 500, 500 );
   for ( i = 2; i < u; i++ ) {
   st += i - 1;
   st += "ª";
   st += "\n";
   //st += " ";
   str += " ";
   str += *nomes [ i ];

   Label3 -> Top = 64;
   Label3 -> Left = 220;
   Label3 -> Caption = st;

   Label1 -> Top = 64;
   Label1 -> Caption = str;
   str += "\n";
   }
   }
   FuncAtual ( Sender );
     break;
    }while ( i == u );
    Edit1 -> Text = " ";
    if ( y == false ) {
      Edit1 -> SetFocus ( );
    }
    else {
     Edit1 -> Visible = false;
     Lbl -> Caption = " ";
     Informe ( Sender );
    }
    v = Contador_Negativo ( Sender );
    Label2 -> Caption = StrToInt ( v );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender )
{
  Canvas -> Font -> Size = 14;
  Canvas -> Font -> Name = "Garamond";
  Canvas -> Pen -> Color = clGray;
  Canvas -> Pen -> Width = 10;
  Canvas -> Rectangle ( 05, 05, 595, 295 );
  SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
  Canvas -> TextOut ( 135, 12, "C++ BUILDER - MATRIZ DE STRING III" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
exit ( 0);
}
//---------------------------------------------------------------------------


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>

#include <Classes.hpp>
#include <Controls.hpp>
#include <Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Vcl.Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TEdit *Edit1;
    TPaintBox *PaintBox1;
    TBitBtn *BitBtn1;
    TLabel *Label1;
    TLabel *Lbl;
    TBitBtn *BitBtn2;
    TLabel *Label2;
    TLabel *Label3;
    void __fastcall FuncAtual ( TObject *Sender );
    void __fastcall Label_Button_Manual ( TObject *Sender );
    void __fastcall PaintBox1Paint(TObject *Sender);
    int __fastcall Contador ( TObject *Sender );
    int __fastcall Contador_Negativo ( TObject *Sender );
    void __fastcall BitBtn1Click(TObject *Sender);
    void __fastcall FormPaint(TObject *Sender);
    void __fastcall Informe ( TObject *Sender );
    void __fastcall BitBtn2Click(TObject *Sender);
private:    // User declarations
    bool y = false;
    bool x = false;
    String nomes [ 60 ] [ 16 ];
    int i, t, u, v;
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 515
  Top = 206
  Caption = 'C++ BUILDER - MATRIZ DE STRING III'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 512
    Top = 231
    Width = 41
    Height = 42
    OnPaint = PaintBox1Paint
  end
  object Label1: TLabel
    Left = 248
    Top = 120
    Width = 5
    Height = 19
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Lbl: TLabel
    Left = 64
    Top = 168
    Width = 3
    Height = 13
  end
  object Label2: TLabel
    Left = 512
    Top = 40
    Width = 32
    Height = 56
    Font.Charset = ANSI_CHARSET
    Font.Color = clRed
    Font.Height = -53
    Font.Name = 'alarm clock'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label3: TLabel
    Left = 16
    Top = 176
    Width = 5
    Height = 19
    Font.Charset = ANSI_CHARSET
    Font.Color = clRed
    Font.Height = -17
    Font.Name = 'Arial'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Edit1: TEdit
    Left = 232
    Top = 68
    Width = 121
    Height = 21
    TabOrder = 0
  end
  object BitBtn1: TBitBtn
    Left = 256
    Top = 248
    Width = 75
    Height = 25
    Caption = 'OK'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    ParentShowHint = False
    ShowHint = False
    TabOrder = 1
    OnClick = BitBtn1Click
  end
  object BitBtn2: TBitBtn
    Left = 256
    Top = 248
    Width = 75
    Height = 25
    Caption = 'Sair'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 2
    OnClick = BitBtn2Click
  end
end

sexta-feira, 1 de novembro de 2019

C++ builder - gerando caracteres

A cada click no botão gerar temos uma sequência
diferente de caracteres, que pode ter tamanhos
variados limitado pelos elementos do vetor Dim,
Que são embaralhados e invocados um elemento 
por vez para que seja o tamanho da string gerada..



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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define tam 10
#define dim 6

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 230, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 230, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 250, "sa_sp10@hotmail.com" );
}
//---------------------------------------------------------------------------
void emb_lhar ( char Num [ 10 ] [ 4 ] ) {
    int i, j, b = 0;
        for ( i = 0; i < tam; i++ ){
            j = i + rand ( ) % ( tam - i  );
            char t = *Num [ j ];
            *Num [ j ] = *Num [ i ];
            *Num [ i ] = t;
        }
}
//---------------------------------------------------------------------------
 void emblhar ( int Num [ dim ] ) {
    int i, r, temp;
    int j;
        srand ( time(NULL ) );
    for( i = 0;i < dim; i++ ) {
        j = rand ( ) % dim;
        temp = Num [ j ];
        Num [ j ] = Num [ i ];
        Num [ i ] = temp;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click ( TObject *Sender )
{
    String str_1 = " ";
    String str_2 = " ";
    Canvas -> Font -> Size = 18;
    Canvas -> Font -> Name = "Garamond";
    char Mat [ 10 ] [ 4 ] = {"s","a","m","u","e","l","l","i","m","a"};
    int vet [ dim ] = { 2, 3, 4, 5, 6, 7 };
    int b;
    int a = 1;
    int i, x = 0;
          srand ( time(NULL ) );
           emblhar ( vet );
               str_2 = " ";
                for ( i = 1; i < 6; i++ ) {
                    a = vet [ i ];
                }
                str_2 = a;
                emb_lhar ( Mat );
                for ( b = 1; b <= a; b++ ) {
                    str_1 += Mat [ b ];
                }
                Canvas -> Font -> Color = clRed;
                Canvas -> TextOut ( 190, 50, "                " );
                Canvas -> TextOut ( 190, 50, str_1 );
                Canvas -> Font -> Color = clBlue;
                Canvas -> TextOut ( 190, 80, "Geramos" );
                Canvas -> Font -> Color = clRed;
                Canvas -> TextOut ( 290, 80, str_2 );
                Canvas -> Font -> Color = clBlue;
                Canvas -> TextOut ( 305, 80, " Caracteres" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1 -> Close ( );
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
    Canvas -> Font -> Size = 18;
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Pen -> Color = clRed;
    Canvas -> Pen -> Width = 10;
    Canvas -> Rectangle ( 05, 05, 595, 295 );
    SetTextColor ( Canvas -> Handle, RGB ( 255, 0, 255 ) );
    Canvas -> TextOut ( 80, 20, "C++ BUILDER - GERANDO CARACTERES" );
    Informe ( Sender );
}
//---------------------------------------------------------------------------
 


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

#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

    TPaintBox *PaintBox1;
    TLabel *Label1;
    TButton *Button1;
    TButton *Button2;
    void __fastcall Button1Click ( TObject *Sender );
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall PaintBox1Paint(TObject *Sender);
    void __fastcall Informe ( TObject *Sender );
private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 455
  Top = 89
  Caption = 'INVOCANDO VETOR DE STRINGS'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 544
    Top = 219
    Width = 41
    Height = 33
    OnPaint = PaintBox1Paint
  end
  object Label1: TLabel
    Left = 208
    Top = 48
    Width = 5
    Height = 19
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Button1: TButton
    Left = 40
    Top = 256
    Width = 75
    Height = 25
    Caption = 'Gerar'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 488
    Top = 256
    Width = 75
    Height = 25
    Caption = 'Sair'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
    OnClick = Button2Click
  end
end