segunda-feira, 23 de dezembro de 2019

Pesquisando múltiplos em matriz de inteiro

Múltiplos são números que resultam da multiplicação
de um número inteiro qualquer por um número natural.
X é múltiplo de Y se X = nY, com n natural.
Qualquer número multiplicado pela sequência
dos números naturais, obtém - se os seus Múltiplos.
Partindo da lógica matemática, criei este programa
em C++ utilizando o C++ Builder da embarcadero.






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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1* Form1;
int A[ 10 ][ 10 ];
int x = 0;
int t = 0;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner ) : TForm ( Owner ) {
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject* Sender ) {
    Edit1 -> SetFocus ( );
    Edit1 -> Font->Size = 12;
    Edit1 -> Width = 90;
    Edit1 -> Height = 20;
    Edit1 -> Left = 160;
    Edit1 -> Top = 280;

    Label1 -> Font->Size = 12;
    Label1 -> Font->Name = "Arial";
    Label1 -> Font->Color = clBlack;
    Label1 -> Width = 30;
    Label1 -> Height = 20;
    Label1 -> Left = 160;
    Label1 -> Top = 310;

    BitBtn1 -> Font->Size = 12;
    BitBtn1 -> Font->Color = clBlue;
    BitBtn1 -> Font->Style = TFontStyles ( ) << fsItalic;
    BitBtn1 -> Top = 280;
    BitBtn1 -> Left = 260;
    BitBtn1 -> Height = 23;
    BitBtn1 -> Width = 30;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject* Sender ) {
    Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Size = 12;
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 400, 290, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 440, 290, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 400, 305, "sa_sp10@hotmail.com" );
    Canvas -> Font -> Name = "Garamond";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint ( TObject* Sender ) {
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Name = "Arial";
    Canvas -> Font -> Style = TFontStyles ( ) << fsBold << fsItalic ;
    Canvas -> Pen -> Width = 12;
    Canvas -> Pen -> Color = clBlue;
    Canvas -> Rectangle ( 05, 05, 595, 345 );
    SetTextColor ( Canvas->Handle, RGB ( 255, 25, 2 ) );
    Canvas -> TextOut ( 60, 10, "PESQUISANDO MÚLTIPLOS EM MATRIZ DE INTEIROS" );
    Canvas -> Font -> Size = 11;
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 160, 260, "Digite um número: " );

    Imprime_matriz ( Sender );
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Imprime_matriz ( TObject* Sender ) {
    Canvas->Font->Name = "Arial";
    int a = 0, b = 0, i;
    for ( i = 0; i < 100; i ++ ) {
        A [ b ] [ a ] = i;
        a ++ ;
    }
    for ( a = 0; a < 10; a ++ ) {
        for ( b = 0; b < 10; b ++ ) {
            Canvas -> Font -> Color = clBlack;
            Canvas -> TextOut ( 160 + ( b* 28 ), 55 + ( a* 20 ), A [ a ] [ b ] );
        }
    }
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject* Sender ) {
    if ( x == 0 ) {
        Imprime_matriz  ( Sender );
    }
    Canvas -> Font -> Size = 12;
    Canvas -> Font -> Name = "Arial";
    Canvas -> Pen -> Width = 10;

    int a, b, n = 0;
    n = StrToInt ( Edit1 -> Text );
    t = 0;
    for ( a = 0; a < 10; a ++ ) {
        for ( b = 0; b < 10; b ++ ) {
            if ( A [ a ] [ b ] % n == 0 && A [ a ] [ b ]  != 0 ) {
                t ++ ;
                if ( x == 0 ) {
                    SetBkColor ( Canvas -> Handle, RGB ( 255, 0, 255 ) );
                    Canvas -> TextOut ( 160 + ( b* 28 ), 55 + ( a* 20 ),
                        A [ a ] [ b ] );
                }
            }
        }
    }
       Canvas -> Refresh ( );
       Label1 -> Caption = "Encontrado ";
       Label1 -> Caption = Label1 -> Caption + t;
       Label1 -> Caption = Label1 -> Caption + " Múltiplos de  ";
       Label1 -> Caption = Label1 -> Caption + n;
       Informe ( Sender );

    Edit1 -> Clear ( );
    Edit1 -> SetFocus ( );
}
// ---------------------------------------------------------------------------


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

#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>

// ---------------------------------------------------------------------------
class TForm1 : public TForm {
__published : // IDE-managed Components

    TPaintBox* PaintBox1;
    TLabel* Label1;
    TBitBtn* BitBtn1;
    TEdit* Edit1;

    void __fastcall PaintBox1Paint ( TObject* Sender );
    void __fastcall BitBtn1Click ( TObject* Sender );
    void __fastcall FormShow ( TObject* Sender );

private : // User declarations
    void __fastcall Label_Manual ( TObject* Sender );
    void __fastcall Imprime_matriz ( TObject* Sender );
    void __fastcall Informe ( TObject *Sender );
public : // User declarations
    __fastcall TForm1 ( TComponent* Owner );
} ;

// ---------------------------------------------------------------------------
extern PACKAGE TForm1* Form1;
// ---------------------------------------------------------------------------
#endif



object Form1: TForm1
  Left = 413
  Top = 118
  Caption = 'Substituindo elementos em matriz de string com memset'
  ClientHeight = 350
  ClientWidth = 600
  Color = clCream
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 536
    Top = 248
    Width = 33
    Height = 25
    OnPaint = PaintBox1Paint
  end
  object Label1: TLabel
    Left = 40
    Top = 208
    Width = 3
    Height = 13
  end
  object BitBtn1: TBitBtn
    Left = 536
    Top = 80
    Width = 33
    Height = 25
    Caption = 'OK'
    TabOrder = 0
    OnClick = BitBtn1Click
  end
  object Edit1: TEdit
    Left = 448
    Top = 32
    Width = 121
    Height = 21
    TabOrder = 1
  end
end

Nenhum comentário:

Postar um comentário

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