segunda-feira, 25 de fevereiro de 2019

C++ builder - Calculando médias em struct

As colunas e as linhas do StringGrid
foram carregadas com os membros da struct Escola.
Cada coluna do tipo float equivale
as notas obtidas pelo desempenho
de quatro alunos de uma determinada escola.
Mas o que queremos mesmos é saber a média
obtida por cada um dos quatros alunos
que prestaram exames do ensino médio.
Na varredura do laço for, as quatros
variáveis globais do tipo float,
recebem as cargas das variáveis membros
de struct Escola, e por serem globais,
ficou fácil trabalhar com elas dentro
dos métodos dos 4 RadioButton.



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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <string.h>
#include <conio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner ) :
TForm ( Owner ) {
}
float media;
float res_1 = 0, res_2 = 0, res_3 = 0, res_4 = 0;
struct Escola {
char *materias;
float notas_1;
float notas_2;
float notas_3;
float notas_4;
};
Escola Alunos [ 4 ] =
        {{"Ciências Humanas e Suas Tecnologias     ", 8.5, 6.8, 5.6, 7.5 },
{ "Ciências da Natureza e suas Tecnologias ", 9.5, 5.3, 5.8, 8.9 },
{ "Linguagens e Códigos e suas Tecnologias ", 8.6, 6.4, 8.1, 9.3 },
{ "Matemática e suas Tecnologias           ", 9.7, 6.5, 5.1, 7.2 }};
void __fastcall TForm1::RadioButton1Click ( TObject *Sender ) {
Canvas -> Font -> Size = 11;
Canvas -> Font -> Name = "arial";
Canvas -> Font -> Color = clBlue;
media = res_1 / 4;
if ( media < 7 ) {
Beep ( 1000, 500 );
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 50, 197, "Reprovado");
Canvas -> TextOut( 50, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 90, 217, " " + media_1 );
}
if ( media >= 7 ) {
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 50, 197, "Aprovado");
Canvas -> TextOut( 50, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 90, 217, " " + media_1 );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click ( TObject *Sender ) {
Canvas -> Font -> Size = 11;
Canvas -> Font -> Name = "arial";
Canvas -> Font -> Color = clBlue;
media = res_2 / 4;
if ( media < 7 ) {
Beep ( 1000, 500 );
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 192, 197, "Reprovado");
Canvas -> TextOut( 192, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 232, 217, " " + media_1 );
}
if ( media >= 7 ) {
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 192, 197, "Aprovado");
Canvas -> TextOut( 192, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 232, 217, " " + media_1 );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton3Click ( TObject *Sender ) {
Canvas -> Font -> Size = 11;
Canvas -> Font -> Name = "arial";
Canvas -> Font -> Color = clBlue;
media = res_3 / 4;
if ( media < 7 ) {
Beep ( 1000, 500 );
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 336, 197, "Reprovado");
Canvas -> TextOut( 336, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 376, 217, " " + media_1 );
}
if ( media >= 7 ) {
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 336, 197, "Aprovado");
Canvas -> TextOut( 336, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 376, 217, " " + media_1 );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton4Click ( TObject *Sender ) {
Canvas -> Font -> Size = 11;
Canvas -> Font -> Name = "arial";
Canvas -> Font -> Color = clBlue;
media = res_4 / 4;
if ( media < 7 ) {
Beep ( 1000, 500 );
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 480, 197, "Reprovado");
Canvas -> TextOut( 480, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 232, 217, " " + media_1 );
}
if ( media >= 7 ) {
//Removendo caracteres a direita da String
AnsiString media_1 = String ( media );
media_1.Delete ( 5, 15 );
Canvas -> TextOut( 480, 197, "Aprovado");
Canvas -> TextOut( 480, 217, "Média" );
Canvas -> Font -> Color = clRed;
//Impressão após a remoção
Canvas -> TextOut( 520, 217, " " + media_1 );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton5Click ( TObject *Sender )
{
Canvas -> Font -> Size = 13;
Canvas -> Font -> Name = "arial";
Canvas -> Font -> Color = clRed;
Canvas -> TextOut( 280, 265, "Samuel Lima");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnCreate ( TObject *Sender ) {
Label1 -> Left = 120;
Label1 -> Top = 15;
Label1 -> Caption = "C++ BUILDER - CALCULANDO MÉDIAS EM STRUCT ";
Label2 -> Left = 30;
Label2 -> Top = 45;
Label2 -> Caption = "    Ord                        Matérias                              AL1 AL2 AL3 AL4";
int a = 0, i;
StringGrid1 -> ColWidths [ 0 ] = 30;
StringGrid1 -> ColWidths [ 1 ] = 320;
StringGrid1 -> ColWidths [ 2 ] = 30;
StringGrid1 -> ColWidths [ 3 ] = 30;
StringGrid1 -> ColWidths [ 4 ] = 30;
StringGrid1 -> ColWidths [ 5 ] = 30;
for ( i = 0; i < 4; i++ ) {
a++;
StringGrid1 -> Cells [ 0 ] [ i ] = a;
StringGrid1 -> Cells [ 1 ] [ i ] = Alunos [ i ].materias;
StringGrid1 -> Cells [ 2 ] [ i ] = Alunos [ i ].notas_1;
StringGrid1 -> Cells [ 3 ] [ i ] = Alunos [ i ].notas_2;
StringGrid1 -> Cells [ 4 ] [ i ] = Alunos [ i ].notas_3;
StringGrid1 -> Cells [ 5 ] [ i ] = Alunos [ i ].notas_4;
res_1 = res_1 + Alunos [ i ].notas_1;
res_2 = res_2 + Alunos [ i ].notas_2;
res_3 = res_3 + Alunos [ i ].notas_3;
res_4 = res_4 + Alunos [ i ].notas_4;
}
}

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


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Grids.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TStringGrid *StringGrid1;
TRadioButton *RadioButton1;
TRadioButton *RadioButton2;
TRadioButton *RadioButton3;
TRadioButton *RadioButton4;
TShape *Shape1;
TLabel *Label1;
TLabel *Label2;
TRadioButton *RadioButton5;
void __fastcall OnCreate(TObject *Sender);
void __fastcall RadioButton1Click(TObject *Sender);
void __fastcall RadioButton2Click(TObject *Sender);
void __fastcall RadioButton3Click(TObject *Sender);
void __fastcall RadioButton4Click(TObject *Sender);
void __fastcall RadioButton5Click(TObject *Sender);

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

object Form1: TForm1
  Left = 439
  Top = 165
  Caption = 'C++ BUILDER - CALCULANDO M'#201'DIAS EM STRUCT'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clRed
  Font.Height = 0
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnCreate = OnCreate
  PixelsPerInch = 96
  TextHeight = 19
  object Shape1: TShape
    Left = 0
    Top = 0
    Width = 601
    Height = 297
    Brush.Color = clCream
    Pen.Width = 10
  end
  object Label2: TLabel
    Left = 0
    Top = 0
    Width = 5
    Height = 19
    Caption = ' '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = 0
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label1: TLabel
    Left = 0
    Top = 0
    Width = 5
    Height = 19
    Caption = ' '
  end
  object StringGrid1: TStringGrid
    Left = 48
    Top = 72
    Width = 505
    Height = 121
    BevelWidth = 3
    Color = clBlack
    ColCount = 6
    DefaultColWidth = 24
    FixedColor = clRed
    FixedCols = 0
    RowCount = 4
    FixedRows = 0
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    GradientEndColor = clBlack
    GradientStartColor = clBlack
    GridLineWidth = 5
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goThumbTracking]
    ParentFont = False
    TabOrder = 0
    ColWidths = (
      24
      24
      24
      24
      24
      24)
  end
  object RadioButton1: TRadioButton
    Left = 48
    Top = 240
    Width = 73
    Height = 17
    Caption = 'Aluno 1'
    TabOrder = 1
    OnClick = RadioButton1Click
  end
  object RadioButton2: TRadioButton
    Left = 192
    Top = 240
    Width = 73
    Height = 17
    Caption = 'Aluno 2'
    TabOrder = 2
    OnClick = RadioButton2Click
  end
  object RadioButton3: TRadioButton
    Left = 336
    Top = 240
    Width = 73
    Height = 17
    Caption = 'Aluno 3'
    TabOrder = 3
    OnClick = RadioButton3Click
  end
  object RadioButton4: TRadioButton
    Left = 480
    Top = 240
    Width = 73
    Height = 17
    Caption = 'Aluno 4'
    TabOrder = 4
    OnClick = RadioButton4Click
  end
  object RadioButton5: TRadioButton
    Left = 224
    Top = 264
    Width = 49
    Height = 17
    Caption = 'Por:'
    TabOrder = 5
    OnClick = RadioButton5Click
  end
end


Nenhum comentário:

Postar um comentário

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