Abaixo temos a interface com: 3 botões, 1 listbox e 1 opendialog:
Na ativação do form, a aplicação irá requisitar a identificação do arquivo a ser processado:
Após isso, fica a critério do usuário pedir estatística geral ou detalhada:
A seguir o código para a classe TForm1:
1) Aplicativo.h
//---------------------------------------------------------------------------
#ifndef AplicativoH
#define AplicativoH
//-------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include <stdio.h>
#include "Estatistica.h"
//-------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TListBox *ListBox1;
TButton *Button2;
TOpenDialog *OpenDialog1;
TButton *Button3;
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall FormActivate(TObject *Sender);
private: // User declarations
FILE *fh;
Estatistica *geral, *detalhada[27];
int uf, qtd;
bool abrir;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//-------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//-------------------------------------------------------------------------
#endif
2) Aplicativo.cpp
//-------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Aplicativo.h"
//-------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
OpenDialog1->Title = "Abrir Dados";
OpenDialog1->Filter = "Parâmetros (*.txt)|*.txt";
OpenDialog1->DefaultExt = "txt";
if ( OpenDialog1->Execute() == false ){
abrir = false;
}
else{
abrir = true;
}
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// botão Sair
Application->Terminate() ;
}
//-------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
// fechar form
Application->Terminate() ;
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// botão Estatística Geral
if ( abrir == false ) return;
fh = fopen(OpenDialog1->FileName.c_str(), "r");
geral = new Estatistica(); // instanciar objeto
while ( true ){
fscanf(fh, "%d %d", &uf, &qtd);
if ( uf == 0 ) break;
geral->Acumular( qtd );
}
geral->Calcular();
ListBox1->Items->Clear();
ListBox1->Items->Add( "Média Geral: " + String( geral->media ) );
fclose( fh );
delete geral; // destruir objeto
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// botão Estatística Detalhada
int i;
if ( abrir == false ) return;
fh = fopen(OpenDialog1->FileName.c_str(), "r");
if ( fh == NULL ) return;
for (i = 0; i < 27; i++){
detalhada[i] = new Estatistica(); // instanciar objeto[i]
}
while ( true ){
fscanf(fh, "%d %d", &uf, &qtd);
if ( uf == 0 ) break;
detalhada[ uf - 1 ]->Acumular( qtd );
}
ListBox1->Items->Clear();
for (i = 0; i < 27; i++){
detalhada[i]->Calcular();
ListBox1->Items->Add( "Média[" +
String(i + 1) + "]: " +
String( detalhada[i]->media ) );
}
fclose( fh );
delete [] detalhada; // destruir array de objetos
}
//-------------------------------------------------------------------------
Agora temos o código para a classe Estatistica:
1) Estatistica.h
//-------------------------------------------------------------------------
#ifndef EstatisticaH
#define EstatisticaH
class Estatistica{
private: int cont;
private: int soma;
public: int media;
Estatistica(); // construtor
public: void Acumular( int );
public: void Calcular();
~Estatistica(); // destrutor
};
//-------------------------------------------------------------------------
#endif
2) Estatistica.cpp
//-------------------------------------------------------------------------
#pragma hdrstop
#include "Estatistica.h"
//-------------------------------------------------------------------------
#pragma package(smart_init)
Estatistica :: Estatistica(){ // construtor
cont = 0;
soma = 0;
media = 0;
return;
}
void Estatistica :: Acumular( int q ){
cont ++;
soma += q;
return;
}
void Estatistica :: Calcular() {
if ( cont > 0 ){
media = soma / cont;
}
return;
}
Estatistica :: ~Estatistica(){ // destrutor
return;
}
//-------------------------------------------------------------------------
Por último, aqui estão os dados utilizados nos testes:
1 2000
2 4000
3 3500
4 3600
5 1200
3 500
2 800
2 3800
4 1250
5 540
1 6500
3 420
1 8700
3 9500
2 1240
1 370
5 850
3 1230
2 3290
1 4320
0 0
Nenhum comentário:
Postar um comentário