sábado, 25 de abril de 2020

C++ Builder - Calculando o volume de uma esfera

É impressionante a utilidade de corpos esféricos nos dias atuais.
Podemos citar algumas, mas seria impossível destacar
todos os locais onde ela se encontra.
Na maioria dos melhores esportes, na indústria,
nas máquinas, nos veículos motorizados, etc...
Por ser tão utilizadas, foi necessário a criação
de fórmulas matemáticas para cálculos precisos
de seu volume, área, circunferência e diâmetro.
Sabendo-se o valor do raio que é a distância entre
o centro da esfera e sua extremidade e o valor constante
do número irracional π (pi), dado por aproximadamente 3,14,
já somos capazes de criar um programa capaz de calcular
o seu volume com boa precisão e com certa facilidade.
E como estou usando o C++ Builder, aproveitei uma variável
nativa contida na Vcl, própria para cálculos matemáticos,
empregados em programação, e os resultados são
apresentados abaixo num vídeo com o programa funcionando.





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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
    : TForm ( Owner )
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
    Edit1 -> Font -> Name = "alarm clock";
    Edit1 -> SetFocus ( );
    Edit1 -> Font -> Color = clBlack;
    Edit1 -> Color = clOlive;
    Edit1 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit1 -> Font -> Size = 12;
    Edit1 -> Width = 105;
    Edit1 -> Height = 21;
    Edit1 -> Left = 170;
    Edit1 -> Top = 68;
    Edit1 -> Alignment = taRightJustify; //Orientação à direita do TEdit

    Image1 -> Width = 600;
    Image1 -> Height = 300;
    Image1 -> Left = 0;
    Image1 -> Top = 0;

    Image2 -> Width = 130;
    Image2 -> Height = 35;
    Image2 -> Left = 280;
    Image2 -> Top = 62;

    Image3 -> Width = 130;
    Image3 -> Height = 35;
    Image3 -> Left = 280;
    Image3 -> Top = 62;

    Image4 -> Width = 600;
    Image4 -> Height = 300;
    Image4 -> Left = 0;
    Image4 -> Top = 0;

    Image5 -> Width = 130;
    Image5 -> Height = 40;
    Image5 -> Left = 280;
    Image5 -> Top = 50;

    Image6 -> Width = 130;
    Image6 -> Height = 40;
    Image6 -> Left = 280;
    Image6 -> Top = 50;

    Label1 -> Visible = true;
    Label1 -> Font -> Size = 12;
    Label1 -> Font -> Name = "Venacti";
    Label1 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label1 -> Font -> Color = clBlack;
    Label1 -> Width = 130;
    Label1 -> Height = 20;
    Label1 -> Left = 270;
    Label1 -> Top = 110;

    Label2 -> Visible = true;
    Label2 -> Font -> Size = 12;
    Label2 -> Font -> Name = "Venacti";
    Label2 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label2 -> Font -> Color = clBlack;
    Label2 -> Width = 130;
    Label2 -> Height = 20;
    Label2 -> Left = 270;
    Label2 -> Top = 157;

    Label3 -> Visible = true;
    Label3 -> Font -> Size = 12;
    Label3 -> Font -> Name = "Venacti";
    Label3 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label3 -> Font -> Color = clBlack;
    Label3 -> Width = 330;
    Label3 -> Height = 20;
    Label3 -> Left = 270;
    Label3 -> Top = 204;

    Label4 -> Visible = true;
    Label4 -> Font -> Size = 12;
    Label4 -> Font -> Name = "Venacti";
    Label4 -> Font-> Style = TFontStyles ( ) << fsItalic << fsBold;
    Label4 -> Font -> Color = clBlack;
    Label4 -> Width = 330;
    Label4 -> Height = 20;
    Label4 -> Left = 270;
    Label4 -> Top = 249;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender) {
       Label_Manual ( Sender );
       Image4 -> Visible = false;
       Image6 -> Visible = false;
       Image5 -> Visible = false;

       Edit1 -> Height = 21;

       Label1 -> Visible = false;
       Label2 -> Visible = false;
       Label3 -> Visible = false;
       Label4 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2MouseEnter ( TObject *Sender ) {
        Image3 -> Visible = true;
        Image2 -> Visible = false;
        Image5 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image3MouseLeave ( TObject *Sender ) {
        Image3 -> Visible = false;
        Image2 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image3Click ( TObject *Sender ) {
       double Raio, Dim, Circumferencia, Area, Volume;

       Raio = Edit1 -> Text.ToDouble ( );
       Dim = Raio * 2;
       Circumferencia = Raio * 2 * M_PI;
       Area = Raio * Raio * M_PI;
       Volume = Raio * Raio * Raio * 4.00 * M_PI / 3;

       Label1 -> Visible = true;
       Label2 -> Visible = true;
       Label3 -> Visible = true;
       Label4 -> Visible = true;

       Label1 -> Caption = FloatToStrF ( Dim, ffFixed, 8, 2 );
       Label2 -> Caption = FloatToStrF ( Circumferencia, ffFixed, 8, 2 );
       Label3 -> Caption = FloatToStrF ( Area, ffFixed, 8, 2 );
       Label4 -> Caption = FloatToStrF ( Volume, ffFixed, 8, 2 );

       Image1 -> Visible = false;
       Image2 -> Visible = false;
       Image3 -> Visible = true;
       Image4 -> Visible = true;
       Image6 -> Visible = true;
       Image5 -> Visible = true;
       Edit1 -> Top = 63;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image5MouseEnter ( TObject *Sender ) {
    Image5 -> Visible = false;
    Image6 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image6MouseLeave ( TObject *Sender ) {
    Image6 -> Visible = false;
    Image5 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image6Click ( TObject *Sender ) {
         Image1 -> Visible = true;
         Image2 -> Visible = true;
         Image3 -> Visible = false;
         Image4 -> Visible = false;
         Image5 -> Visible = false;
         Image6 -> Visible = false;

         Edit1 -> Top = 68;
         Edit1 -> Clear ( );

         Label1 -> Caption = " ";
         Label2 -> Caption = " ";
         Label3 -> Caption = " ";
         Label4 -> Caption = " ";
}
//---------------------------------------------------------------------------


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

#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.Graphics.hpp>
#include <Vcl.Imaging.pngimage.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TImage *Image1;
    TEdit *Edit1;
    TImage *Image2;
    TImage *Image3;
    TImage *Image4;
    TImage *Image5;
    TImage *Image6;
    TLabel *Label1;
    TLabel *Label2;
    TLabel *Label3;
    TLabel *Label4;
    void __fastcall Label_Manual ( TObject *Sender );
    void __fastcall FormShow ( TObject *Sender );
    void __fastcall Image2MouseEnter ( TObject *Sender );
    void __fastcall Image3MouseLeave ( TObject *Sender );
    void __fastcall Image3Click ( TObject *Sender );
    void __fastcall Image5MouseEnter ( TObject *Sender );
    void __fastcall Image6MouseLeave ( TObject *Sender );
    void __fastcall Image6Click ( 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.