segunda-feira, 23 de setembro de 2019

C++ Builder - Calculando hipotenusa

O Teorema de Pitágoras é considerado uma das
principais descobertas da Matemática.
Ele descreve uma relação existente no triângulo retângulo.
Vale lembrar que o triângulo retângulo pode ser identificado
pela existência de um ângulo reto, isto é, que mede 90º.
O triângulo retângulo é formado por dois catetos e a hipotenusa,
que constitui o maior segmento do triângulo
e localiza-se opostamente ao ângulo reto, e sua fórmula é esta abaixo:
Cateto a² + Cateto b² = h²
O Teorema de Pitágoras diz que:
“a soma dos quadrados dos catetos é igual ao quadrado da hipotenusa.”

Fonte: https://brasilescola.uol.com.br/matematica/teorema-pitagoras.html

Partindo destas informações ficou muito fácil
converter sua fórmula matemática em uma equação
em que o computador possa entender, e foi o que fizemos.
Na verdade isto é muito simples, e nos foi ensinado
quando estávamos dando nossos primeiros passos no mundo
maravilhoso da programação, a única diferença é que aqui,
estamos utilizando uma janela gráfica feita no
C++ Builder da Embarcadero, e eu estou usando a versão 10.3.
É uma pena que muitos não conhecem o poder desta ferramenta
profissional que pertencia no início a Borland,
mas de alguns anos para hoje pertence a Embarcadero,
e na minha opinião é a melhor opção para quem estuda
linguagem C ou C++, e não quer mudar de linguagem,
por estar familiarizado com suas sintaxes.


Veja abaixo um vídeo com o programa em execução:




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

#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include "Unit1.h"
#include <memory>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 240, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 240, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Color = clSkyBlue;
    Canvas -> TextOut ( 196, 266, "sa_sp10@hotmail.com" );
    SetBkMode ( Canvas -> Handle, TRANSPARENT );
    Canvas -> Font -> Color = clBlue;
    Canvas -> TextOut ( 190, 260, "sa_sp10@hotmail.com" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
     Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clRed;
    Canvas -> Rectangle ( 05, 05, 595, 295 );
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Name = "arial";
    Canvas -> Font -> Color = clRed;
    Canvas -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic << fsUnderline;
    Canvas -> TextOut ( 180, 12, "CALCULANDO HIPOTENUSA" );
    Informe ( Sender );
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  double c1 = 0.00, c2 = 0.00, h = 0.00;
  c1 = StrToFloat ( Edit1 -> Text );
  c2 = StrToFloat ( Edit2 -> Text );
  h = sqrt ( pow ( c1, 2 ) + pow ( c2, 2 ) );
  Edit3 -> Text = FloatToStrF ( h, ffFixed, 2, 2 );
  Image1->Picture->LoadFromFile("C:\\Users\\ManMachine\\Downloads\\"
    "\\Calculando hipotenusa\\trianguloReto.bmp");

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Form1 -> Close ( );
}
//---------------------------------------------------------------------------



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

#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.Imaging.pngimage.hpp>
#include <Vcl.Graphics.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TEdit *Edit1;
    TEdit *Edit2;
    TBevel *Bevel1;
    TEdit *Edit3;
    TLabel *Label1;
    TLabel *Label2;
    TLabel *Label3;
    TButton *Button1;
    TButton *Button2;
    TBevel *Bevel2;
    TImage *Image1;
    void __fastcall Informe ( TObject *Sender );
    void __fastcall FormPaint(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



object Form1: TForm1
  Left = 455
  Top = 177
  Caption = 'CALCULANDO HIPOTENUSA'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Bevel1: TBevel
    Left = 32
    Top = 47
    Width = 265
    Height = 186
  end
  object Label1: TLabel
    Left = 55
    Top = 70
    Width = 56
    Height = 16
    Caption = 'Cateto 1'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 55
    Top = 108
    Width = 56
    Height = 16
    Caption = 'Cateto 2'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label3: TLabel
    Left = 55
    Top = 151
    Width = 73
    Height = 16
    Caption = 'Hipotenusa'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Bevel2: TBevel
    Left = 312
    Top = 47
    Width = 265
    Height = 170
  end
  object Image1: TImage
    Left = 328
    Top = 64
    Width = 233
    Height = 137
  end
  object Edit1: TEdit
    Left = 152
    Top = 69
    Width = 121
    Height = 24
    Alignment = taRightJustify
    Color = clOlive
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    Text = ' '
  end
  object Edit2: TEdit
    Left = 152
    Top = 107
    Width = 121
    Height = 24
    Alignment = taRightJustify
    Color = clOlive
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
    Text = ' '
  end
  object Edit3: TEdit
    Left = 152
    Top = 150
    Width = 121
    Height = 24
    Alignment = taRightJustify
    Color = clOlive
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 2
    Text = ' '
  end
  object Button1: TButton
    Left = 53
    Top = 192
    Width = 75
    Height = 25
    Caption = 'Calcular'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 3
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 198
    Top = 192
    Width = 75
    Height = 25
    Caption = 'Sair'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 4
    OnClick = Button2Click
  end
end

Nenhum comentário:

Postar um comentário

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