quinta-feira, 16 de maio de 2019

C++ builder - de inteiro para hex e bin

Este programa feito no C++ builder,
converte números inteiros em Hexadecimal
e binário, mas pode ser expandido 
para cálculos diversos graças ao teclado 
numérico que criamos utilizando botões
e programando rotinas de códigos para
acionamento dos eventos chamados pelo
click do mouse, ou na entrada pelo teclado.
Não vou detalhar como fiz mas deixo postado
os códigos para quem tiver interesse
em testar este ótimo programa.


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

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString Dest;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1( TComponent* Owner )
: TForm ( Owner )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
}
class Convert_Dec_Bin {
public:
const static int DEC = 1;
int DecBin( int dec ){
int bin = 0, tp = 1;
while ( dec > 0 ){
bin = bin + ( dec % 2 ) * tp;
dec = dec / 2;
tp *= 10;
}
return bin;
}
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "1";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button13Click ( TObject *Sender )
{
int StrInt;
StrInt = StrToInt ( Edit1 -> Text );
Convert_Dec_Bin conv;
int x = Edit1 -> Text.ToIntDef ( 0 );
int conversor = conv.DecBin ( x );
if ( x > 0 ) {
Edit1 -> Text = conversor;
} else {
Edit1 -> Clear ( );
}
Edit1 -> SetFocus ( );
Dest = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button14Click ( TObject *Sender )
{
String inf = "   Criado por: Samuel Lima\n\n";
String inf_1 = "        MUITO OBRIGADO";
Memo1 -> Enabled = true;
Memo1 -> Visible = true;
Memo1 -> Lines -> Strings [ 0 ] = inf;
Memo1 -> Lines -> Strings [ 1 ] = inf_1;
Sleep ( 2800 );
exit ( 0 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click ( TObject *Sender )
{
int StrInt;
StrInt = StrToInt ( Edit1 -> Text );
Edit1 -> Text = IntToHex ( StrInt, 1 );
Dest = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "2";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
 void __fastcall TForm1::Button4Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "3";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "4";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "5";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "6";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "7";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "8";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
 void __fastcall TForm1::Button10Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "9";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button11Click ( TObject *Sender )
{
 Memo1 -> Enabled = false;
 Memo1 -> Visible = false;
 int i = 0, um = 0;
  um++;
  char str [ ] = "0";
  while ( i < 10 ) {
AppendStr ( Dest, str );
Edit1 -> Text = Dest;
i++;
break;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button12Click ( TObject *Sender )
{
Edit1 -> Clear ( );
Edit1 -> Text = "0";
Dest = "";
Edit1 -> SetFocus ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender )
{
 Canvas -> Font -> Size = 13;
 Canvas -> Font -> Name = "Arial";
 Canvas -> Font -> Style = TFontStyles ( ) << fsBold;
 //Colorindo a janela
 Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0, 255, 255 ) );
 Canvas -> Pen -> Width = 10;
 Canvas -> Pen -> Color = clBlack;
 Canvas -> Rectangle ( 05, 05, 315, 213 );
 Canvas -> Font -> Color = clRed;
 Canvas -> TextOut ( 42, 10, "DE INTEIRO PARA HEX E BIN" );
Form1 -> Constraints -> MaxHeight = 255;
Form1 -> Constraints -> MaxWidth = 335;
}
//---------------------------------------------------------------------------


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Buttons.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TLabel *Label2;
TEdit *Edit1;
TShape *Shape1;
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
TButton *Button5;
TButton *Button6;
TButton *Button7;
TButton *Button8;
TButton *Button9;
TButton *Button10;
TButton *Button11;
TButton *Button12;
TButton *Button13;
TButton *Button14;
TMemo *Memo1;

void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall Button5Click(TObject *Sender);
void __fastcall Button6Click(TObject *Sender);
void __fastcall Button7Click(TObject *Sender);
void __fastcall Button8Click(TObject *Sender);
void __fastcall Button9Click(TObject *Sender);
void __fastcall Button10Click(TObject *Sender);
void __fastcall Button11Click(TObject *Sender);
void __fastcall Button12Click(TObject *Sender);
void __fastcall Button13Click(TObject *Sender);
void __fastcall Button14Click(TObject *Sender);

void __fastcall OnPaint(TObject *Sender);

private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 541
  Top = 118
  Caption = 'DE INTEIRO PARA HEXADECIMAL'
  ClientHeight = 217
  ClientWidth = 317
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  Visible = True
  OnPaint = OnPaint
  DesignSize = (
    317
    217)
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 304
    Top = 96
    Width = 5
    Height = 21
    Caption = ' '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label2: TLabel
    Left = 320
    Top = 96
    Width = 5
    Height = 21
    Caption = ' '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Shape1: TShape
    Left = 23
    Top = 33
    Width = 275
    Height = 77
    Brush.Style = bsClear
    Pen.Color = clBlue
    Pen.Width = 5
  end
  object Button1: TButton
    Left = 22
    Top = 115
    Width = 41
    Height = 25
    Caption = '1'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 210
    Top = 146
    Width = 41
    Height = 25
    Caption = 'H'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 2
    OnClick = Button2Click
  end
  object Edit1: TEdit
    AlignWithMargins = True
    Left = 28
    Top = 38
    Width = 265
    Height = 68
    Alignment = taRightJustify
    Anchors = []
    AutoSelect = False
    AutoSize = False
    BevelWidth = 10
    Color = clOlive
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -21
    Font.Name = 'Arial'
    Font.Style = [fsItalic]
    NumbersOnly = True
    ParentFont = False
    TabOrder = 1
    Text = '0'
  end
  object Button3: TButton
    Left = 69
    Top = 115
    Width = 41
    Height = 25
    Caption = '2'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 3
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 116
    Top = 115
    Width = 41
    Height = 25
    Caption = '3'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 4
    OnClick = Button4Click
  end
  object Button5: TButton
    Left = 163
    Top = 115
    Width = 41
    Height = 25
    Caption = '4'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 5
    OnClick = Button5Click
  end
  object Button6: TButton
    Left = 210
    Top = 115
    Width = 41
    Height = 25
    Caption = '5'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 6
    OnClick = Button6Click
  end
  object Button7: TButton
    Left = 257
    Top = 115
    Width = 41
    Height = 25
    Caption = '6'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 7
    OnClick = Button7Click
  end
  object Button8: TButton
    Left = 22
    Top = 146
    Width = 41
    Height = 25
    Caption = '7'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 8
    OnClick = Button8Click
  end
  object Button9: TButton
    Left = 69
    Top = 146
    Width = 41
    Height = 25
    Caption = '8'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 9
    OnClick = Button9Click
  end
  object Button10: TButton
    Left = 116
    Top = 146
    Width = 41
    Height = 25
    Caption = '9'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 10
    OnClick = Button10Click
  end
  object Button11: TButton
    Left = 163
    Top = 146
    Width = 41
    Height = 25
    Caption = '0'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 11
    OnClick = Button11Click
  end
  object Button12: TButton
    Left = 257
    Top = 146
    Width = 41
    Height = 25
    Caption = 'CR'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 12
    OnClick = Button12Click
  end
  object Button13: TButton
    Left = 23
    Top = 177
    Width = 41
    Height = 25
    Caption = 'B'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 13
    OnClick = Button13Click
  end
  object Button14: TButton
    Left = 70
    Top = 177
    Width = 228
    Height = 25
    Caption = 'Sair do programa'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 14
    OnClick = Button14Click
  end
  object Memo1: TMemo
    Left = 28
    Top = 38
    Width = 265
    Height = 68
    Color = clOlive
    Enabled = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    Lines.Strings = (
      ' ')
    ParentFont = False
    TabOrder = 15
  end
end


Nenhum comentário:

Postar um comentário

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