O projeto segue a mesma linha como feito no CBuilder. A primeira imagem anexada é da interface gráfica.
A segunda, já em execução do "openFileDialog".
A terceira, quando solicitada a estatística geral.
E a última imagem a seguir, da estatística detalhada.
A seguir temos o código para a aplicação desenvolvida em MS VS 2005 C++:
1) Arquivo "Formulario.h"
#pragma once
#include <stdio.h>
#include "Estatisticas.h"
namespace oop1a {
// O array de ponteiros para objetos da classe Estatistica não pode ser definida como atributo
// de classe, como feito no CBuilder:
Estatisticas *detalhada[27];
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
// namespace necessária ao uso de Marshal para converter string em char:
using namespace System::Runtime::InteropServices;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
private:
Estatisticas *geral;
FILE *fh;
int uf, qtd;
bool abrir;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::ListBox^ listBox1;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
this->button3 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 69);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(154, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Sai&r";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(12, 12);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(154, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Estatística Geral";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->Location = System::Drawing::Point(209, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(337, 264);
this->listBox1->TabIndex = 2;
//
// button3
//
this->button3->Location = System::Drawing::Point(12, 41);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(154, 23);
this->button3->TabIndex = 3;
this->button3->Text = L"Estatítica Detalhada";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(626, 357);
this->Controls->Add(this->button3);
this->Controls->Add(this->listBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Estatísticas";
this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
this->Shown += gcnew System::EventHandler(this, &Form1::Form1_Shown);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Shown(System::Object^ sender, System::EventArgs^ e) {
// quando da exibição do form pela primeira vez
openFileDialog1->Title = "Abrir Dados";
openFileDialog1->Filter = "Parâmetros (*.txt)|*.txt";
openFileDialog1->DefaultExt = "txt";
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ){
abrir = true;
}
else{
abrir = false;
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
// botão Sair
Application::Exit();
}
private: System::Void Form1_FormClosed(System::Object^ sender, System::Windows::Forms::FormClosedEventArgs^ e) {
// form Close
Application::Exit();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
// botão Estatística Geral
if ( abrir == false ) return;
// converter string em char e abrir o arquivo
System::String ^ filename = openFileDialog1->FileName ;
char* filenameANSI = (char*)(void*)Marshal::StringToHGlobalAnsi(filename);
fh = fopen( filenameANSI, "r" );
Marshal::FreeHGlobal( (IntPtr)filenameANSI );
geral = new Estatisticas(); // 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: " +
System::Convert::ToString ( geral->media ) );
fclose( fh );
delete geral; // destruir objeto
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
// botão Estatística Detalhada
int i;
if ( abrir == false ) return;
// converter string em char e abrir o arquivo
System::String ^ filename = openFileDialog1->FileName ;
char* filenameANSI = (char*)(void*)Marshal::StringToHGlobalAnsi(filename);
fh = fopen( filenameANSI, "r" );
Marshal::FreeHGlobal( (IntPtr)filenameANSI );
for (i = 0; i < 27; i++){
detalhada[i] = new Estatisticas(); // instanciar objeto[i]
}
while ( true ){
fscanf(fh, "%d %d", &uf, &qtd);
if ( uf == 0 ) break;
detalhada[ uf - 1 ]->Acumular( qtd );
}
geral->Calcular();
listBox1->Items->Clear();
for (i = 0; i < 27; i++){
detalhada[i]->Calcular();
listBox1->Items->Add( "Média[" +
System::Convert::ToString(i + 1) + "]: " +
System::Convert::ToString( detalhada[i]->media ) );
}
fclose( fh );
// a destruição dos objetos precisou ser individualmente:
for (i = 0; i < 27; i++){
delete detalhada[i]; // destruir objeto[i]
}
}
};
}
2) Arquivo "Estatistica.h"
// Estatisticas.h
class Estatisticas {
public: int cnt;
public: int somatoria;
public: int media;
Estatisticas( ); // Construtor
public: void Acumular( int );
public: void Calcular( );
~Estatisticas( ); // Destrutor
};
3) Arquivo "Estatistica.cpp"
#include "stdafx.h"
#include "Estatisticas.h"
Estatisticas :: Estatisticas( ) // Construtor
{
cnt = 0;
somatoria = 0;
media = 0;
}
void Estatisticas :: Acumular( int a )
{
cnt ++;
somatoria += a;
return;
}
void Estatisticas :: Calcular( )
{
if ( cnt > 0){
media = somatoria / cnt;
}
return;
}
Estatisticas :: ~Estatisticas( ) // Destrutor
{
}
4) Arquivo "Dados.txt"
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