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

Nenhum comentário:

Postar um comentário

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