sexta-feira, 24 de abril de 2020

C++ Builder - Cálculo de variação percentual

Quando queremos descobrir a diferença entre dois valores,
usamos um pequeno cálculo matemático numa fórmula aplicada.
se dois valores são comparados utilizando a fórmula abaixo:
Val2 - Val1 / Val1 x 100, podemos dizer que a relação
entre os dois é a variação percentual entre eles.
Se o resultado destas comparações forem positivos,
na matemática dizemos que houve um aumento percentual,
e se for negativo uma diminuição negativa foi constatada,
e para deixar isto mais claro criamos este programa
no C++ Builder para cálculos destas variações,
acompanhem no vídeo, e se tiver interesse clique
no link e confira como isto foi feito.





//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
    : TForm ( Owner ) {
}
int x = 0;
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 = 272;
    Edit1 -> Top = 70;
    Edit1 -> Alignment = taRightJustify; //Orientação à direita do TEdit

    Edit2 -> Font -> Name = "alarm clock";
    Edit2 -> Font -> Color = clBlack;
    Edit2 -> Color = clOlive;
    Edit2 -> Font-> Style = TFontStyles ( ) << fsBold;
    Edit2 -> Font -> Size = 12;
    Edit2 -> Width = 105;
    Edit2 -> Height = 21;
    Edit2 -> Left = 272;
    Edit2 -> Top = 98;
    Edit2 -> Alignment = taRightJustify; //Orientação à direita do TEdit

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

    Image2 -> Visible = true;
    Image2 -> Width = 130;
    Image2 -> Height = 35;
    Image2 -> Left = 430;
    Image2 -> Top = 80;

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

    Image4 -> Width = 160;
    Image4 -> Height = 36;
    Image4 -> Left = 430;
    Image4 -> Top = 80;

    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 = 380;
    Label1 -> Top = 150;

    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 = 380;
    Label2 -> Top = 185;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2Click ( TObject *Sender ) {
       double Valor_Antigo = 0.00, Valor_Novo = 0.00, Valor_Resp;
       double Dif_Percent;

       Valor_Antigo = StrToFloat ( Edit1 -> Text );
       Valor_Novo = StrToFloat ( Edit2 -> Text );
       Valor_Resp = ( Valor_Novo - Valor_Antigo );

       Dif_Percent = ( Valor_Resp / Valor_Antigo );
       Dif_Percent = ( Dif_Percent * 100 );

       Label1 -> Caption = FloatToStrF ( Valor_Resp, ffFixed, 8, 3 );
       Label2 -> Caption = FloatToStrF ( Dif_Percent, ffFixed, 8, 2 );
       Label2 -> Caption = Label2 -> Caption + " %";

        Label_Manual ( Sender );
        Image3 -> Visible = true;
        Image4 -> Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image4Click ( TObject *Sender ) {
        Label_Manual ( Sender );
        Image3 -> Visible = false;
        Image4 -> Visible = false;
        Label1 -> Caption = " ";
        Label2 -> Caption = " ";
        Edit1 -> Clear ( );
        Edit2 -> Clear ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
    Label_Manual ( Sender );
    Image1 -> Visible = true;
    Image2 -> Visible = true;
    Image3 -> Visible = false;
    Image4 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender ) {
 Label_Manual ( Sender );
}
//---------------------------------------------------------------------------
 


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

#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>
#include <Vcl.Imaging.GIFImg.hpp>
#include <Vcl.Graphics.hpp>
#include <Vcl.Imaging.pngimage.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TImage *Image1;
    TImage *Image2;
    TImage *Image3;
    TImage *Image4;
    TLabel *Label1;
    TLabel *Label2;
    TEdit *Edit1;
    TEdit *Edit2;
    void __fastcall Label_Manual ( TObject *Sender );
    void __fastcall Image2Click(TObject *Sender);
    void __fastcall Image4Click(TObject *Sender);
    void __fastcall FormShow(TObject *Sender);
    void __fastcall FormPaint(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.