domingo, 9 de agosto de 2020

FMX - pesquisando substring em frases

  •  O C++ builder possui funções nativa para pesquisa em string,
  • Mas vamos ignorar isto e vamos usar a função,
  • strstr da biblioteca padrão string.h do C.


  • //---------------------------------------------------------------------------

  • #include <fmx.h>
  • #pragma hdrstop

  • #include "Unit1.h"
  • //---------------------------------------------------------------------------
  • #pragma package(smart_init)
  • #pragma resource "*.fmx"
  • #pragma resource ("*.Windows.fmx", _PLAT_MSWINDOWS)

  • TForm1 *Form1;

  • //---------------------------------------------------------------------------
  • __fastcall TForm1::TForm1 ( TComponent* Owner )
  • : TForm ( Owner )
  • {
  • }
  • int a;
  • bool y = false;
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
  • Rectangle1 -> Width = 600;
  • Rectangle1 -> Height = 300;
  • Rectangle1 -> Position -> X = 0;
  • Rectangle1 -> Position -> Y = 0;

  • Label1 -> Position -> X = 170;
  • Label1 -> Position -> Y = 210;
  • Label1 -> Size -> Width = 200;
  • Label1 -> Size -> Height = 30;
  • Label1 -> TextSettings -> Font -> Size = 18;
  • Label1 -> TextSettings -> FontColor = claBlue;
  • Label1 -> TextSettings -> Font -> Family = "Consolas";
  • Label1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
  • Label1 -> Text = "Por: Samuel Lima";

  • Label2 -> Position -> X = 170;
  • Label2 -> Position -> Y = 225;
  • Label2 -> Size -> Width = 200;
  • Label2 -> Size -> Height = 30;
  • Label2 -> TextSettings -> Font -> Size = 18;
  • Label2 -> TextSettings -> FontColor = claBlack;
  • Label2 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
  • Label2 -> Text = "sa_sp10@hotmail.com";

  • Label3 -> Position -> X = 160;
  • Label3 -> Position -> Y = 145;
  • Label3 -> Size -> Width = 400;
  • Label3 -> Size -> Height = 30;
  • Label3 -> TextSettings -> Font -> Size = 18;
  • Label3 -> TextSettings -> FontColor = claRed;
  • Label3 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

  • Text1 -> Position -> X = -55;
  • Text1 -> Position -> Y = 17;
  • Text1 -> Size -> Width = 700;
  • Text1 -> Size -> Height = 20;
  • Text1 -> TextSettings -> Font -> Size = 16;
  • Text1 -> TextSettings -> FontColor = claRed;
  • //Text1 -> AutoSize = True;
  • Text1 -> Size -> PlatformDefault = False;
  • Text1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
  • //Text1 -> Text = str_3;

  • Text2 -> Position -> X = -146;
  • Text2 -> Position -> Y = 47;
  • Text2 -> Size -> Width = 700;
  • Text2 -> Size -> Height = 20;
  • Text2 -> TextSettings -> Font -> Size = 14;
  • Text2 -> TextSettings -> FontColor = claBlack;
  • //Text1 -> AutoSize = True;
  • Text2 -> Size -> PlatformDefault = False;
  • Text2 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

  • Text3 -> Position -> X = -132;
  • Text3 -> Position -> Y = 67;
  • Text3 -> Size -> Width = 700;
  • Text3 -> Size -> Height = 20;
  • Text3 -> TextSettings -> Font -> Size = 14;
  • Text3 -> TextSettings -> FontColor = claBlack;
  • //Text1 -> AutoSize = True;
  • Text3 -> Size -> PlatformDefault = False;
  • Text3 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

  • Text4 -> Position -> X = -142;
  • Text4 -> Position -> Y = 87;
  • Text4 -> Size -> Width = 700;
  • Text4 -> Size -> Height = 20;
  • Text4 -> TextSettings -> Font -> Size = 14;
  • Text4 -> TextSettings -> FontColor = claBlack;
  • //Text4 -> AutoSize = True;
  • Text4 -> Size -> PlatformDefault = False;
  • Text4 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

  • Text5 -> Position -> X = -205;
  • Text5 -> Position -> Y = 107;
  • Text5 -> Size -> Width = 700;
  • Text5 -> Size -> Height = 20;
  • Text5 -> TextSettings -> Font -> Size = 14;
  • Text5 -> TextSettings -> FontColor = claRed;
  • //Text5 -> AutoSize = True;
  • Text5 -> Size -> PlatformDefault = False;
  • Text5 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

  • Text6 -> Position -> X = -95;
  • Text6 -> Position -> Y = 127;
  • Text6 -> Size -> Width = 700;
  • Text6 -> Size -> Height = 20;
  • Text6 -> TextSettings -> Font -> Size = 14;
  • Text6 -> TextSettings -> FontColor = claBlack;
  • //Text6 -> AutoSize = True;
  • Text6 -> Size -> PlatformDefault = False;
  • Text6 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

  • Edit1 -> SetFocus ( );
  • Edit1 -> Font -> Size = 10;
  • Edit1 -> Width = 130;
  • Edit1 -> Height = 17;
  • Edit1 -> Position -> X = 290;
  • Edit1 -> Position -> Y = 47;

  • Edit2 -> Font -> Size = 10;
  • Edit2 -> Width = 130;
  • Edit2 -> Height = 17;
  • Edit2 -> Position -> X = 290;
  • Edit2 -> Position -> Y = 67;

  • Button1 -> Width = 30;
  • Button1 -> Height = 17;
  • Button1 -> Position -> X = 390;
  • Button1 -> Position -> Y = 87;
  • Button1 -> TextSettings -> Font -> Family = "Arial";
  • //Button1 -> TextSettings -> Font -> Size = 16;
  • Button1 -> TextSettings -> FontColor = claBlue;

  • Button2 -> Width = 50;
  • Button2 -> Height = 17;
  • Button2 -> Position -> X = 470;
  • Button2 -> Position -> Y = 260;
  • Button2 -> TextSettings -> Font -> Family = "Arial";
  • Button2 -> TextSettings -> Font -> Size = 16;
  • Button2 -> TextSettings -> FontColor = claRed;
  • Button2 -> Text = "Sair";
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::FormShow ( TObject *Sender ) {
  • Label_Manual ( Sender );
  • Text1 -> Text = "FMX - PESQUISANDO SUBSTRING EM FRASES";
  • a = 0;
  • if ( a == 0 ) {
  • Text2 -> Text = "Digite uma frase : ";
  • Text3 -> Text = "Digite uma sub-frase : ";
  • Text4 -> Text = "Pesquisar na frase: ";
  • }
  • Button1 -> Text = "Ok";
  • Button2 -> Visible = false;
  • Label1 -> Visible = false;
  • Label2 -> Visible = false;
  • Edit1 -> Visible = true;
  • Edit2 -> Visible = true;
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::Button1Click ( TObject *Sender ) {
  • char str [ 50 ] = " ";
  • char str_2 [ 50 ] = " ";

  • String st;
  • st += Edit1 -> Text;
  • wcstombs (  str, st.c_str ( ), 50 );

  • String st_2;
  • st_2 += Edit2 -> Text;
  • wcstombs (  str_2, st_2.c_str ( ), 50 );

  • if (  Edit1 -> Text != "" && Edit2 -> Text != "" ) {
  •     a++;
  • if ( a == 1 ) {
  • Edit1 -> Visible = false;
  • Edit2 -> Visible = false;
  • Text3 -> Text = " ";
  • Text4 -> Text = " ";
  • Text2 -> Text = "Abaixo a frase digitada: ";

  •         Text3 -> Size -> Width = 860;
  • Text3 -> TextSettings -> FontColor = claRed;
  • Text3 -> Position -> X = -192;
  • Text3 -> Text = st;
  • }
  • char *ptr;
  • if ( strstr ( str, str_2 ) != NULL ) {
  • Text2 -> Text = " ";
  • Text3 -> Text = " ";
  • Text2 -> Text = "Abaixo a frase digitada: ";
  • Text3 -> TextSettings -> FontColor = claRed;
  • Text3 -> Position -> X = -192;
  • Text3 -> Text = st;

  • Text4 -> Position -> X = -132;
  • Text4 -> Text = "A frase contém a substring";
  • Text5 -> Text += str_2;
  •         Button1 -> Width = 100;
  • Button1 -> Position -> X = 70;
  • Button1 -> Position -> Y = 260;
  • Button1 -> Text = "Nova frase";
  • Button2 -> Visible = true;
  • Label1 -> Visible = true;
  • Label2 -> Visible = true;
  • Edit1 -> Visible = false;
  • Edit2 -> Visible = false;
  • }
  • else {
  • Text2 -> Text = " ";
  • Text3 -> Text = " ";
  • Text2 -> Text = "Abaixo a frase digitada: ";
  • Text3 -> TextSettings -> FontColor = claRed;
  • Text3 -> Position -> X = -203;
  • Text3 -> Text = st;

  • Text4 -> Position -> X = -122;
  • Text4 -> Text = "A frase não contém a substring";
  • Text5 -> Text += str_2;
  • Text6 -> Position -> Y = 177;
  • Text6 -> Text = "Tente começar tudo de novo";

  •         Button1 -> Width = 100;
  • Button1 -> Position -> X = 70;
  • Button1 -> Position -> Y = 260;
  • Button1 -> Text = "Nova frase";
  • Button2 -> Visible = true;
  • Label1 -> Visible = true;
  • Label2 -> Visible = true;
  • Edit1 -> Visible = false;
  • Edit2 -> Visible = false;

  • }
  • if ( a == 2 ) {
  • Text4 -> Text = " ";
  • Text5 -> Text = " ";
  • Text6 -> Text = " ";
  • Edit1 -> Text = "";
  • Edit2 -> Text = "";
  • Label3 -> Text = " ";
  • FormShow ( Sender );
  •   }


  • String Usuario_1 = "Corinthians";
  • String Usuario_2 = Edit2 -> Text;
  • Boolean Result_1 = SameText ( Usuario_1, Usuario_2 );
  • if ( Result_1 == True ) {
  • Beep ( 400, 500 );
  •     Label3 -> Text = "Não digite palavrões, por favor!";
  • }


  • }
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::Button2Click ( TObject *Sender ) {
  • Close ( );
  • }
  • //---------------------------------------------------------------------------


Nenhum comentário:

Postar um comentário

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