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



Nenhum comentário:

Postar um comentário

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