domingo, 17 de março de 2019

C++ builder - gerando matriz embaralhada

Preenchemos uma matriz de inteiros por um 
contador com laço for, em seguida, num bloco de código
específico para isto embaralhamos os elementos gerados.
Para a impressão na janela, o C++ builder tem várias opções, 
mas preferi utilizar um StringGrid,
já que o mesmo nos fornece amplas possibilidades de personalizações.
Abra seu Embarcadero Rad Studio agora mesmo e
Acese o menu File -> New, e clique com o mouse em VCL Forms Application - C++ builder.
Deixe o Embarcadero criar os arquivos necessários para o projeto.
Agora que ele terminou seu processo basta substituir seu arquivos pelos publicados abaixo,
para fazer isto só precisa apagar os códigos que ele gerou e copiar estes no lugar seguindo a organização.




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

#include <vcl.h>
#pragma hdrstop
#define tam 8
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
String str_1 = " ";
int a = 0, b = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
    : TForm ( Owner )
{
  StringGrid1 -> DefaultDrawing = False;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender )
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clRed;
    Canvas -> Rectangle ( 05, 05, 595, 295 );
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Name = "arial";
    Canvas -> Font -> Color = clRed;
    Canvas -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic << fsUnderline;
    Canvas -> TextOut ( 60, 12, "C++ BUILDER - GERANDO MATRIZ EMBARALHADA" );
    StringGrid1 -> Cells [ 0 ] [ 1 ] = "Linha 0";
    StringGrid1 -> Cells [ 0 ] [ 2 ] = "Linha 1";
    StringGrid1 -> Cells [ 0 ] [ 3 ] = "Linha 2";
    StringGrid1 -> Cells [ 0 ] [ 4 ] = "Linha 3";
    StringGrid1 -> Cells [ 0 ] [ 5 ] = "Linha 4";
    StringGrid1 -> Cells [ 0 ] [ 6 ] = "Linha 5";
    StringGrid1 -> Cells [ 0 ] [ 7 ] = "Linha 6";
    StringGrid1 -> Cells [ 0 ] [ 8 ] = "Soma";

    StringGrid1 -> Cells [ 1 ] [ 0 ] = "Col 0";
    StringGrid1 -> Cells [ 2 ] [ 0 ] = "Col 1";
    StringGrid1 -> Cells [ 3 ] [ 0 ] = "Col 2";
    StringGrid1 -> Cells [ 4 ] [ 0 ] = "Col 3";
    StringGrid1 -> Cells [ 5 ] [ 0 ] = "Col 4";
    StringGrid1 -> Cells [ 6 ] [ 0 ] = "Col 5";
    StringGrid1 -> Cells [ 7 ] [ 0 ] = "Col 6";
    StringGrid1 -> Cells [ 8 ] [ 0 ] = "Soma";

    int vet [ tam ] [ tam ];
    int i, j, temp, r, y;
    int soma = 0, tot = 0;
        for( i = 0; i < tam * tam; i++ ) {
        vet [ a ] [ b ] =  i;
        b++;
    }
    srand ( time ( NULL ) );
    for ( i = 1; i < tam; i++ ) {
    for ( j = 1; j < tam; j++ ) {
        r = rand ( ) % tam;
        y = rand ( ) % tam;
        temp = vet [ i ] [ j ];
        vet [ i ] [ j ] = vet [ r ] [ y ];
        vet [ r ] [ y ] = temp;
    }
    }
    i = 1;
    j = 1;
    for ( i = 1; i < tam; i++ ) {
        for ( j = 1; j < tam; j++ )
        tot += vet [ i ] [ j ];
        StringGrid1 -> Cells [ j ] [ i ] = tot;
        tot = 0;
    }
        for ( j = 1; j < tam; j++ ) {
        for ( i = 1; i < tam; i++ )
         soma += vet [ i ] [ j ];
        StringGrid1 -> Cells [ j ] [ i ] = soma;
        soma = 0;
    }
    for ( i = 1; i < tam; i++ ) {
   for ( j = 1; j < tam; j++ ) {
   StringGrid1 -> Cells [ j ] [ i ] = vet [ i ] [ j ];
  }
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell ( TObject *Sender, int ACol,
 int ARow, TRect &Rect, TGridDrawState State )
{
 if( State.Contains ( gdFixed ) )
    {
    StringGrid1 -> Canvas -> Font -> Size = 8;
    StringGrid1 -> Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0,191,255 ) );
    StringGrid1 -> Canvas -> Font -> Style = TFontStyles ( ) << fsBold;
    StringGrid1 -> Canvas -> Font -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
    StringGrid1 -> Canvas -> Rectangle ( Rect );
    }
    else if ( State.Contains ( gdSelected ) )
    {
    StringGrid1 -> Canvas -> Font -> Size = 10;
    StringGrid1 -> Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0, 205, 155 ) );
    StringGrid1 -> Canvas -> Font -> Style = TFontStyles ( ) >> fsBold;
    StringGrid1 -> Canvas -> FillRect ( Rect );
    }
    else if( State.Contains ( gdSelected ) == 0 )
    {
    Beep ( 1000, 250 );
    StringGrid1 -> Canvas -> Font -> Size = 10;
    StringGrid1 -> Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0,255,255 ) );
    StringGrid1 -> Canvas -> Font -> Style = TFontStyles ( ) << fsBold;
    StringGrid1 -> Canvas -> Font -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
    StringGrid1 -> Canvas -> Rectangle ( Rect );
    }
    if( ACol == 8 && ARow == 8 || ACol == 0 && ARow == 0 ||
     ACol == 0 && ARow == 8  || ACol == 8 && ARow == 0 )
    {
     StringGrid1 -> Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0, 255, 0 ) );
    }
    UnicodeString text = StringGrid1 -> Cells [ ACol ] [ ARow ];
    StringGrid1 -> Canvas -> TextRect ( Rect, Rect.Left, Rect.Top, text );
}

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


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


#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Grids.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TStringGrid *StringGrid1;
    void __fastcall OnPaint ( TObject *Sender );
    //void __fastcall FormCreate(TObject *Sender);
    //void __fastcall FormDestroy ( TObject *Sender );
    void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
          TGridDrawState State);
private:    // User declarations
public:     // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endi


object Form1: TForm1
  Left = 356
  Top = 177
  Caption = 'C++ BUILDER - GERANDO MATRIZ EMBARALHADA'
  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 = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 120
    Top = 40
    Width = 372
    Height = 234
    ColCount = 9
    DefaultColWidth = 40
    FixedColor = clHotLight
    RowCount = 9
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 0
    OnDrawCell = StringGrid1DrawCell
    RowHeights = (
      24
      24
      24
      30
      24
      24
      24
      24
      24)
  end
en




Nenhum comentário:

Postar um comentário

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