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

Nenhum comentário:

Postar um comentário

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