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

Nenhum comentário:

Postar um comentário

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