terça-feira, 24 de dezembro de 2019

Pesquisando caracteres em matriz de string

Já que criamos um programa que pesquisa os
múltiplos de um número numa matriz de inteiros,
achei muito justo criar um programa que pesquise,
caracteres numa matriz de string, e isto foi o que fiz.
Abri o C++ Builder e comecei a programar inserindo
nossos conhecimentos em linguagem C/C++.
Com este exemplo poderíamos por exemplo criar
um jogo de palavras, entrava com a matriz vazia,
lançava duas ou três dicas das palavras secretas,
e o usuário entraria com os caracteres.
Se estes caracteres pertencestes as palavras,
seriam marcados na tela, e senão o programa
emitiria uma mensagem de erro acompanhada de um Beep.
Quer saber como isto foi feito click no link,
agora mesmo e desfrute de um bom código,
feito exclusivamente no C++ Builder da Embarcadero.




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

#include <vcl.h>
#pragma hdrstop
#define tam 20
#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
     char string [ 20 ] [ 22 ] = {
                  "QOBGCOHOPPNAYGBDFRHO",
                  "OCVWMLWAUPWGKISJGAUA",
                  "OIIJVIPIDFQNIOKZZZYC",
                  "TTWSSCPAXBLIGSORVIOA",
                  "NSOJEAMBINDTHTYHFLOE",
                  "GIHTJBMKJZIAAGOKIAMT",
                  "RLTPNWTQYFYCOTZWSBHE",
                  "XAVCLERGVWCHNGAJJAIC",
                  "ABZSLFMIKTHEIVOUKGLA",
                  "ZAJIZEAARIMSARKPNDFC",
                  "RCYBLBTWHANAEEHCZNLL",
                  "RJNEDAVNHNLDJNXZAFEB",
                  "CDAUCCNLEOIVXJQTSFHI",
                  "LATGLOUARCJPOEVJUVOX",
                  "MOJIQBLABMAWSUDWGRFD",
                  "RYDHRRCOCQLJREGAEDVF",
                  "ETKOIRKLGHAKDJBJBTTB",
                  "EVBCAHVBAOWDCAIAZAZJ",
                  "OANBYCUVGCACOSQSWILN",
                  "YRXUJCVSTXBGKYWJBMGI"};
                  int t = 0, x = 0;
 //---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 260, 440, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 300, 440, "Samuel Lima" );
    //Canvas -> Font -> Color = clBlack;
    //Canvas -> TextOut ( 200, 300, "sa_sp10@hotmail.com" );
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Imprime_matriz ( TObject* Sender ) {
      int a = 0, b = 0, i = 0;

    for ( a = 0; a < tam; a++ ) {
        for ( b = 0; b < tam; b++ ) {
             Canvas -> Font -> Color = clBlack;
             Canvas -> TextOut ( 110 + ( b * 20 ), 35 + ( a * 20 ), string [ a ] [ b ] );
        }
    }
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject* Sender ) {
    Edit1 -> SetFocus ( );
    Edit1 -> Font -> Size = 12;
    Edit1 -> Width = 90;
    Edit1 -> Height = 20;
    Edit1 -> Left = 110;
    Edit1 -> Top = 460;

    Label1 -> Font -> Size = 12;
    Label1 -> Font -> Name = "Arial";
    Label1 -> Font -> Color = clBlack;
    Label1 -> Width = 30;
    Label1 -> Height = 20;
    Label1 -> Left = 250;
    Label1 -> Top = 460;

    BitBtn1 -> Font -> Size = 12;
    BitBtn1 -> Font -> Color = clBlue;
    BitBtn1 -> Font -> Style = TFontStyles ( ) << fsItalic;
    BitBtn1 -> Top = 460;
    BitBtn1 -> Left = 210;
    BitBtn1 -> Height = 23;
    BitBtn1 -> Width = 30;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
    Canvas -> Pen -> Color = clBlue;
    Canvas -> Pen -> Width = 10;
    Canvas -> Rectangle ( 05, 05, 595, 495 );

    Canvas -> Font -> Size = 13;
    Canvas -> Font -> Name = "Arial";
    Canvas -> Font -> Style = TFontStyles ( ) << fsItalic << fsBold;
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 130, 10, "PESQUISANDO CARACTERES EM MATRIZ" );

    Canvas -> Font -> Style = TFontStyles ( ) << fsItalic;
    Canvas -> Font -> Size = 11;
    Canvas -> Font -> Color = clBlue;
    Canvas -> TextOut ( 110, 440, "Digite um caractere: " );
    Canvas -> Font -> Size = 12;
    Canvas -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;

    Imprime_matriz ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
      Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
    int a = 0, b = 0, i = 0;
    if ( x == 0 ) {
        Imprime_matriz  ( Sender );
    }
    String str_2;
    str_2 += Edit1 -> Text;
    #define SIZE 20
    char st [ SIZE ];
    char conv [ 3 ];
    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 );
     t = 0;
    for ( a = 0; a < tam; a++ ) {
        for ( b = 0; b < tam; b++ ) {
        //toupper converte de minusculo para maiusculo
            if ( string [ a ] [ b ]  == _toupper ( *busca ) ) {
              t ++ ;
                if ( x == 0 ) {
              //Canvas -> Font -> Color = clRed;
              SetBkColor ( Canvas -> Handle, RGB ( 0,250,154 ) );
               Canvas -> TextOut ( 110 + ( b * 20 ), 35 + ( a * 20 ), string [ a ] [ b ] );
               }
            }
        }
    }

       Canvas -> Refresh ( );
       Label1 -> Caption = "Encontrado ";
       Label1 -> Caption = Label1 -> Caption + t;
       Label1 -> Caption = Label1 -> Caption + " Caracteres  ";
       //strupr converte de minusculo para maiusculo
       Label1 -> Caption = Label1 -> Caption + strupr ( busca );

       Informe ( Sender );

       Edit1 -> Clear ( );
       Edit1 -> SetFocus ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(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.Grids.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Vcl.Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TLabel *Label1;
    TEdit *Edit1;
    TBitBtn *BitBtn1;
    TBitBtn *BitBtn2;

    void __fastcall FormPaint ( TObject *Sender );
    void __fastcall Label_Manual ( TObject* Sender );
    void __fastcall FormShow ( TObject *Sender );
    void __fastcall BitBtn1Click ( TObject *Sender );
    void __fastcall Informe ( TObject *Sender );
    void __fastcall Imprime_matriz ( TObject* Sender );
    void __fastcall BitBtn2Click ( TObject *Sender );
private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 439
  Top = 55
  Caption = 'C++ BUILDER - MATRIZ 8 x 8 EM STRINGGRID'
  ClientHeight = 500
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clRed
  Font.Height = -13
  Font.Name = 'Arial'
  Font.Style = [fsBold, fsItalic]
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 16
  object Label1: TLabel
    Left = 536
    Top = 24
    Width = 4
    Height = 16
  end
  object Edit1: TEdit
    Left = 536
    Top = 64
    Width = 49
    Height = 24
    ParentShowHint = False
    ShowHint = False
    TabOrder = 0
  end
  object BitBtn1: TBitBtn
    Left = 536
    Top = 120
    Width = 41
    Height = 25
    Caption = 'Ok'
    TabOrder = 1
    OnClick = BitBtn1Click
  end
  object BitBtn2: TBitBtn
    Left = 483
    Top = 455
    Width = 57
    Height = 25
    Caption = 'Sair'
    TabOrder = 2
    OnClick = BitBtn2Click
  end
end

Nenhum comentário:

Postar um comentário

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