domingo, 13 de março de 2022

C++ Builder - retornando números primos ou não primos


Criamos uma função simples e funcional
capaz de retornar números primos ou não primos.
ora, isto é muito básico e totalmente didático,
é indicado aos iniciantes em qualquer linguagem
de programação, poderia ser escrito em menos 
de dez linhas de códigos, mas como me sobrou
um pouco mais de tempo no dia de hoje, resolvi
escrevê-los tudo na IDE da Embarcadero, o C++ Builder,
com interface em janelas, mas que também poderia
ser escrito para rodar num console utilizando
os excelentes compiladores antigos da Borlan,
para C ou C++.
Acompanhem no vídeo esta brincadeira didática,
Voltada aos programadores iniciantes:



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

#include <vcl.h>
#include <stdio.h>
#include <ctype.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TLabel *Label1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
	Canvas -> Font -> Size = 11;
	Canvas -> Font -> Name = "Garamond";
	Canvas -> Font -> Color = clBlue;
	Canvas -> TextOut ( 240, 250, "Por: " );
	Canvas -> Font -> Color = clRed;
	Canvas -> TextOut ( 270, 250, "Samuel Lima" );
	Canvas -> Font -> Color = clYellow;
	Canvas -> TextOut ( 240, 265, "sa_sp10@hotmail.com" );
	Canvas -> Font -> Name = "Garamond";
}
//--------------------------------------------------------------------------
 int Ret_prim_NPrim ( int n ) {
	int j = 0;
	for ( int i = 2; i <= n / 2; i++ )
		if ( ! ( n % i ) )
			j++;
	return ( j );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject* Sender ) {

	 Edit1 -> Clear ( );
         Edit1 -> Font -> Name = "alarm clock";
         Edit1 -> Font -> Size = 11;
	 Edit1 -> Color = static_cast < TColor > ( RGB ( 0, 255, 255 ) );
         Edit1 -> Font -> Style = TFontStyles ( ) << fsBold;
	 Edit1 -> Font -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
         Edit1 -> BevelWidth = 3;
	 //Edit1 -> BevelKind = bkFlat;
	 Edit1 -> BorderStyle = bsSingle;
         Edit1 -> Width = 60;
	 Edit1 -> Height = 19;
         Edit1 -> Left = 240;
	 Edit1 -> Top = 170;

		Image1 -> Width = 40;
		Image1 -> Height = 39;
		Image1 -> Left = 310;
		Image1 -> Top = 170;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_2 ( int X, int Y,
	int R, int G, int B, bool transparent, String FontName, int S,
	TFontStyles style, int R1, int G1, int B1, String str_1 ) {
    Label1 = new TLabel ( Form1 );
    Label1 -> Parent = Form1;
    Label1 -> AutoSize = true;
    Label1 -> Transparent = transparent;
    Label1 -> Color = static_cast < TColor > ( RGB ( R, G, B ) );
    Label1 -> WordWrap = false;
    Label1 -> Font -> Name = FontName;
    Label1 -> Font -> Size = S;
    Label1 -> Font-> Style = style;
    Label1 -> Font -> Color = static_cast < TColor > ( RGB ( R1, G1, B1 ) );
    Label1 -> Left = X;
    Label1 -> Top = Y;
    Label1 -> Caption = str_1;
}
//--------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender){
 Canvas -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic;
 Canvas -> Font -> Size = 11;
 Canvas -> Font -> Name = "Tahoma";
 Canvas -> Pen -> Width = 10;
 Canvas -> Pen -> Color = clBlue;
 Canvas -> RoundRect ( 05, 05, 595, 295, 20, 20 );

 Informe ( Sender );
}
 //---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::Image1Click ( TObject *Sender ) {
	  Image1 -> Picture -> LoadFromFile ("Sem título_3.bmp" );
			Label_2 ( 240, 150, 255, 255, 0, false, "Consolas", 8,
			  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Digite um número" );

			Label_2 ( 170, 13, 255, 0, 0, false, "Consolas", 11,
			  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "RETORNANDO NÚMERO PRIMO OU NÃO PRIMO" );

		 int Prim;
		 Prim = StrToInt ( Edit1 -> Text );

		 Label_2 ( 240, 200, 255, 165, 0, false, "Consolas", 10,
			  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "                 " );

				 Label_2 ( 280, 200, 255, 165, 0, false, "Consolas", 10,
			  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, Ret_prim_NPrim ( Prim ) ?  "Não é Primo" : " É Primo" );

		 Label_2 ( 240, 200, 255, 0, 0, false, "Consolas", 10,
			  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, Prim );

		 Edit1 -> Clear ( );
		 Edit1 -> SetFocus ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate ( TObject *Sender ) {
		Label_Manual ( Sender );

		Label_2 ( 170, 13, 0, 0, 0, false, "Consolas", 11,
			  TFontStyles ( ) << fsBold << fsItalic, 255, 0, 0, "RETORNANDO NÚMERO PRIMO OU NÃO PRIMO" );

		Label_2 ( 240, 150, 135, 206, 235, false, "Consolas", 8,
			  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Digite um número" );

		Label_2 ( 315, 170, 244, 244, 244, true, "Consolas", 11,
			  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Ok" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1MouseEnter ( TObject *Sender ) {
	  Label_2 ( 170, 13, 0, 0, 0, false, "Consolas", 11,
			  TFontStyles ( ) << fsBold << fsItalic, 255, 0, 0, "RETORNANDO NÚMERO PRIMO OU NÃO PRIMO" );
}
//---------------------------------------------------------------------------






Nenhum comentário:

Postar um comentário

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