sábado, 25 de abril de 2020

C++ Builder - Calculando o volume de uma esfera

É impressionante a utilidade de corpos esféricos nos dias atuais.
Podemos citar algumas, mas seria impossível destacar
todos os locais onde ela se encontra.
Na maioria dos melhores esportes, na indústria,
nas máquinas, nos veículos motorizados, etc...
Por ser tão utilizadas, foi necessário a criação
de fórmulas matemáticas para cálculos precisos
de seu volume, área, circunferência e diâmetro.
Sabendo-se o valor do raio que é a distância entre
o centro da esfera e sua extremidade e o valor constante
do número irracional π (pi), dado por aproximadamente 3,14,
já somos capazes de criar um programa capaz de calcular
o seu volume com boa precisão e com certa facilidade.
E como estou usando o C++ Builder, aproveitei uma variável
nativa contida na Vcl, própria para cálculos matemáticos,
empregados em programação, e os resultados são
apresentados abaixo num vídeo com o programa funcionando.





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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
    : TForm ( Owner )
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
    Edit1 -> Font -> Name = "alarm clock";
    Edit1 -> SetFocus ( );
    Edit1 -> Font -> Color = clBlack;
    Edit1 -> Color = clOlive;
    Edit1 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit1 -> Font -> Size = 12;
    Edit1 -> Width = 105;
    Edit1 -> Height = 21;
    Edit1 -> Left = 170;
    Edit1 -> Top = 68;
    Edit1 -> Alignment = taRightJustify; //Orientação à direita do TEdit

    Image1 -> Width = 600;
    Image1 -> Height = 300;
    Image1 -> Left = 0;
    Image1 -> Top = 0;

    Image2 -> Width = 130;
    Image2 -> Height = 35;
    Image2 -> Left = 280;
    Image2 -> Top = 62;

    Image3 -> Width = 130;
    Image3 -> Height = 35;
    Image3 -> Left = 280;
    Image3 -> Top = 62;

    Image4 -> Width = 600;
    Image4 -> Height = 300;
    Image4 -> Left = 0;
    Image4 -> Top = 0;

    Image5 -> Width = 130;
    Image5 -> Height = 40;
    Image5 -> Left = 280;
    Image5 -> Top = 50;

    Image6 -> Width = 130;
    Image6 -> Height = 40;
    Image6 -> Left = 280;
    Image6 -> Top = 50;

    Label1 -> Visible = true;
    Label1 -> Font -> Size = 12;
    Label1 -> Font -> Name = "Venacti";
    Label1 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label1 -> Font -> Color = clBlack;
    Label1 -> Width = 130;
    Label1 -> Height = 20;
    Label1 -> Left = 270;
    Label1 -> Top = 110;

    Label2 -> Visible = true;
    Label2 -> Font -> Size = 12;
    Label2 -> Font -> Name = "Venacti";
    Label2 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label2 -> Font -> Color = clBlack;
    Label2 -> Width = 130;
    Label2 -> Height = 20;
    Label2 -> Left = 270;
    Label2 -> Top = 157;

    Label3 -> Visible = true;
    Label3 -> Font -> Size = 12;
    Label3 -> Font -> Name = "Venacti";
    Label3 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label3 -> Font -> Color = clBlack;
    Label3 -> Width = 330;
    Label3 -> Height = 20;
    Label3 -> Left = 270;
    Label3 -> Top = 204;

    Label4 -> Visible = true;
    Label4 -> Font -> Size = 12;
    Label4 -> Font -> Name = "Venacti";
    Label4 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label4 -> Font -> Color = clBlack;
    Label4 -> Width = 330;
    Label4 -> Height = 20;
    Label4 -> Left = 270;
    Label4 -> Top = 249;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender) {
       Label_Manual ( Sender );
       Image4 -> Visible = false;
       Image6 -> Visible = false;
       Image5 -> Visible = false;

       Edit1 -> Height = 21;

       Label1 -> Visible = false;
       Label2 -> Visible = false;
       Label3 -> Visible = false;
       Label4 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2MouseEnter ( TObject *Sender ) {
        Image3 -> Visible = true;
        Image2 -> Visible = false;
        Image5 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image3MouseLeave ( TObject *Sender ) {
        Image3 -> Visible = false;
        Image2 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image3Click ( TObject *Sender ) {
       double Raio, Dim, Circumferencia, Area, Volume;

       Raio = Edit1 -> Text.ToDouble ( );
       Dim = Raio * 2;
       Circumferencia = Raio * 2 * M_PI;
       Area = Raio * Raio * M_PI;
       Volume = Raio * Raio * Raio * 4.00 * M_PI / 3;

       Label1 -> Visible = true;
       Label2 -> Visible = true;
       Label3 -> Visible = true;
       Label4 -> Visible = true;

       Label1 -> Caption = FloatToStrF ( Dim, ffFixed, 8, 2 );
       Label2 -> Caption = FloatToStrF ( Circumferencia, ffFixed, 8, 2 );
       Label3 -> Caption = FloatToStrF ( Area, ffFixed, 8, 2 );
       Label4 -> Caption = FloatToStrF ( Volume, ffFixed, 8, 2 );

       Image1 -> Visible = false;
       Image2 -> Visible = false;
       Image3 -> Visible = true;
       Image4 -> Visible = true;
       Image6 -> Visible = true;
       Image5 -> Visible = true;
       Edit1 -> Top = 63;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image5MouseEnter ( TObject *Sender ) {
    Image5 -> Visible = false;
    Image6 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image6MouseLeave ( TObject *Sender ) {
    Image6 -> Visible = false;
    Image5 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image6Click ( TObject *Sender ) {
         Image1 -> Visible = true;
         Image2 -> Visible = true;
         Image3 -> Visible = false;
         Image4 -> Visible = false;
         Image5 -> Visible = false;
         Image6 -> Visible = false;

         Edit1 -> Top = 68;
         Edit1 -> Clear ( );

         Label1 -> Caption = " ";
         Label2 -> Caption = " ";
         Label3 -> Caption = " ";
         Label4 -> Caption = " ";
}
//---------------------------------------------------------------------------


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

#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.Graphics.hpp>
#include <Vcl.Imaging.pngimage.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TImage *Image1;
    TEdit *Edit1;
    TImage *Image2;
    TImage *Image3;
    TImage *Image4;
    TImage *Image5;
    TImage *Image6;
    TLabel *Label1;
    TLabel *Label2;
    TLabel *Label3;
    TLabel *Label4;
    void __fastcall Label_Manual ( TObject *Sender );
    void __fastcall FormShow ( TObject *Sender );
    void __fastcall Image2MouseEnter ( TObject *Sender );
    void __fastcall Image3MouseLeave ( TObject *Sender );
    void __fastcall Image3Click ( TObject *Sender );
    void __fastcall Image5MouseEnter ( TObject *Sender );
    void __fastcall Image6MouseLeave ( TObject *Sender );
    void __fastcall Image6Click ( TObject *Sender );
private:    // User declarations
public:        // User declarations
    __fastcall TForm1 ( TComponent* Owner );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

sexta-feira, 24 de abril de 2020

C++ Builder - Cálculo de variação percentual

Quando queremos descobrir a diferença entre dois valores,
usamos um pequeno cálculo matemático numa fórmula aplicada.
se dois valores são comparados utilizando a fórmula abaixo:
Val2 - Val1 / Val1 x 100, podemos dizer que a relação
entre os dois é a variação percentual entre eles.
Se o resultado destas comparações forem positivos,
na matemática dizemos que houve um aumento percentual,
e se for negativo uma diminuição negativa foi constatada,
e para deixar isto mais claro criamos este programa
no C++ Builder para cálculos destas variações,
acompanhem no vídeo, e se tiver interesse clique
no link e confira como isto foi feito.





//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
    : TForm ( Owner ) {
}
int x = 0;
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
    Edit1 -> Font -> Name = "alarm clock";
    Edit1 -> SetFocus ( );
    Edit1 -> Font -> Color = clBlack;
    Edit1 -> Color = clOlive;
    Edit1 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit1 -> Font -> Size = 12;
    Edit1 -> Width = 105;
    Edit1 -> Height = 21;
    Edit1 -> Left = 272;
    Edit1 -> Top = 70;
    Edit1 -> Alignment = taRightJustify; //Orientação à direita do TEdit

    Edit2 -> Font -> Name = "alarm clock";
    Edit2 -> Font -> Color = clBlack;
    Edit2 -> Color = clOlive;
    Edit2 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit2 -> Font -> Size = 12;
    Edit2 -> Width = 105;
    Edit2 -> Height = 21;
    Edit2 -> Left = 272;
    Edit2 -> Top = 98;
    Edit2 -> Alignment = taRightJustify; //Orientação à direita do TEdit

    Image1 -> Visible = true;
    Image1 -> Width = 600;
    Image1 -> Height = 300;
    Image1 -> Left = 0;
    Image1 -> Top = 0;

    Image2 -> Visible = true;
    Image2 -> Width = 130;
    Image2 -> Height = 35;
    Image2 -> Left = 430;
    Image2 -> Top = 80;

    Image3 -> Width = 600;
    Image3 -> Height = 300;
    Image3 -> Left = 0;
    Image3 -> Top = 0;

    Image4 -> Width = 160;
    Image4 -> Height = 36;
    Image4 -> Left = 430;
    Image4 -> Top = 80;

    Label1 -> Visible = true;
    Label1 -> Font -> Size = 12;
    Label1 -> Font -> Name = "Venacti";
    Label1 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label1 -> Font -> Color = clBlack;
    Label1 -> Width = 130;
    Label1 -> Height = 20;
    Label1 -> Left = 380;
    Label1 -> Top = 150;

    Label2 -> Visible = true;
    Label2 -> Font -> Size = 12;
    Label2 -> Font -> Name = "Venacti";
    Label2 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label2 -> Font -> Color = clBlack;
    Label2 -> Width = 130;
    Label2 -> Height = 20;
    Label2 -> Left = 380;
    Label2 -> Top = 185;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2Click ( TObject *Sender ) {
       double Valor_Antigo = 0.00, Valor_Novo = 0.00, Valor_Resp;
       double Dif_Percent;

       Valor_Antigo = StrToFloat ( Edit1 -> Text );
       Valor_Novo = StrToFloat ( Edit2 -> Text );
       Valor_Resp = ( Valor_Novo - Valor_Antigo );

       Dif_Percent = ( Valor_Resp / Valor_Antigo );
       Dif_Percent = ( Dif_Percent * 100 );

       Label1 -> Caption = FloatToStrF ( Valor_Resp, ffFixed, 8, 3 );
       Label2 -> Caption = FloatToStrF ( Dif_Percent, ffFixed, 8, 2 );
       Label2 -> Caption = Label2 -> Caption + " %";

        Label_Manual ( Sender );
        Image3 -> Visible = true;
        Image4 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image4Click ( TObject *Sender ) {
        Label_Manual ( Sender );
        Image3 -> Visible = false;
        Image4 -> Visible = false;
        Label1 -> Caption = " ";
        Label2 -> Caption = " ";
        Edit1 -> Clear ( );
        Edit2 -> Clear ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
    Label_Manual ( Sender );
    Image1 -> Visible = true;
    Image2 -> Visible = true;
    Image3 -> Visible = false;
    Image4 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender ) {
 Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
 


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Buttons.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Vcl.Imaging.GIFImg.hpp>
#include <Vcl.Graphics.hpp>
#include <Vcl.Imaging.pngimage.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TImage *Image1;
    TImage *Image2;
    TImage *Image3;
    TImage *Image4;
    TLabel *Label1;
    TLabel *Label2;
    TEdit *Edit1;
    TEdit *Edit2;
    void __fastcall Label_Manual ( TObject *Sender );
    void __fastcall Image2Click(TObject *Sender);
    void __fastcall Image4Click(TObject *Sender);
    void __fastcall FormShow(TObject *Sender);
    void __fastcall FormPaint(TObject *Sender);

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

domingo, 19 de abril de 2020

C++ Builder - calculando o volume do cilindro

Um corpo redondo, sólido e de geometria tridimensional,
é denominado cilindro.
O cilindro possui um volume que determina a sua capacidade,
e a sua base é de formato circular.
Para encontrar a área da base temos que calcular
a área de uma circunferência, já que a base de um cilindro
é circular como já foi citado acima. e sua fórmula

para cálculo da área da base é:

Ab = π . r²

    Ab: é a área da base;
    π: é o número pi (3,14);
    r: é o raio da base;

E para calcular seu volume,
basta multiplicar sua área base pela altura,
e sua fórmula é apresentada abaixo:

V = Ab . h = π . r² . h

Pronto, isto é tudo o que precisávamos saber
para criar um programa capaz de calcular
o volume de um cilindro, acompanhem no vídeo abaixo:





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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
    : TForm ( Owner )
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
    Edit1 -> Visible = true;
    Edit1 -> Font -> Name = "alarm clock";
    Edit1 -> SetFocus ( );
    Edit1 -> Clear ( );
    Edit1 -> Font -> Color = clBlack;
    Edit1 -> Color = clOlive;
    Edit1 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit1 -> Font -> Size = 12;
    Edit1 -> Width = 90;
    Edit1 -> Height = 20;
    Edit1 -> Left = 270;
    Edit1 -> Top = 48;

    Edit2 -> Visible = true;
    Edit2 -> Font -> Name = "alarm clock";
    Edit2 -> Clear ( );
    Edit2 -> Font -> Color = clBlack;
    Edit2 -> Color = clOlive;
    Edit2 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit2 -> Font -> Size = 12;
    Edit2 -> Width = 90;
    Edit2 -> Height = 20;
    Edit2 -> Left = 270;
    Edit2 -> Top = 78;

    BitBtn1 -> Font -> Name = "Venacti";
    BitBtn1 -> Visible = true;
    BitBtn1 -> Font -> Size = 12;
    BitBtn1    -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    BitBtn1 -> Font -> Color = clBlack;
    BitBtn1     -> Width = 210;
    BitBtn1     -> Height = 22;
    BitBtn1 -> Top = 110;
    BitBtn1 -> Left = 152;
    BitBtn1 -> Caption = ( "Calcular volume do cilindro" );

    BitBtn2 -> Font -> Name = "Venacti";
    BitBtn2 -> Font -> Size = 12;
    BitBtn2 -> Font -> Color = clBlack;
    BitBtn2    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn2 -> Top = 230;
    BitBtn2 -> Left = 152;
    BitBtn2 -> Height = 21;
    BitBtn2 -> Width = 110;
    BitBtn2 -> Caption = ( "Novo cálculo" );

    BitBtn3 -> Font -> Name = "Venacti";
    BitBtn3 -> Font -> Size = 12;
    BitBtn3 -> Font -> Color = clBlack;
    BitBtn3    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn3 -> Top = 230;
    BitBtn3 -> Left = 430;
    BitBtn3 -> Height = 21;
    BitBtn3 -> Width = 140;
    BitBtn3 -> Caption = ( "Sair do programa" );

       Label1 -> Visible = true;
    Label1 -> Font -> Size = 12;
    Label1 -> Font -> Name = "Arial";
    Label1 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label1 -> Font -> Color = clRed;
    Label1 -> Width = 130;
    Label1 -> Height = 20;
    Label1 -> Left = 270;
    Label1 -> Top = 110;

    Label2 -> Visible = true;
    Label2 -> Font -> Size = 12;
    Label2 -> Font -> Name = "Arial";
    Label2 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label2 -> Font -> Color = clRed;
    Label2 -> Width = 130;
    Label2 -> Height = 20;
    Label2 -> Left = 270;
    Label2 -> Top = 140;

    Label3 -> Visible = true;
    Label3 -> Font -> Size = 11;
    Label3 -> Font -> Name = "Arial";
    Label3 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label3 -> Font -> Color = clRed;
    Label3 -> Width = 330;
    Label3 -> Height = 20;
    Label3 -> Left = 270;
    Label3 -> Top = 170;

    Label4 -> Visible = true;
    Label4 -> Font -> Size = 11;
    Label4 -> Font -> Name = "Arial";
    Label4 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label4 -> Font -> Color = clRed;
    Label4 -> Width = 330;
    Label4 -> Height = 20;
    Label4 -> Left = 270;
    Label4 -> Top = 200;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
       Label_Manual ( Sender );

       Image1 -> Visible = true;
       Image2 -> Visible = false;

       BitBtn2 -> Visible = false;
       BitBtn3 -> Visible = false;

       Label1 -> Visible = false;
       Label2 -> Visible = false;
       Label3 -> Visible = false;
       Label4 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender ) {
       double Radius = 0.00, Height = 0.00;
       double BaseArea, LateralArea, TotalArea, Volume;

       Radius = StrToFloat ( Edit1 -> Text );
       Height = StrToFloat ( Edit2 -> Text );

       BaseArea = Radius * Radius * M_PI;
       LateralArea = Radius * Height * 2 * M_PI;
       TotalArea = BaseArea + LateralArea;
       Volume = BaseArea * Height;

       Label1 -> Caption = FloatToStrF ( BaseArea, ffFixed, 8, 6 );
       Label2 -> Caption = FloatToStrF ( LateralArea, ffFixed, 8, 6 );
       Label3 -> Caption = FloatToStrF ( TotalArea, ffFixed, 8, 6 );
       Label4 -> Caption = FloatToStrF ( Volume, ffFixed, 8, 6 );

       Constraints -> MinHeight = 345;
       Constraints -> MinWidth = 600;
       Constraints -> MaxHeight = 345;
       Constraints -> MaxWidth = 600;

       Image1 -> Visible = false;
       Image2 -> Visible = true;

       BitBtn1 -> Visible = false;
       BitBtn2 -> Visible = true;
       BitBtn3 -> Visible = true;

       Label1 -> Visible = true;
       Label2 -> Visible = true;
       Label3 -> Visible = true;
       Label4 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click ( TObject *Sender ) {
         Label_Manual ( Sender );

         Label1 -> Caption = " ";
         Label2 -> Caption = " ";
         Label3 -> Caption = " ";
         Label4 -> Caption = " ";

         Edit1 -> Clear ( );
         Edit2 -> Clear ( );

         Image1 -> Visible = true;
         Image2 -> Visible = false;

         BitBtn2 -> Visible = false;
         BitBtn3 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click ( 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.ExtCtrls.hpp>
#include <Vcl.Graphics.hpp>
#include <Vcl.Imaging.pngimage.hpp>
#include <Vcl.Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TImage *Image1;
    TImage *Image2;
    TBitBtn *BitBtn1;
    TBitBtn *BitBtn2;
    TBitBtn *BitBtn3;
    TEdit *Edit1;
    TEdit *Edit2;
    TLabel *Label1;
    TLabel *Label2;
    TLabel *Label3;
    TLabel *Label4;

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




sábado, 18 de abril de 2020

C++ Builder - Divisores de um número

Exceto os números primos, todos os demais,
possuem múltiplos e divisores.
Um número é divisor de outro quando o resto da
divisão for igual a 0. Portanto:

12 é divisível por 1, 2, 3, 4, 6 e 12.
36 é divisível por 1, 2, 3, 4, 6, 9, 12, 18 e 36.
48 é divisível por 1, 2, 3, 4, 6, 8, 12, 24 e 48.

Observações importantes:

1 - O menor divisor natural de um número é sempre o número 1.
2 - O maior divisor de um número é o próprio número.
3 - O zero não é divisor de nenhum número.
4 - Os divisores de um número formam um conjunto finito.

Alguns números têm apenas dois divisores: o 1 e ele mesmo,
esses números são chamados de primos.
Isto já é suficiente para o que queremos fazer,
mas algumas condições foram necessárias para que
o programa funcionasse corretamente, confira no vídeo abaixo:






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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString str_1;
int a, i, j, l, primo, n_1, n_2;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
    Edit1 -> Visible = true;
    Edit1 -> Font -> Name = "alarm clock";
    Edit1 -> SetFocus ( );
    Edit1 -> Clear ( );
    Edit1 -> Font -> Color = clBlack;
    Edit1 -> Color = clOlive;
    Edit1 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit1 -> Font -> Size = 12;
    Edit1 -> Width = 90;
    Edit1 -> Height = 20;
    Edit1 -> Left = 332;
    Edit1 -> Top = 65;

    Edit2 -> Visible = true;
    Edit2 -> Font -> Name = "alarm clock";
    Edit2 -> Clear ( );
    Edit2 -> Font -> Color = clBlack;
    Edit2 -> Color = clOlive;
    Edit2 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit2 -> Font -> Size = 12;
    Edit2 -> Width = 90;
    Edit2 -> Height = 20;
    Edit2 -> Left = 332;
    Edit2 -> Top = 95;

    BitBtn1 -> Font -> Name = "Venacti";
    BitBtn1 -> Visible = true;
    BitBtn1 -> Font -> Size = 12;
    BitBtn1    -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    BitBtn1 -> Font -> Color = clBlack;
    BitBtn1     -> Width = 130;
    BitBtn1     -> Height = 23;
    BitBtn1 -> Top = 130;
    BitBtn1 -> Left = 170;
    BitBtn1 -> Caption = ( "Gerar divisores" );

    BitBtn2 -> Font -> Name = "Venacti";
    BitBtn2 -> Font -> Size = 12;
    BitBtn2 -> Font -> Color = clBlack;
    BitBtn2    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn2 -> Top = 270;
    BitBtn2 -> Left = 230;
    BitBtn2 -> Height = 21;
    BitBtn2 -> Width = 140;
    BitBtn2 -> Caption = ( "Nova operação" );

    BitBtn3 -> Font -> Name = "Venacti";
    BitBtn3 -> Font -> Size = 12;
    BitBtn3 -> Font -> Color = clBlack;
    BitBtn3    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn3 -> Top = 300;
    BitBtn3 -> Left = 230;
    BitBtn3 -> Height = 21;
    BitBtn3 -> Width = 140;
    BitBtn3 -> Caption = ( "Sair do programa" );

    RichEdit1 -> Lines -> Clear ( );
    RichEdit1 -> Font -> Name = "Garamond";
    RichEdit1 -> Font -> Size = 14;
    RichEdit1 -> Font -> Color = clBlack;
    RichEdit1 -> Color = clTeal;
    //RichEdit1 -> Color = ( RGB ( 128, 128, 0 ) );
    RichEdit1 -> Font -> Style = TFontStyles ( ) << fsBold;
    RichEdit1 -> BevelWidth = 3;
    //RichEdit1 -> BorderStyle = bsNone;
    RichEdit1 -> BorderWidth = 5;
    RichEdit1 -> Width = 400;
    RichEdit1 -> Height = 220;
    RichEdit1 -> Left = 100;
    RichEdit1 -> Top = 40;
    RichEdit1 -> ScrollBars = ssVertical;

    Label1 -> Font -> Size = 14;
    Label1 -> Font -> Name = "CactusSSK";
    Label1 -> Font -> Color = clBlack;
    Label1 -> AutoSize = true;

    SpeedButton1 -> Width = 205;
    SpeedButton1 -> Height = 43;
    SpeedButton1 -> Left = 210;
    SpeedButton1 -> Top = 345;

     Shape1 -> Visible = false;
     Shape1 -> Width = 404;
     Shape1 -> Height = 224;
     Shape1 -> Left = 98;
     Shape1 -> Top = 38;
     Shape1 -> Brush -> Style = bsClear;
     Shape1 -> Pen -> Color = clBlack;
     Shape1 -> Pen -> Width = 5;
     Shape1 -> Shape = stRectangle;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
    Label_Manual ( Sender );
    n_1 = 0;
    n_2 = 0;
    a = 0;
    i = 0;
    Shape1 -> Visible = false;
    Label1 -> Visible = false;
    RichEdit1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
    SpeedButton1 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender ) {
    n_1 = StrToInt ( Edit1 -> Text );
    n_2 = StrToInt ( Edit2 -> Text );

    if ( n_1 == n_2 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 80;
    Label1 -> Top = 360;
    Label1 -> Caption = "OS DOIS VALORES NÃO PODEM SER IGUAIS";
    i = 0;
    }

    if ( n_1 > n_2 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 30;
    Label1 -> Top = 360;
    Label1 -> Caption = "VALOR INICIAL MAIOR QUE O VALOR À PESQUISAR";
    i = 0;
    }

    if ( n_1 < 2 || n_2 < 2 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 40;
    Label1 -> Top = 360;
    Label1 -> Caption = "VALORES MENORES QUE DOIS NÃO SÃO ACEITOS";
    i = 0;
    }

       if ( ( n_1 == n_2 ) || ( n_1 > n_2 ) || ( n_1 < 2 || 2 > n_2 ) ) {
       }else{
        Shape1 -> Visible = true;
        Label1 -> Left = 100;
        Label1 -> Visible = true;
        RichEdit1 -> Visible = true;
        Edit1  -> Visible = false;
        Edit2  -> Visible = false;
        BitBtn1-> Visible = false;
        BitBtn2 -> Visible = true;
        BitBtn3 -> Visible = true;

    for ( a = n_1; a <= n_2; a++ ) {
        int ePrimo = 1;
        if (  n_2 % a == 0 ) {
           ePrimo = 0;
        }
        if ( ePrimo == 0 ) {
            i++;
            str_1 += "  ";
            if ( a >= 0 && a < 10 )
                str_1 += ( "000" );
            if ( a  >= 10 && a < 100 )
                str_1 += ( "00" );
            if ( a >= 100 && a < 1000 )
                str_1 += ( "0" );
            str_1 += a;
        }
    }

    //RichEdit1 -> Text = str_1;
    RichEdit1 -> Lines -> Add ( str_1 );
    //Concatenando Label
    Label1 -> Top = 365;
    Label1 -> Caption = "Entre ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( n_1 );
    Label1 -> Caption = Label1 -> Caption + " e ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( n_2 );
    Label1 -> Caption = Label1 -> Caption + " existem ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( i );
    Label1 -> Caption = Label1 -> Caption + " divisores de ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( n_2 );
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label1Click ( TObject *Sender ) {
    Label1 -> Visible = false;
    n_1 = 0;
    n_2 = 0;
    Edit1 -> Clear ( );
    Edit2 -> Clear ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click ( TObject *Sender ) {
   l++;
  if ( l == 1 ) {
    SpeedButton1 -> Visible = true;
    Label1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click ( TObject *Sender ) {
    n_1 = 0;
    n_2 = 0;
    a = 0;
    i = 0;
    str_1 = " ";
    Label1 -> Caption = " ";
    RichEdit1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
    Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click ( TObject *Sender ){
  exit ( 0 );
}
//---------------------------------------------------------------------------
 


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

#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.Graphics.hpp>
#include <Vcl.Buttons.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TRichEdit *RichEdit1;
    TEdit *Edit1;
    TEdit *Edit2;
    TBitBtn *BitBtn1;
    TBitBtn *BitBtn2;
    TBitBtn *BitBtn3;
    TImage *Image1;
    TLabel *Label1;
    TSpeedButton *SpeedButton1;
    TShape *Shape1;
    void __fastcall Label_Manual ( TObject *Sender );
    void __fastcall Label1Click(TObject *Sender);
    void __fastcall SpeedButton1Click(TObject *Sender);
    void __fastcall BitBtn3Click(TObject *Sender);
    void __fastcall BitBtn2Click(TObject *Sender);
    void __fastcall BitBtn1Click(TObject *Sender);
    void __fastcall FormShow(TObject *Sender);
private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

quinta-feira, 16 de abril de 2020

C++ Builder - Múltiplos entre dois valores

Entendemos que múltiplos de um número natural,
está relacionado ao conjunto dos números inteiros.
Quando tratamos do assunto múltiplos referimo-nos,
a conjuntos numéricos que exigem algumas condições.
Qualquer número multiplicado pela sequência
dos números naturais, obtém - se os seus múltiplos.
Veja abaixo a sequência dos números múltiplos de 2 até 5:

2 x 0 = 0
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10

Agora que sabemos a lógica de partida podemos aplicá-la
em qualquer linguagem de programação,
Mas algumas condições importantes devem ser observadas,
segue algumas para que nosso programa tenha sucesso:

1ª) O número inicial sempre tem que ser maior que um.
2ª) O número final nunca pode ser menor que o número inicial.
3ª) O número à pesquisar tem que ser maior que um,
pois não faz sentido procurar os múltiplos de um.
4ª) O número à pesquisar tem que ser menor que o número final.
5ª) O número inicial não pode ser igual ao número final.


Aplicando estas condições simples teremos nosso programa
funcionando corretamente, assista o vídeo abaixo:





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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString str_1;
int a, i, j, l, primo, n_1, n_2, n_3;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
    Edit1 -> Visible = true;
    Edit1 -> SetFocus ( );
    Edit1 -> Clear ( );
    Edit1 -> Font -> Color = clRed;
    Edit1 -> Font-> Style = TFontStyles ( ) << fsItalic;
    Edit1 -> Font -> Size = 11;
    Edit1 -> Width = 90;
    Edit1 -> Height = 20;
    Edit1 -> Left = 250;
    Edit1 -> Top = 80;

    Edit2 -> Visible = true;
    Edit2 -> Clear ( );
    Edit2 -> Font -> Color = clRed;
    Edit2 -> Font-> Style = TFontStyles ( ) << fsItalic;
    Edit2 -> Font -> Size = 11;
    Edit2 -> Width = 90;
    Edit2 -> Height = 20;
    Edit2 -> Left = 250;
    Edit2 -> Top = 145;

    Edit3 -> Visible = true;
    Edit3 -> Clear ( );
    Edit3 -> Font -> Color = clRed;
    Edit3 -> Font-> Style = TFontStyles ( ) << fsItalic;
    Edit3 -> Font -> Size = 11;
    Edit3 -> Width = 90;
    Edit3 -> Height = 20;
    Edit3 -> Left = 250;
    Edit3 -> Top = 210;

    BitBtn1 -> Font -> Name = "Bauhaus 93";
    BitBtn1 -> Visible = true;
    BitBtn1 -> Font -> Size = 12;
    BitBtn1 -> Font -> Color = clBlack;
    BitBtn1     -> Width = 90;
    BitBtn1     -> Height = 23;
    BitBtn1 -> Top = 250;
    BitBtn1 -> Left = 250;
    BitBtn1 -> Caption = ( "Pesquisar" );

    BitBtn2 -> Font -> Name = "Bauhaus 93";
    BitBtn2 -> Font -> Size = 12;
    BitBtn2 -> Font -> Color = clBlack;
    BitBtn2    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn2 -> Top = 270;
    BitBtn2 -> Left = 230;
    BitBtn2 -> Height = 21;
    BitBtn2 -> Width = 140;
    BitBtn2 -> Caption = ( "Nova operação" );

    BitBtn3 -> Font -> Name = "Bauhaus 93";
    BitBtn3 -> Font -> Size = 12;
    BitBtn3 -> Font -> Color = clBlack;
    BitBtn3    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn3 -> Top = 300;
    BitBtn3 -> Left = 230;
    BitBtn3 -> Height = 21;
    BitBtn3 -> Width = 140;
    BitBtn3 -> Caption = ( "Sair do programa" );

    RichEdit1 -> Lines -> Clear ( );
    RichEdit1 -> Font -> Name = "Arial";
    RichEdit1 -> Font -> Size = 12;
    //RichEdit1 -> Color = ( RGB ( 84,247,142 ) );
    RichEdit1 -> Font -> Color = clBlack;
    RichEdit1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
    RichEdit1 -> BevelWidth = 3;
    //RichEdit1 -> BorderStyle = bsNone;
    RichEdit1 -> BorderWidth = 5;
    RichEdit1 -> Width = 400;
    RichEdit1 -> Height = 220;
    RichEdit1 -> Left = 100;
    RichEdit1 -> Top = 40;
    RichEdit1 -> ScrollBars = ssVertical;

    Label1 -> Visible = true;
    Label1 -> Font -> Size = 16;
    Label1 -> Font -> Name = "Bauhaus 93";
    Label1 -> Font -> Color = clBlack;
    Label1 -> AutoSize = true;

    SpeedButton1 -> Width = 205;
    SpeedButton1 -> Height = 50;
    SpeedButton1 -> Left = 210;
    SpeedButton1 -> Top = 345;

     Shape1 -> Visible = false;
     Shape1 -> Width = 404;
     Shape1 -> Height = 224;
     Shape1 -> Left = 98;
     Shape1 -> Top = 38;
     Shape1 -> Brush -> Style = bsClear;
     Shape1 -> Pen -> Color = clBlack;
     Shape1 -> Pen -> Width = 5;
     Shape1 -> Shape = stRectangle;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
    Label_Manual ( Sender );
    n_1 = 0;
    n_2 = 0;
    n_3 = 0;
    a = 0;
    i = 0;
    Shape1 -> Visible = false;
    Label1 -> Visible = false;
    RichEdit1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
    SpeedButton1 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender ) {
    n_1 = StrToInt ( Edit1 -> Text );
    n_2 = StrToInt ( Edit2 -> Text );
    n_3 = StrToInt ( Edit3 -> Text );
    int tam, tot;
    tam = n_2 - n_1;

    if ( n_1 == n_2 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 100;
    Label1 -> Top = 360;
    Label1 -> Caption = "O número inicial não pode ser igual ao final";
    i = 0;
    }

    if ( n_1 > n_2 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 100;
    Label1 -> Top = 360;
    Label1 -> Caption = "O número inicial não pode ser maior que o final";
    i = 0;
    }

    if ( n_1 < 2 || 2 > n_2 || 2 > n_3 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 150;
    Label1 -> Top = 350;
    Label1 -> Caption = "Números menores que 2 não são aceitos";
    i = 0;
    }

    if ( n_3 < 2 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 110;
    Label1 -> Top = 350;
    Label1 -> Caption = "O valor à pesquisar tem que ser maior que um";
    i = 0;
    }

    if ( n_3 > n_2 ) {
    Label1 -> Visible = true;
    Label1 -> Left = 70;
    Label1 -> Top = 350;
    Label1 -> Caption = "O valor à pesquisar não pode ser maior que o final";
    i = 0;
    }
       if ( ( n_1 == n_2 ) || ( n_1 > n_2 ) || ( n_1 < 2 || 2 > n_2 || 2 > n_3 )
        || ( n_3 < 2 ) || ( n_3 > n_2 ) ) {
       }else{
        Shape1 -> Visible = true;
        Label1 -> Left = 100;
        Label1 -> Visible = true;
        RichEdit1 -> Visible = true;
        Edit1  -> Visible = false;
        Edit2  -> Visible = false;
        Edit3  -> Visible = false;
        BitBtn1-> Visible = false;
        BitBtn2 -> Visible = true;
        BitBtn3 -> Visible = true;

    for ( a = n_1; a <= n_2; a++ ) {
        int ePrimo = 1;
        if ( a % n_3 == 0 ) {
           ePrimo = 0;
        }
        if ( ePrimo == 0 ) {
            i++;
            str_1 += "  ";
            if ( a >= 0 && a < 10 )
                str_1 += ( "000" );
            if ( a  >= 10 && a < 100 )
                str_1 += ( "00" );
            if ( a >= 100 && a < 1000 )
                str_1 += ( "0" );
            str_1 += a;
        }
    }

    RichEdit1 -> Text = str_1;
    //Concatenando Label
    Label1 -> Caption = "Entre ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( n_1 );
    Label1 -> Caption = Label1 -> Caption + " e ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( n_2 );
    Label1 -> Caption = Label1 -> Caption + " existem ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( i );
    Label1 -> Caption = Label1 -> Caption + " múltiplos de ";
    Label1 -> Caption = Label1 -> Caption + StrToInt ( n_3 );
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label1Click ( TObject *Sender ) {
    Label1 -> Visible = false;
    n_1 = 0;
    n_2 = 0;
    n_3 = 0;
    Edit1 -> Clear ( );
    Edit2 -> Clear ( );
    Edit3 -> Clear ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click ( TObject *Sender ) {
   l++;
  if ( l == 1 ) {
    SpeedButton1 -> Visible = true;
    Label1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click ( TObject *Sender ) {
    n_1 = 0;
    n_2 = 0;
    n_3 = 0;
    a = 0;
    i = 0;
    str_1 = " ";
    Label1 -> Caption = " ";
    RichEdit1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
    Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click ( TObject *Sender ){
  exit ( 0 );
}
//---------------------------------------------------------------------------


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

#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.Graphics.hpp>
#include <Vcl.Buttons.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TImage *Image1;
    TEdit *Edit1;
    TEdit *Edit2;
    TEdit *Edit3;
    TBitBtn *BitBtn1;
    TRichEdit *RichEdit1;
    TBitBtn *BitBtn2;
    TBitBtn *BitBtn3;
    TLabel *Label1;
    TSpeedButton *SpeedButton1;
    TShape *Shape1;
    void __fastcall Label_Manual ( TObject *Sender );
    void __fastcall FormShow ( TObject *Sender );
    void __fastcall BitBtn1Click(TObject *Sender);
    void __fastcall Label1Click(TObject *Sender);
    void __fastcall BitBtn3Click(TObject *Sender);
    void __fastcall BitBtn2Click(TObject *Sender);
    void __fastcall SpeedButton1Click(TObject *Sender);
private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



terça-feira, 14 de abril de 2020

C++ builder - números primos entre dois valores

Já sabemos que um número é classificado 
como primo se ele é maior do que um 
e é divisível  apenas por um e por ele mesmo.
Isto já é suficiente para aplicação de uma lógica
computacional para seleção dos mesmos.
Este exemplo exibe números primos entre dois valores
inseridos pelo usuário, mas configurado para que
o número final nunca seja menor que o inicial,
e que não se permita a entrada de números menores que 2.




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

#include <vcl.h>
#pragma hdrstop
#define lin  12
#define col 44
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString str_1;
int a, i, j, l, primo, n_1, n_2;
bool x = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
: TForm ( Owner )
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    //altera o estilo da font do Canvas para itálico
    Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 270, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 270, "Samuel Lima" );
    Canvas -> Font -> Color = clBlue;
    Canvas -> TextOut ( 200, 300, "sa_sp10@hotmail.com" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {

    Form1 -> Color = ( RGB ( 229, 159, 185 ) );
    Bevel1 -> Visible = true;
    Bevel1 -> Width = 240;
    Bevel1 -> Height = 115;
    Bevel1 -> Left = 160;
    Bevel1 -> Top = 40;

    Edit1 -> Visible = true;
    Edit1 -> SetFocus ( );
    Edit1 -> Clear ( );
    Edit1 -> Font -> Color = clRed;
    Edit1 -> Font-> Style = TFontStyles ( ) << fsItalic;
    Edit1 -> Font -> Size = 11;
    Edit1 -> Width = 90;
    Edit1 -> Height = 20;
    Edit1 -> Left = 290;
    Edit1 -> Top = 60;

    Edit2 -> Visible = true;
    Edit2 -> Clear ( );
    Edit2 -> Font -> Color = clRed;
    Edit2 -> Font-> Style = TFontStyles ( ) << fsItalic;
    Edit2 -> Font -> Size = 11;
    Edit2 -> Width = 90;
    Edit2 -> Height = 20;
    Edit2 -> Left = 290;
    Edit2 -> Top = 90;

    Label1 -> Visible = true;
    Label1 -> Font -> Size = 12;
    Label1 -> Font -> Name = "Arial";
    Label1 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label1 -> Font -> Color = clBlack;
    Label1 -> Width = 130;
    Label1 -> Height = 20;
    Label1 -> Left = 180;
    Label1 -> Top = 60;

    Label2 -> Visible = true;
    Label2 -> Font -> Size = 12;
    Label2 -> Font -> Name = "Arial";
    Label2 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label2 -> Font -> Color = clBlack;
    Label2 -> Width = 130;
    Label2 -> Height = 20;
    Label2 -> Left = 180;
    Label2 -> Top = 90;

    Label3 -> Visible = true;
    Label3 -> Font -> Size = 11;
    Label3 -> Font -> Name = "Arial";
    Label3 -> Font-> Style = TFontStyles ( ) << fsItalic ;
    Label3 -> Font -> Color = clBlack;
    Label3 -> Width = 330;
    Label3 -> Height = 20;
    Label3 -> Left = 140;
    Label3 -> Top = 330;

    Panel1 -> Font -> Size = 12;
    Panel1 -> Font -> Color = clRed;
    Panel1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
    Panel1 -> BevelWidth = 3;

    BitBtn1 -> Visible = true;
    BitBtn1 -> Font -> Size = 12;
    BitBtn1 -> Font -> Color = clBlue;
    BitBtn1    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn1 -> Top = 120;
    BitBtn1 -> Left = 180;
    BitBtn1 -> Height = 23;
    BitBtn1 -> Width = 200;
    BitBtn1 -> Caption = ( "Gerar números primos" );

    BitBtn2 -> Font -> Size = 11;
    BitBtn2 -> Font -> Color = clBlack;
    BitBtn2    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn2 -> Top = 270;
    BitBtn2 -> Left = 230;
    BitBtn2 -> Height = 21;
    BitBtn2 -> Width = 140;
    BitBtn2 -> Caption = ( "Nova operação" );
    BitBtn2 -> TabOrder = 2;

    BitBtn3 -> Font -> Size = 11;
    BitBtn3 -> Font -> Color = clBlack;
    BitBtn3    -> Font-> Style = TFontStyles ( ) << fsItalic ;
    BitBtn3 -> Top = 300;
    BitBtn3 -> Left = 230;
    BitBtn3 -> Height = 21;
    BitBtn3 -> Width = 140;
    BitBtn3 -> Caption = ( "Sair do programa" );

    RichEdit1 -> Lines -> Clear ( );
    RichEdit1 -> Font -> Name = "Arial";
    RichEdit1 -> Font -> Size = 12;
    RichEdit1 -> Color = ( RGB ( 128, 128, 0 ) );
    RichEdit1 -> Font -> Color = clBlack;
    RichEdit1 -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic;
    RichEdit1 -> BevelWidth = 3;
    RichEdit1 -> BorderStyle = bsNone;
    RichEdit1 -> BorderWidth = 5;
    RichEdit1 -> Width = 400;
    RichEdit1 -> Height = 220;
    RichEdit1 -> Left = 100;
    RichEdit1 -> Top = 40;
    RichEdit1 -> ScrollBars = ssVertical;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
    Label_Manual ( Sender );
    Panel1 -> Visible = false;
    RichEdit1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
    Label1 -> Caption = "Número inicial";
    Label2 -> Caption = "Número final";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender ) {
    n_1 = StrToInt ( Edit1 -> Text );
    n_2 = StrToInt ( Edit2 -> Text );
    int tam, tot;
    tam = n_2 - n_1;
    if ( n_1 > n_2 ) {
        Panel1 -> Visible = true;
        Panel1 -> Width = 400;
        Panel1 -> Height = 30;
        Panel1 -> Top = 320;
        Panel1 -> Left = 110;
        Panel1 -> Caption = "O número inicial não pode ser maior que o final";
        i = 0;
    }
    if ( n_1 < 2 || 2 > n_2 ) {
        Panel1 -> Visible = true;
        Panel1 -> Width = 330;
        Panel1 -> Height = 30;
        Panel1 -> Top = 320;
        Panel1 -> Left = 145;
        Panel1 -> Caption = "Números menores que 2 são rejeitados";
        i = 0;
    }

    if ( n_1 < n_2 && n_1 > 1 ) {
        RichEdit1 -> Visible = true;
        Label1 -> Visible = false;
        Bevel1 -> Visible = false;
        Edit1  -> Visible = false;
        Edit2  -> Visible = false;
        BitBtn1-> Visible = false;
        BitBtn2 -> Visible = true;
        BitBtn3 -> Visible = true;
        x = true;
    }

    for ( a = n_1; a < n_2; a++ ) {
        int ePrimo = 1;
        for ( j = 2; j <= a / 2; j++ ) {
            if ( a % j == 0 ) {
                ePrimo = 0;
                break;
            }
        }
        if ( ePrimo == 1 ) {
            i++;
            str_1 += "  ";
            if ( a >= 0 && a < 10 )
                str_1 += ( "000" );
            if ( a  >= 10 && a < 100 )
                str_1 += ( "00" );
            if ( a >= 100 && a < 1000 )
                str_1 += ( "0" );
            str_1 += a;
        }
    }
    if ( x == true ) {
    RichEdit1 -> Text = str_1;
    //Concatenando Label
    Label3 -> Caption = "Entre ";
    Label3 -> Caption = Label3 -> Caption + StrToInt ( n_1 );
    Label3 -> Caption = Label3 -> Caption + " e ";
    Label3 -> Caption = Label3 -> Caption + StrToInt ( n_2 );
    Label3 -> Caption = Label3 -> Caption + " existem ";
    Label3 -> Caption = Label3 -> Caption + StrToInt ( i );
    Label3 -> Caption = Label3 -> Caption + " números primos";
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel1Click ( TObject *Sender ) {
    Panel1 -> Visible = false;
    n_1 = 0;
    n_2 = 0;
    Edit1 -> Clear ( );
    Edit2 -> Clear ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click ( TObject *Sender ) {
    n_1 = 0;
    n_2 = 0;
    a = 0;
    i = 0;
    str_1 = " ";
    str_2 = " ";
    x = false;
    Label3 -> Caption = " ";
    RichEdit1 -> Visible = false;
    BitBtn2 -> Visible = false;
    BitBtn3 -> Visible = false;
    Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click ( TObject *Sender ) {
    l++;
    Label3 -> Caption = " ";
    if ( l == 1 ) {
        BitBtn2 -> Font -> Color = clRed;
        BitBtn3 -> Font -> Color = clBlack;
        BitBtn2 -> Caption = ( "Por: Samuel Lima" );
        BitBtn3 -> Caption = ( "sa_sp100@hotmail.com" );
        BitBtn3 -> Width = 170;
    }
    if ( l == 2 ) {
        exit ( 0 );
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender ){
    //altera o estilo da font do Canvas para itálico
    Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Name = "Arial";
    Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clBlack;
    Canvas -> Rectangle ( 05, 05, 595, 365 );
    SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
    Canvas -> TextOut ( 110, 10, "NÚMEROS PRIMOS ENTRE DOIS VALORES" );
}
//---------------------------------------------------------------------------



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

#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>
#include <System.ImageList.hpp>
#include <Vcl.ImgList.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
    TLabel *Label1;
    TBitBtn *BitBtn1;
    TLabel *Label2;
    TLabel *Label4;
    TEdit *Edit1;
    TEdit *Edit2;
    TBevel *Bevel1;
    TPanel *Panel1;
    TBitBtn *BitBtn2;
    TBitBtn *BitBtn3;
    TRichEdit *RichEdit1;
    TLabel *Label3;
    void __fastcall Informe ( TObject *Sender );
    void __fastcall BitBtn1Click(TObject *Sender);
    void __fastcall FormShow(TObject *Sender);
    void __fastcall Panel1Click(TObject *Sender);
    void __fastcall BitBtn2Click(TObject *Sender);
    void __fastcall BitBtn3Click(TObject *Sender);
    void __fastcall FormPaint(TObject *Sender);
private: // User declarations
String str_1;
String str_2;
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 = 'N'#218'MEROS PRIMOS ENTRE DOIS VALORES'
  ClientHeight = 370
  ClientWidth = 600
  Color = clWhite
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 40
    Top = 208
    Width = 3
    Height = 13
  end
  object Label2: TLabel
    Left = 73
    Top = 128
    Width = 3
    Height = 13
  end
  object Label4: TLabel
    Left = 80
    Top = 270
    Width = 5
    Height = 19
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Bevel1: TBevel
    Left = 537
    Top = 81
    Width = 32
    Height = 25
  end
  object Label3: TLabel
    Left = 80
    Top = 208
    Width = 3
    Height = 13
  end
  object BitBtn1: TBitBtn
    Left = 537
    Top = 128
    Width = 33
    Height = 25
    Caption = 'OK'
    TabOrder = 0
    OnClick = BitBtn1Click
  end
  object Edit1: TEdit
    Left = 536
    Top = 16
    Width = 33
    Height = 21
    TabOrder = 1
  end
  object Edit2: TEdit
    Left = 537
    Top = 43
    Width = 33
    Height = 21
    TabOrder = 2
  end
  object Panel1: TPanel
    Left = 537
    Top = 251
    Width = 34
    Height = 30
    TabOrder = 3
    OnClick = Panel1Click
  end
  object BitBtn2: TBitBtn
    Left = 535
    Top = 170
    Width = 33
    Height = 25
    ModalResult = 5
    NumGlyphs = 2
    TabOrder = 4
    OnClick = BitBtn2Click
  end
  object BitBtn3: TBitBtn
    Left = 535
    Top = 208
    Width = 32
    Height = 25
    TabOrder = 5
    OnClick = BitBtn3Click
  end
  object RichEdit1: TRichEdit
    Left = 8
    Top = 8
    Width = 33
    Height = 25
    BevelWidth = 3
    BorderWidth = 5
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    Lines.Strings = (
      '')
    ParentFont = False
    TabOrder = 6
    Zoom = 100
  end
end