terça-feira, 24 de dezembro de 2019

Pesquisando string em matriz bidimensional

Sabemos que o C e o C++ (C/C++), possuem
funções nativas para eficientes pesquisas
em textos de arquivos ou matriz,
e o C++ Bulder também possui, mas preferi
criar uma rotina manualmente e deu
um bom trabalho, porque se fosse só pesquisar
seria muito fácil, mas queria destacar as pesquisas
encontradas com um fundo em cor diferente do texto
apresentado, tal como em editores profissionais
de texto famosos que conhecemos,
depois de umas boas cabeçadas chegamos no
objetivo final, e lhes mostro ainda como fiz,
mas antes pergunto, você consegue encontrar
outros exemplos de códigos com o mesmo objetivo
deste que lhes apresento? certamente que não.
O código completo é oferecido aos que utilizam
esta ferramenta excepcional para programação em C/C++.




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

#include <vcl.h>
#pragma hdrstop
#define tam 20
#define lin  12
#define col 45
#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
     char texto [ lin ] [ col ] = {
        "No Meio do Caminho                         ",
        "Carlos Drummond de Andrade                 ",
        "No meio do caminho tinha uma pedra         ",
        "Tinha uma pedra no meio do caminho         ",
        "Tinha uma pedra                            ",
        "No meio do caminho tinha uma pedra.        ",
        "Nunca me esquecerei desse acontecimento    ",
        "Na vida de minhas retinas tao fatigadas.   ",
        "Nunca me esquecerei que no meio do caminho ",
        "Tinha uma pedra                            ",
        "Tinha uma pedra no meio do caminho         ",
        "No meio do caminho tinha uma pedra.        "};
                  int t = 0, x = 0;

                  char tex_to [ lin ] [ col ];
 //---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Arial";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 360, 290, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 400, 290, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 360, 310, "sa_sp10@hotmail.com" );
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Imprime_matriz ( TObject* Sender ) {
    int a = 0, b = 0, i = 0;
    Canvas -> Font -> Size = 12;
    Canvas -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    for ( a = 0; a < lin; a++ ) {
        for ( b = 0; b < col; b++ ) {
             Canvas -> Font -> Color = clBlack;
             Canvas -> TextOut ( 60 + ( b * 13 ), 35 + ( a * 20 ), texto [ 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 = 305;

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

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

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

    Canvas -> Font -> Style = TFontStyles ( ) << fsItalic;
    Canvas -> Font -> Size = 11;
    Canvas -> Font -> Color = clBlue;
    Canvas -> TextOut ( 110, 280, "Digite uma palavra: " );

    Imprime_matriz ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender )
{
      Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender )
{
    int a = 0, b = 0, g = 0, i = 0, j = 0, k = 0, cont = 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 *pt;
    pt = new char [ str_2.Length ( ) + 1 ];
    wcstombs ( pt, str_2.c_str ( ), str_2.Length ( ) + 1 );
     t = 0;
        int lenv = strlen ( pt );
        for ( i = 0; i < lin; i++ ) {
           for ( j = 0; j < col; j++ ) {
               k = 0;
               while ( texto [ i ] [ j + k ] == pt [ k ] && pt [ k ] != '\0'
                && texto [ i ] [ j + k ] != '\0' )
                   k++;
               if ( pt [ k ] == '\0' ) {
                   ++cont;
                   SetBkColor ( Canvas -> Handle, RGB ( 255, 255, 0 ) );
                   for ( g = 0; g < lenv; g++ ) {
                   Canvas -> TextOut ( 60 + ( j * 13 ), 35 + ( i * 20 ), texto [ i ] [ j ] );
                       j++;
                   }
               }
           }
       }

       Canvas -> Refresh ( );
       Label1 -> Caption = "Encontrado ";
       Label1 -> Caption = Label1 -> Caption + cont;
       Label1 -> Caption = Label1 -> Caption + " Ocorrências da palavra  ";
       Label1 -> Caption = Label1 -> Caption + ( pt );

       Informe ( Sender );

       Edit1 -> Clear ( );
       Edit1 -> SetFocus ( );
       free ( pt );
}
//---------------------------------------------------------------------------
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 = 'PESQUISANDO STRINGS EM MATRIZ'
  ClientHeight = 385
  ClientWidth = 665
  Color = clCream
  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
    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 = 579
    Top = 300
    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.