terça-feira, 8 de setembro de 2020

C++ builder - Variáveis signed e unsigned

 Variáveis signed e unsigned significam ( com sinal e sem sinal ),
que são dois modificadores de tipos muito útil aos programadores,
desta importante linguagem.
A finalidade destes modificadores é de rejeitar
valores que não serão aceitos em nosso programa.
Os tipos inteiros e os ( char ), aceitam normalmente
valores negativos ou positivos,
mas se na declaração destes tipos for antecedido
com o modificador unsigned, só podemos fazer uso
de números iguais ou maiores que zero.
Programadores podem querer tentar atribuir valores
acima do que uma variável possa suportar,
já adiantamos que se uma variável for excedida,
sendo declarada com unsigned, seus valores
continuarão a partir do zero, até o máximo
que a variável possa suportar, e se chegar
no seu extremo voltará novamente para o zero.
mas se for do tipo modificador signed, 
seus valores serão armazendos,
a partir do menor valor negativo ( -1 ).
até o máximo que a variável possa suportar,
e se chegar no seu extremo voltará novamente para o zero.
Então nestas linhas aprendemos que uma variável unsigned,
não aceita valores negativos, e no exemplo passado aqui,
podemos ver isto com muita clareza.

Declaramos duas variáveis, e inicializamos as mesmas,
com o mesmo valor ( 10 ), uma do tipo char com unsigned,
cujos valores ficam restrito entre ( 0 e 255 ),
que é justamente o dobro dos valores máximos de uma variável
char sem sinal ( -128 a 127 ).
e a outra variável também do tipo char declarada
com signed ( com sinal ).
Assista agora atentamente este vídeo,
e acesse o link para apreciação do código,
não leve em conta as diversas linhas atribuída
a interface gráfica, sobrou um pouco de tempo
e a paciência foi a virtude fundamental para isto. 




Veja abaixo os códigos do programa:

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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

unsigned char a = 10;
signed char b = 10;
bool x = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
: TForm ( Owner )
{
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
        Canvas -> Font -> Size = 11;
        Canvas -> Font -> Name = "Arial";
        Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 160, 250, "Por: " );
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 200, 250, "Samuel Lima" );
Canvas -> Font -> Color = clBlue;
Canvas -> TextOut ( 160, 265, "sa_sp10@hotmail.com" );
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
Label1 -> Visible = true;
        Label1 -> Font -> Size = 16;
Label1 -> Font -> Name = "alarm clock";
Label1 -> Font-> Style = TFontStyles ( ) << fsBold;
Label1 -> Font -> Color = clRed;
Label1 -> Width = 230;
Label1 -> Height = 20;
Label1 -> Left = 230;
Label1 -> Top = 69;

Label2 -> Visible = true;
Label2 -> Font -> Size = 16;
        Label2 -> Font -> Name = "alarm clock";
Label2 -> Font-> Style = TFontStyles ( ) << fsBold;
Label2 -> Font -> Color = clRed;
        Label2 -> Width = 230;
Label2 -> Height = 20;
Label2 -> Left = 230;
Label2 -> Top = 99;

BitBtn1 -> Font -> Name = "Venacti";
        BitBtn1 -> Visible = true;
        BitBtn1 -> Font -> Size = 12;
BitBtn1 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
        BitBtn1 -> Font -> Color = clBlack;
BitBtn1 -> Width = 50;
BitBtn1 -> Height = 22;
BitBtn1 -> Top = 260;
BitBtn1 -> Left = 70;
BitBtn1 -> Caption = ( "Stop" );

        BitBtn2 -> Font -> Name = "Venacti";
        BitBtn2 -> Font -> Size = 12;
BitBtn2 -> Font -> Color = clBlack;
BitBtn2 -> Font-> Style = TFontStyles ( ) << fsItalic ;
BitBtn2 -> Top = 260;
BitBtn2 -> Left = 480;
BitBtn2 -> Height = 22;
BitBtn2 -> Width = 50;
BitBtn2 -> Caption = ( "Run" );
}
//---------------------------------------------------------------------------
void Partida_Rapida ( void ) {
if ( a > 10 )
   Form1 -> Timer1 -> Interval = 100;
   Form1 -> Label1 -> Caption = a;
   a--;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnTimer ( TObject *Sender ) {
Label_Manual ( Sender );
if ( x == false ) {
Timer1 -> Interval = 0;
}
if ( x == true ) {
Timer1 -> Interval = 1000;
}
Label2 -> Caption = b;
if ( a < 10 )
Label1 -> Caption = a;

if ( a > 10 ) {
  Partida_Rapida ( );
}
a--;
b--;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender ) {
Timer1 -> Interval = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click ( TObject *Sender ) {
Timer1 -> Interval = 1000;
         x = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
Label_Manual ( Sender );
  Label1 -> Left = 230;
Label1 -> Caption =  a;

Label2 -> Left = 230;
Label2 -> Caption = StrToInt ( b );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender ) {
Canvas -> Font -> Size = 14;
Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
Canvas -> Font -> Name = "Arial";
                 Canvas -> Pen -> Width = 10;
Canvas -> Pen -> Color = clRed;
Canvas -> RoundRect ( 05, 05, 595, 295, 25, 25 );
SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
Canvas -> TextOut ( 160, 10, "VARIÁVEIS SIGNED E UNSIGNED" );
Canvas -> Font -> Size = 12;
Canvas -> Font -> Color = clBlue;
Canvas -> TextOut ( 160, 35, "Observe o comportamento das variáveis" );

Canvas -> Font -> Size = 14;
Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
Canvas -> Font -> Name = "Arial";
Canvas -> Font -> Size = 12;
Canvas -> Font -> Color = clBlue;
Canvas -> TextOut ( 70, 70,  "Variável unsigned => " );
Canvas -> TextOut ( 70, 100, "Variável signed     => " );
Informe ( Sender );
}
//---------------------------------------------------------------------------

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

#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
TTimer *Timer1;
TLabel *Label1;
TLabel *Label2;
TBitBtn *BitBtn1;
TBitBtn *BitBtn2;
void __fastcall OnTimer ( TObject *Sender );
void __fastcall Informe ( TObject *Sender );
void __fastcall Label_Manual ( TObject *Sender );
void __fastcall FormShow (TObject *Sender );
void __fastcall FormPaint ( TObject *Sender );
void __fastcall BitBtn1Click ( TObject *Sender );
void __fastcall BitBtn2Click ( 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.