terça-feira, 21 de julho de 2020

FMX - Embaralhando frases

Este programa foi criado num projeto C++,
utilizando é claro o C++ Builder da Embarcadero,
mas a parte lógica foi criada em C,
sendo que nada impede os mais experientes
converterem este código para outras linguagens.
Fiz uso da biblioteca FMX,
que é simplesmente incrível e poderosa.
A maioria de meus programas no C++ Builder,
eu utilizo a biblioteca VCL,
mas as vezes sinto falta do FMX,
e me rendo a sua soberania.
Viva o C++, Viva o C++ Builder, Viva FMX!



Veja abaixo os códigos do programa:

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

#include <fmx.h>
#pragma hdrstop
#include <time.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
int a;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
   Image1 -> Width = 600;
   Image1 -> Height = 350;
   Image1 -> Position -> X = 0;
   Image1 -> Position -> Y = 0;

   Image2 -> Width = 600;
   Image2 -> Height = 350;
   Image2 -> Position -> X = 0;
   Image2 -> Position -> Y = 0;

   Edit1 -> SetFocus ( );
   Edit1 -> Font -> Size = 10;
   Edit1 -> Width = 130;
   Edit1 -> Height = 17;
   Edit1 -> Position -> X = 287;
   Edit1 -> Position -> Y = 35;

   Label1 -> Position -> X = 140;
   Label1 -> Position -> Y = 90;
   Label1 -> Size -> Width = 400;
   Label1 -> Size -> Height = 35;
   Label1 -> TextSettings -> Font -> Size = 19;
   Label1 -> TextSettings -> FontColor = claRed;
   Label1 -> Size -> PlatformDefault = False;
   Label1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

   Label2 -> Position -> X = 140;
   Label2 -> Position -> Y = 170;
   Label2 -> Size -> Width = 400;
   Label2 -> Size -> Height = 35;
   Label2 -> TextSettings -> Font -> Size = 19;
   Label2 -> TextSettings -> FontColor = claBlue;
   Label2 -> Size -> PlatformDefault = False;
   Label2 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;

   Button1 -> TextSettings -> Font -> Size = 16;
   Button1 -> TextSettings -> FontColor = claBlue;
   Button1 -> TextSettings -> Font -> Family = "Arial";
   Button1 -> TextSettings -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
   Button1 -> Position -> X = 430;
   Button1 -> Position -> Y = 35;
   Button1 -> Height = 18;
   Button1 -> Width = 30;
   Button1 -> Text = "OK";
   Button1 -> OnClick = FormShow;

   RoundRect1 -> Position -> X = 340;
   RoundRect1 -> Position -> Y = 270;
   RoundRect1 -> Height = 18;
   RoundRect1 -> Width = 100;
   //RoundRect1 -> Text = "NOVA FRASE";

}
//---------------------------------------------------------------------------
int Resolve_Problema ( int x ) {
int j, b, p;

int lenv;
char *Vet;
char *Vet_2;
String st;
char *r;
st += Form1 -> Edit1 -> Text;

Vet = ( char * )  malloc ( 28 );
r = Vet;

wcstombs (  Vet, st.c_str ( ), 50 );
Form1 -> Label1 -> Text = Vet;

if ( x >= 2 ) {
//Beep ( 500, 500 );
srand ( time ( NULL ) );
for ( b = 0; b < strlen ( Vet ); b++ ) {
j = rand ( ) % ( strlen ( Vet ) );
char t = r [ j ];
r [ j ] = r [ b ];
r [ b ] = t;
}
Form1 -> Label2 -> Text = " ";
for ( p = 0; p < strlen ( Vet ); p++ ) {
  Form1 -> Label2 -> Text = Form1 -> Label2 -> Text + r [ p ];
}
}
return ( 0 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
int x;
Label_Manual ( Sender );
a++;
x++;
   Resolve_Problema ( x );
   Image2 -> Visible = false;
   Image1 -> Visible = true;
   RoundRect1 -> Visible = false;
if ( a >= 2 ) {
Form1 -> Image1 -> Visible = false;
Form1 -> Image2 -> Visible = true;
Edit1 -> Visible = false;

Button1 -> Position -> X = 140;
Button1 -> Position -> Y = 270;
Button1 -> Height = 18;
Button1 -> Width = 100;
Button1 -> Text = "EMBARALHA";
RoundRect1 -> Visible = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RoundRect1Click ( TObject *Sender ) {
   a = 0;
   Label_Manual ( Sender );
   FormShow ( Sender );
   Image2 -> Visible = false;
   Image1 -> Visible = true;
   Edit1 -> Visible = true;
   Label1 -> Text = " ";
   Label2 -> Text = " ";
}

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



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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.Edit.hpp>
#include <FMX.Objects.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Types.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TImage *Image1;
TImage *Image2;
TEdit *Edit1;
TButton *Button1;
TLabel *Label1;
TLabel *Label2;
TRoundRect *RoundRect1;
void __fastcall FormShow(TObject *Sender);
    void __fastcall Label_Manual ( TObject *Sender );
void __fastcall RoundRect1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

segunda-feira, 20 de julho de 2020

FMX - Imprimindo tabuada

Criar uma tabuada em C ou C++ é tarefa muito fácil,
qualquer iniciante pode fazer, mas esta aqui usa 
uma janela gráfica feita no C++ Builder da Embarcadero,
acompanhem seu funcionamento no vídeo abaixo:



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

#include <fmx.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
int x;
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
   Edit1 -> SetFocus ( );
   Edit1 -> Font -> Size = 10;
   Edit1 -> Width = 90;
   Edit1 -> Height = 20;
   Edit1 -> Position -> X = 180;
   Edit1 -> Position -> Y = 70;

   Image1 -> Width = 600;
   Image1 -> Height = 350;
   Image1 -> Position -> X = 0;
   Image1 -> Position -> Y = 0;

   Image2 -> Width = 600;
   Image2 -> Height = 350;
   Image2 -> Position -> X = 0;
   Image2 -> Position -> Y = 0;

   Label1 -> Position -> X = 230;
   Label1 -> Position -> Y = -10;
   Label1 -> Size -> Width = 380;
   Label1 -> Size -> Height = 375;
   Label1 -> TextSettings -> Font -> Size = 14;
   Label1 -> TextSettings -> FontColor = claBlack;
   Label1 -> Size -> PlatformDefault = False;
   Label1 -> Font -> Style = TFontStyles ( ) << fsBold;

   RoundRect1 -> Width = 85;
   RoundRect1 -> Height = 25;
   RoundRect1 -> Position -> X = 290;
   RoundRect1 -> Position -> Y = 67;

   RoundRect2 -> Width = 85;
   RoundRect2 -> Height = 25;
   RoundRect2 -> Position -> X = 290;
   RoundRect2 -> Position -> Y = 67;
   RoundRect2 -> OnClick = FormShow;

   RoundRect3 -> Width = 119;
   RoundRect3 -> Height = 25;
   RoundRect3 -> Position -> X = 206;
   RoundRect3 -> Position -> Y = 270;

   RoundRect4 -> Width = 119;
   RoundRect4 -> Height = 25;
   RoundRect4 -> Position -> X = 206;
   RoundRect4 -> Position -> Y = 270;
}
//---------------------------------------------------------------------------
int verificaTabuada ( int n ) {
int i, b;
for ( i = 1; i <= 10; i++ ) {
b = n * i;
Form1 -> Label1 -> Text = Form1 -> Label1 -> Text + StrToInt ( n );
Form1 -> Label1 -> Text = Form1 -> Label1 -> Text + " X ";
Form1 -> Label1 -> Text = Form1 -> Label1 -> Text + StrToInt ( i );
Form1 -> Label1 -> Text = Form1 -> Label1 -> Text + " = ";
Form1 -> Label1 -> Text = Form1 -> Label1 -> Text + StrToInt ( b );
Form1 -> Label1 -> Text = Form1 -> Label1 -> Text + "\n";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
   Label_Manual ( Sender );
   Image2 -> Visible = false;
   Image1 -> Visible = true;

   RoundRect1 -> Visible = true;
       RoundRect2 -> Visible = false;
   RoundRect3 -> Visible = false;
   RoundRect4 -> Visible = false;
   int n;
   String str_1;
   x++;
   if ( x == 2 ) {
   n = StrToInt ( Edit1 -> Text );
   if ( n < 20 && n > 0 ) {
verificaTabuada ( n );
Image2 -> Visible = true;
RoundRect3 -> Visible = true;
RoundRect1 -> Visible = false;
RoundRect2 -> Visible = false;

   }
   else {
   Edit1 -> Text = " ";
  x = 0;
   }
   x = 0;
}

}
//---------------------------------------------------------------------------
void __fastcall TForm1::RoundRect1MouseEnter ( TObject *Sender ) {
RoundRect1 -> Visible = false;
RoundRect2 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RoundRect2MouseLeave ( TObject *Sender ) {
   if ( x == 1 ) {
RoundRect2 -> Visible = false;
RoundRect1 -> Visible = true;
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RoundRect3MouseEnter ( TObject *Sender ) {
RoundRect3 -> Visible = false;
RoundRect4 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RoundRect4MouseLeave ( TObject *Sender ) {
if ( x == 0 ) {
RoundRect4 -> Visible = false;
RoundRect3 -> Visible = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RoundRect4Click ( TObject *Sender ) {
FormShow ( Sender );
Edit1 -> SetFocus ( );
Edit1 -> Text = " ";
Label1 -> Text = " ";
}
//---------------------------------------------------------------------------


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.Edit.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Types.hpp>
#include <FMX.Objects.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TLabel *Label1;
TImage *Image1;
TImage *Image2;
TRoundRect *RoundRect1;
TRoundRect *RoundRect2;
TRoundRect *RoundRect3;
TRoundRect *RoundRect4;
void __fastcall FormShow ( TObject *Sender );
void __fastcall Label_Manual ( TObject *Sender );
void __fastcall RoundRect1MouseEnter(TObject *Sender);
void __fastcall RoundRect2MouseLeave(TObject *Sender);
void __fastcall RoundRect3MouseEnter(TObject *Sender);
void __fastcall RoundRect4MouseLeave(TObject *Sender);
void __fastcall RoundRect4Click(TObject *Sender);

private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

segunda-feira, 25 de maio de 2020

C++ Builder - Relógio digital

Já que criamos um relógio digital
em eletrônica com proteus,
outro em Linguagem C,
com saída no console do windows, 
outro com a biblioteca graphics.h,
outro usando linguagem java,
e outro usando o JavaFx, o que poderia impedir
de criarmos um usando o C++ Builder?
Aqui está funcionando no vídeo 
um relógio digital criado no C++ Builder,
e em nada ficou em falta.




//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer ( TObject *Sender ) {
if ( horas < 10 )
Label1 -> Caption = "0";
if ( horas == 24 ) {
Label1 -> Left = 90;
horas = 0;
Label1 -> Caption = "0";
}
Label1 -> Caption = Label1 -> Caption + StrToInt ( horas );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer2Timer ( TObject *Sender ) {
if ( inicia_minutos == true ) {
minutos++;
}
if ( minutos < 10 ) {
Label2 -> Caption = "0";
}
if ( minutos == 60 ) {
minutos = 0;
horas++;
Label2 -> Caption = "0";
}
if ( ( minutos >= 10 && minutos < 60 ) )
Label2 -> Caption = StrToInt ( minutos );

if ( ( minutos >= 20 && minutos < 60 ) ){
Label2 -> Caption = StrToInt ( minutos );
Label2 -> Caption = Label2 -> Caption + " ";
}

Label2 -> Caption = Label2 -> Caption + StrToInt ( minutos );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer3Timer ( TObject *Sender ) {
if ( inicia_segundos == true ) {
segundos++;
}
if ( segundos < 10 ) {
Label3 -> Caption = "0";
}
if ( segundos == 60 ) {
segundos = 0;
        minutos++;
Label3 -> Caption = "0";
}
if ( ( segundos >= 10 && segundos < 60 ) )
Label3 -> Caption = StrToInt ( segundos );

if ( ( segundos >= 20 && segundos < 60 ) ){
Label3 -> Caption = StrToInt ( segundos );
Label3 -> Caption = Label3 -> Caption + " ";
}

Label3 -> Caption = Label3 -> Caption + StrToInt ( segundos );
}
//---------------------------------------------------------------------------



quarta-feira, 6 de maio de 2020

Juros compostos - pesquisando o tempo necessário

Para pesquisar o tempo em uma aplicação de juros compostos,
precisamos entrar com três parâmetros:

1º) Valor do capital
2º) Taxa de juros
3º) Montante

Com estas informações e uma fórmula bem aplicada,
já somos capazes de descobrir o tempo em que
um certo Capital, aplicado com tal Taxa de Juros,
alcance o determinado Montante.
Mas não foi fácil criar esta fórmula.
Quando aprendemos a resolver isto no papel,
usávamos logaritmo para complementar a fórmula,
e agora quando fui passar a fórmula para
linguagem de programação, fui por um caminho
complicado e duvidoso, mas pesquisando sobre
a biblioteca math.h do C, encontrei a função  log ();
e com esta solução o código ficou resumido e funcional.

Assista o programa funcionando no vídeo abaixo:



Veja abaixo a principal função do programa:

//---------------------------------------------------------------------------
void __fastcall TForm5::SpeedButton2Click ( TObject *Sender ) {
              double Capital = 0.00, Juros, Tx_juros = 0.00, Tempo = 0.00, Montante, Aux = 0.00;

              Capital  = StrToFloat ( Edit1 -> Text );
              Tx_juros = StrToFloat ( Edit2 -> Text );
              Montante = StrToFloat ( Edit3 -> Text );

              Montante = Montante / Capital;
              Tx_juros = Tx_juros / 100;
              Aux = ( 1 + Tx_juros );

              Montante = log10 ( Montante );
              Aux = log10 ( Aux );

              Montante =  Montante / Aux;
              Montante = Montante * 12;

              Label1 -> Caption = FloatToStrF ( Montante , ffFixed, 8, 2 );
              Label1 -> Caption = Label1 -> Caption + " Meses";

              //Informe ( Sender );
 }
//---------------------------------------------------------------------------

Juros compostos - pesquisando a taxa de juros

Para pesquisar a taxa de juros em uma aplicação
de juros compostos, precisamos entrar com três parâmetros:
1º) Valor do capital
2º) Valor do juros
3º) Tempo - que em nosso caso estamos entrando
com o tempo em meses, que pode ser facilmente
convertido para bimestre, trimestre, semestre ou ano.
Assista o programa funcionando no vídeo abaixo:



Abaixo a principal função do programa:

//---------------------------------------------------------------------------
void __fastcall TForm4::SpeedButton2Click ( TObject *Sender ) {
              double Capital = 0.00, Val_juros = 0.00, Tx_juros = 0.00, Tempo = 0.00, Montante;

              Capital = StrToFloat ( Edit1 -> Text );
              Val_juros = StrToFloat ( Edit2 -> Text );
              Tempo = StrToFloat ( Edit3 -> Text );

              Tempo = Tempo / 12;

              Montante = Capital + Val_juros;
              Montante = Montante / Capital;
              Montante = pow ( Montante, 1.0 / Tempo );
              Montante = Montante - 1;
              Montante = Montante * 100;
              Label1 -> Caption = FloatToStrF ( Montante , ffFixed, 8, 2 );
              Label1 -> Caption = Label1 -> Caption + " %";

              Informe ( Sender );
}
//---------------------------------------------------------------------------

Juros compostos - pesquisando o valor do juros

Para calcular o valor do juros em uma aplicação
de juros compostos, precisamos entrar com três parâmetros:


1º) Valor do capital
2º) Taxa de juros
3º) Tempo - que em nosso caso estamos entrando
com o tempo em meses, que pode ser facilmente
convertido para bimestre, trimestre, semestre ou ano.


Assista o programa funcionando no vídeo abaixo:





Veja abaixo a principal função do programa:

//---------------------------------------------------------------------------
void __fastcall TForm4::SpeedButton2Click ( TObject *Sender ) {
             double Capital = 0.00, Juros, Tx_juros = 0.00,
             Val_juros = 0.00, Tempo = 0.00, Montante;

              Capital = StrToFloat ( Edit1 -> Text );
              Tx_juros = StrToFloat ( Edit2 -> Text );
              Tempo = StrToFloat ( Edit3 -> Text );

              Tx_juros = Tx_juros / 100;
              Tx_juros = Tx_juros + ( 1 );
              Tempo = Tempo / 12;

              Montante = pow ( Tx_juros, Tempo );
              Montante = Montante * Capital;
              Val_juros = Montante - Capital;
              Label1 -> Caption = FloatToStrF ( Val_juros, ffFixed, 8, 2 );

              //Informe ( Sender );
}
//---------------------------------------------------------------------------

Juros compostos - pesquisando o valor do montante

Juros compostos são muito utilizados
em transações comerciais e financeiras
em todas as partes do mundo.
Compra de casas ou apartamentos a longo prazo,
investimentos em conta bancárias,
aquisição de empréstimos consignados,
e outros negócios onde estes cálculos são necessários.
Revendo nossos conhecimentos em matemática financeira,
criamos quatro programas baseado em juros compostos,
que são os seguintes: Pesquisando o valor do montante,
que é este agora apresentado,
Pesquisando o valor do juros,
Pesquisando a taxa de juros, e,
Pesquisando tempo necessário.
Acompanhem seu funcionamento no vídeo abaixo:





Veja abaixo a principal função do código:

//---------------------------------------------------------------------------
void __fastcall TForm2::SpeedButton2Click ( TObject *Sender ) {

             double Capital = 0.00, Juros, Tx_juros = 0.00, Tempo = 0.00, Montante;

              Capital = StrToFloat ( Edit1 -> Text );
              Tx_juros = StrToFloat ( Edit2 -> Text );
              Tempo = StrToFloat ( Edit3 -> Text );

              Tx_juros = Tx_juros / 100;
              Tx_juros = Tx_juros + ( 1 );
              Tempo = Tempo / 12;

              Montante = pow ( Tx_juros, Tempo );
              Montante = Montante * Capital;

             Label1 -> Caption = FloatToStrF ( Montante , ffFixed, 8, 2 );

             //Informe ( Sender );
}
//---------------------------------------------------------------------------