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

Nenhum comentário:

Postar um comentário

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