domingo, 6 de março de 2022

C++ Builder - renomeando tipos com typedef

 

Todos devem saber que é perfeitamente possível a criação
de novos nomes para qualquer tipo suportado pelo C usando
o comando typedef cuja sintaxe é: typedef tipo ( novo_nome ).
A declaração de um novo nome para um tipo qualquer aceito,
é preferivelmente feito logo após as declarações das bibliotecas
que serão utiizadas pelo seu programa.
O aprendiz de linguagem C, deve ficar sabendo que com isto
não estamos criando um novo tipo de dados, más apenas renomeando
um tipo existente e aceito no C.
Um programador de C, deve achar isto particularmente muito útil usar
O comando typedef para renomear tipos mais complexos, como as estruturas.
As estruturas criadas no exemplo que trago agora foram bem definidas
como tipos através do comando typedef.
Na verdade o C, entende uma estrutura como qualquer outro tipo de dado,
e o novo nome é tido como um genuíno identificador.
Aplicar typedef em declarações de variáveis estrutura, traz simplicidade
e clareza no desenvolvimento de projetos, e o seu uso se estende
as uniões e enumerações.
Neste programa fiz algumas renomeações de tipos usando
o comando typedef, aplicado a alguns tipos de estrutura, procurando
esclarecer na prática como usar este comando
que certamente o veremos muito na medida que nossos programas
forem ficando mais avançados.

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

#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"

#include <sstream>
#include <iostream>
#include <iomanip>

using namespace std;
using std::setw;

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

String str_1;
TLabel *Lbl;
int x;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
void __fastcall Label_2 ( int X, int Y,
                          int R, int G, int B, String FontName, int S,
                          TFontStyles style, int R1, int G1, int B1, String str_1 );
//--------------------------------------------------------------------------
typedef char Dirigente;
typedef char Nomes;
typedef struct Diretor Direcao;
typedef struct Notas Notas_Alunos;
typedef float Vet_Notas;
typedef struct Escola Aula;

//---------------------------------------------------------------------------
struct Diretor {
    Dirigente Dire_tor [ 48 ];
};
//---------------------------------------------------------------------------
Direcao Diretor_De_Escola [ ] = {
    "Um Diretor de Escola da\n",
    "rede Estadual de ensino\n",
    "listou seus melhores alunos.",
    "Pressione qualquer tecla\n    para conhecê-los"};
//---------------------------------------------------------------------------
struct Notas {
    Vet_Notas No_tas = 0.00;
};
//---------------------------------------------------------------------------
Notas_Alunos Aluno [ ] = { 9.5, 8.9, 6.4, 7.5, 8.2, 7.7, 6.9, 9.3, 9.1, 8.5 };
//---------------------------------------------------------------------------
struct Escola {
    Nomes nome [ 16 ];
};
//---------------------------------------------------------------------------
Aula Alu_no [ ] = { { "Ana Célia      " },
                    { "Eder Costa     " },
                    { "Humberto Gomes " },
                    { "Dijalma Lacerda" },
                    { "Caroline Silva " },
                    { "Igor Gonçalves " },
                    { "Bruna Carla    " },
                    { "Fábio Quadros  " },
                    { "Geany Barros   " },
                    { "Jaqueline Vega " }};

//----------------------------------------------------------------------------
void __fastcall TForm1::Label_2 ( int X, int Y,
                                  int R, int G, int B, String FontName, int S,
                                  TFontStyles style, int R1, int G1, int B1, String str_1 ) {

    Label1 = new TLabel ( Form1 );
    Label1 -> Parent = Form1;
    Label1 -> AutoSize = true;
    Label1 -> Transparent = false;
    Label1 -> Color = static_cast < TColor > ( RGB ( R, G, B ) );
    Label1 -> WordWrap = false;
    Label1 -> Font -> Name = FontName;
    Label1 -> Font -> Size = S;
    Label1 -> Font-> Style = style;
    Label1 -> Font -> Color = static_cast < TColor > ( RGB ( R1, G1, B1 ) );
    Label1 -> Left = X;
    Label1 -> Top = Y;
    Label1 -> Caption = str_1;
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Informe_2 ( TObject *Sender ) {
    Label_2 ( 230, 250, 244, 244, 244, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 255, 0, 0, "Por: " );

    Label_2 ( 270, 250, 244, 244, 244, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Samuel Lima" );

    Label_2 ( 230, 265, 244, 244, 244, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 255, "sa_sp10@hotmail.com" );
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Imprime_Nomes_Alunos ( ){
    //Limpa a tela aplicando a mesma cor do form sobre as fontes
    for ( int i = 0; i < 10; i++ ) {
        for ( int j = 0; j < 10; j++ ) {
            Label_2 ( 180 + j * 25, 50 + i * 14, 240, 240, 240, "Consolas", 11,
                      TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "    " );
        }
    }

    Label_2 ( 180, 40, 144, 238, 144, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Veja abaixo os nomes dos Alunos " );

    for ( int i = 0; i < 10; i++ ) {
        Label_2 ( 240, 80 + i * 17, 244, 244, 244, "Consolas", 11,
                  TFontStyles ( ) << fsBold << fsItalic, 0, 100, 0, Alu_no [ i ].nome );
    }


    for ( int i = 0; i < 3; i++ ) {
        for ( int j = 0; j < 10; j++ ) {
            Label_2 ( 170 + j * 25, 250 + i * 10, 240, 240, 240, "Consolas", 11,
                      TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "    " );
        }
    }

    Label_2 ( 210, 270, 144, 238, 144, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "PRESSIONE QUALQUER TECLA" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Imprime_Nomes_Alunos_Ordenados ( ) {
    //Limpa a tela aplicando a mesma cor do form sobre as fontes
    for ( int i = 0; i < 10; i++ ) {
        for ( int j = 0; j < 10; j++ ) {
            Label_2 ( 240 + j * 25, 50 + i * 14, 240, 240, 240, "Consolas", 11,
                      TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "        " );
        }
    }

    int i, j;
    for ( i = 0; i < 10 - 1; i++ ) {
        for ( j = i + 1; j < 10; j++ ) {
            if ( strcmp ( Alu_no [ i ].nome, Alu_no [ j ].nome ) > 0 ) {
                Escola aux = Alu_no [ i ];
                Alu_no [ i ] = Alu_no [ j ];
                Alu_no [ j ] = aux;
            }
        }
    }

    Label_2 ( 180, 40, 144, 238, 144, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Abaixo os nomes dos alunos ordenados" );

    for ( int i = 0; i < 10; i++ ) {
        Label_2 ( 240, 80 + i * 17, 240, 240, 240, "Consolas", 11,
                  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 139, Alu_no [ i ].nome );
    }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Imprime_Nomes_Notas ( ) {
    //Limpa a tela aplicando a mesma cor do form sobre as fontes
    for ( int i = 0; i < 15; i++ ) {
        for ( int j = 0; j < 10; j++ ) {
            Label_2 ( 240 + j * 25, 50 + i * 14, 240, 240, 240, "Consolas", 11,
                      TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "    " );
        }
    }
    int i;

    string str_1;

    const  char *Conv_1;

    Label_2 ( 160, 40, 144, 238, 144, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Abaixo estão os nomes e as notas dos alunos" );

    for ( i = 0; i < 10; i++ ) {
        //stringstream deve ser declarado aqui
        stringstream sst_1;

        Label_2 ( 210, 80 + i * 18, 240, 240, 240, "Consolas", 11,
                  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, Alu_no [ i ].nome );
        sst_1 << endl;
        sst_1 << setw ( 4 ) << Aluno [ i ].No_tas;
        str_1 += sst_1.str ( );

        //Convertendo std::string em const  char
        Conv_1 = str_1.c_str ( );

    }
    Label_2 ( 350, 62, 240, 240, 240, "Consolas", 11,
              TFontStyles ( ) << fsItalic, 255, 0, 0, Conv_1 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Imprime_Notas ( ) {
    //Limpa a tela aplicando a mesma cor do form sobre as fontes
    for ( int i = 0; i < 15; i++ ) {
        for ( int j = 0; j < 10; j++ ) {
            Label_2 ( 150 + j * 35, 40 + i * 15, 240, 240, 240, "Consolas", 11,
                      TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "     " );
        }
    }

    int a, i;
    string str_1;
    const  char *Conv_1;

    Label_2 ( 180, 50, 144, 238, 144, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Estas são as notas dos alunos" );

    for ( i = 0; i < 10; i++ ) {
        //stringstream deve ser declarado aqui
        stringstream sst_1;
        sst_1 << setw ( 4 ) << Aluno [ i ].No_tas << " ";
        str_1 += sst_1.str ( );

        //Convertendo std::string em const  char
        Conv_1 = str_1.c_str ( );
    }
    Label_2 ( 100, 80, 240, 240, 240, "Consolas", 11,
              TFontStyles ( ) << fsItalic, 255, 0, 0, Conv_1 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Imprime_Notas_Ordenadas ( ) {

    int a = 0, i, j, ord;
    float aux;
    string str_1;
    const  char *Conv_1;

    Label_2 ( 180, 130, 144, 238, 144, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "Estas são as notas dos alunos ordenadas" );

    ord = 0;
    while ( ord == 0 ) {
        ord = 1;
        for ( a = 0; a < 10; a++ ) {
            if ( Aluno [ a ].No_tas > Aluno [ ( a + 1 ) ].No_tas ) {
                aux = Aluno [ a ].No_tas;
                Aluno [ a ].No_tas = Aluno [ ( a + 1 ) ].No_tas;
                Aluno [ ( a + 1 ) ].No_tas = aux;
                ord = 0;
            }
        }
    }

    for ( i = 1; i <= 10; i++ ) {
        //stringstream deve ser declarado aqui
        stringstream sst_1;
        sst_1 << setw ( 4 ) << Aluno [ i ].No_tas << " ";
        str_1 += sst_1.str ( );
        //Convertendo std::string em const  char
        Conv_1 = str_1.c_str ( );
    }
    Label_2 ( 100, 160, 240, 240, 240, "Consolas", 11,
              TFontStyles ( ) << fsItalic, 255, 0, 0, Conv_1 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown ( TObject *Sender, WORD &Key,
                                      TShiftState Shift ) {
    x++;

    if ( x == 1 ) {
        Imprime_Nomes_Alunos ( );
    }
    if ( x == 2 ) {
        Imprime_Nomes_Alunos_Ordenados ( );
    }
    if ( x == 3 ) {
        Imprime_Nomes_Notas ( );
    }
    if ( x == 4 ) {
        Imprime_Notas ( );
    }
    if ( x == 5 ) {
        Imprime_Notas_Ordenadas ( );
        Label_2 ( 180, 265, 244, 244, 244, "Consolas", 15,
                  TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, "                          " );
        Informe_2 ( Sender );
    }
    if ( x == 6 )
        Close ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
    string str_1;
    string str_2;
    string str_3;

    const  char *Conv_1;
    const  char *Conv_2;
    const  char *Conv_3;

    for ( int i = 0; i < 3; i++ ) {
        stringstream sst_1;
        sst_1 << setw ( 7 ) << + Diretor_De_Escola [ i ].Dire_tor;
        str_1 += sst_1.str ( );
    }
    //Convertendo std::string em const  char
    Conv_1 = str_1.c_str ( );
    Label_2 ( 180, 50, 176, 196, 222, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, Conv_1 );

    Label_2 ( 200, 250, 135, 206, 250, "Consolas", 11,
              TFontStyles ( ) << fsBold << fsItalic, 0, 0, 0, Diretor_De_Escola [ 3 ].Dire_tor );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender ) {
    Canvas -> Pen -> Width = 10;

    Canvas -> Pen -> Color = clRed;
    //Colorindo as bordas do roundrect ( Moldura ) com rgb
    Canvas -> Pen -> Color = static_cast < TColor > ( RGB ( 0, 100, 0 ) );
    Canvas -> RoundRect ( 05, 05, 595, 295, 25, 25 );
    Canvas -> Font -> Size = 13;
    Canvas -> Font -> Name = "Arial";
    Canvas -> Font -> Color = clRed;
    Canvas -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic;

    Canvas -> TextOut ( 130, 12, "C++ BUILDER - TYPEDEF RENOMEANDO TIPOS" );
}
//---------------------------------------------------------------------------







Nenhum comentário:

Postar um comentário

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