Skip to content

Commit 446e1de

Browse files
idade média
1 parent 6c47db3 commit 446e1de

File tree

1 file changed

+93
-10
lines changed

1 file changed

+93
-10
lines changed

07_TheAlphaProject/ProjCLR/Form1.h

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ namespace ProjCLR {
8080

8181
private: System::Windows::Forms::Button^ btn_feminino;
8282
private: System::Windows::Forms::ToolStripMenuItem^ novoDelegadoSortearToolStripMenuItem;
83+
84+
85+
86+
87+
88+
private: System::Windows::Forms::ToolStripMenuItem^ médiaDeIdadesToolStripMenuItem;
8389
private: System::Windows::Forms::DataGridViewTextBoxColumn^ Nome;
8490
private: System::Windows::Forms::DataGridViewTextBoxColumn^ Localidade;
8591
private: System::Windows::Forms::DataGridViewTextBoxColumn^ AnoNasc;
@@ -130,6 +136,7 @@ namespace ProjCLR {
130136
this->novoDelegadoSortearToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
131137
this->estatísticasToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
132138
this->identificarOMaisVelhoToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
139+
this->médiaDeIdadesToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
133140
this->visualizaçãoToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
134141
this->mostrarToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
135142
this->mostrarOcultarSeletorDeLinhasToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
@@ -267,7 +274,10 @@ namespace ProjCLR {
267274
//
268275
// estatísticasToolStripMenuItem
269276
//
270-
this->estatísticasToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->identificarOMaisVelhoToolStripMenuItem });
277+
this->estatísticasToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {
278+
this->identificarOMaisVelhoToolStripMenuItem,
279+
this->médiaDeIdadesToolStripMenuItem
280+
});
271281
this->estatísticasToolStripMenuItem->Name = L"estatísticasToolStripMenuItem";
272282
this->estatísticasToolStripMenuItem->Size = System::Drawing::Size(76, 20);
273283
this->estatísticasToolStripMenuItem->Text = L"Estatísticas";
@@ -279,6 +289,13 @@ namespace ProjCLR {
279289
this->identificarOMaisVelhoToolStripMenuItem->Text = L"Identificar o mais velho";
280290
this->identificarOMaisVelhoToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::IdentificarOMaisVelhoToolStripMenuItem_Click);
281291
//
292+
// médiaDeIdadesToolStripMenuItem
293+
//
294+
this->médiaDeIdadesToolStripMenuItem->Name = L"médiaDeIdadesToolStripMenuItem";
295+
this->médiaDeIdadesToolStripMenuItem->Size = System::Drawing::Size(197, 22);
296+
this->médiaDeIdadesToolStripMenuItem->Text = L"Média de idades";
297+
this->médiaDeIdadesToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::MédiaDeIdadesToolStripMenuItem_Click);
298+
//
282299
// visualizaçãoToolStripMenuItem
283300
//
284301
this->visualizaçãoToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(3) {
@@ -398,6 +415,7 @@ namespace ProjCLR {
398415
this->Delegado->FillWeight = 80;
399416
this->Delegado->HeaderText = L"Delegado";
400417
this->Delegado->Name = L"Delegado";
418+
this->Delegado->Visible = false;
401419
//
402420
// Form1
403421
//
@@ -411,7 +429,7 @@ namespace ProjCLR {
411429
this->Controls->Add(this->menuStrip1);
412430
this->MainMenuStrip = this->menuStrip1;
413431
this->Name = L"Form1";
414-
this->Text = L"Form1";
432+
this->Text = L"Projecto Alpha";
415433
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
416434
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->EndInit();
417435
this->groupBox1->ResumeLayout(false);
@@ -471,29 +489,22 @@ namespace ProjCLR {
471489
ano_mais_velho = ano_linha;
472490
}
473491

474-
475492
dataGridView1->ClearSelection();
476493
listBox1->Items->Clear();
477494

478-
479495
// Recolher todos os elementos com o ano igual ao mais antigo
480496
for (size_t i = 1; i < n_linhas; i++)
481497
{
482498
ano_linha = Convert::ToInt16(dataGridView1->Rows[i]->Cells["AnoNasc"]->Value);
483499

484500
if (ano_mais_velho == ano_linha)
485501
{
486-
487502
nome = dataGridView1->Rows[i]->Cells["Nome"]->Value->ToString();
488-
489503
resultado = nome + ", " + ano_mais_velho;
490504
listBox1->Items->Add(resultado);
491-
492505
dataGridView1->Rows[i]->Selected = true;
493506
}
494-
495507
}
496-
497508
}
498509

499510

@@ -607,9 +618,80 @@ namespace ProjCLR {
607618

608619
// Repor o estado original da linha de introdução de registos.
609620
dataGridView1->AllowUserToAddRows = linha_intro;
621+
dataGridView1->Columns["Delegado"]->Visible = true;
622+
}
623+
624+
625+
// Gerar uma nota aleatória com ponderação (menos probabilidade de ocorrência nos extremos)
626+
private: int gerar_nota_especial()
627+
{
628+
int nota;
629+
int extra;
630+
631+
Random^ r = gcnew Random(); // inicializar gerador de numeros aleatórios
632+
nota = r->Next(8, 16); // gerar número aleatório para nota mediana
633+
634+
635+
extra = r->Next(0, 6);
636+
switch (extra)
637+
{
638+
case 0: // Nota muito baixa
639+
extra = r->Next(0, nota);
640+
nota = nota - extra;
641+
break;
642+
case 1: // Nota muito alta
643+
extra = r->Next(nota, 21);
644+
nota = nota + extra;
645+
break;
646+
default:
647+
break;
610648
}
649+
return nota;
650+
}
651+
652+
private: void calcular_media()
653+
{
654+
int n, idade_linha, ano_linha;
655+
int idade_max, idade_min;
656+
float idade_media, idade_desv_padrao=0;
657+
int idades[100];
658+
659+
bool linha_intro = dataGridView1->AllowUserToAddRows;
660+
dataGridView1->AllowUserToAddRows = false;
661+
n = dataGridView1->Rows->Count;
662+
663+
for (size_t i = 0; i < n; i++)
664+
{
665+
ano_linha = Convert::ToInt16(dataGridView1->Rows[i]->Cells["AnoNasc"]->Value);
666+
idade_linha = System::DateTime::Now.Year - ano_linha;
667+
idades[i] = idade_linha;
668+
}
669+
670+
// Calcular idade média, min e max
671+
idade_max = idade_min = idades[0];
672+
for (size_t i = 0; i < n; i++)
673+
{
674+
if (idade_max < idades[i]) idade_max = idades[i];
675+
if (idade_min > idades[i]) idade_min = idades[i];
676+
677+
idade_media += idades[i];
678+
}
679+
idade_media = (float)idade_media / (float)n;
680+
681+
682+
// Calcular desvio-padrão das idades
683+
684+
685+
686+
// Mostrar resultados
687+
listBox1->Items->Add("Média: " + Convert::ToString(idade_media));
688+
listBox1->Items->Add("Desv. Padrão: " + Convert::ToString(idade_desv_padrao));
689+
690+
691+
dataGridView1->AllowUserToAddRows = linha_intro;
692+
}
611693

612-
private: System::Void Btn_init_grid_Click(System::Object^ sender, System::EventArgs^ e) { init_grid(); }
694+
private: System::Void Btn_init_grid_Click(System::Object^ sender, System::EventArgs^ e) { init_grid(); }
613695
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { on_form_load(); }
614696
private: System::Void ToolStripMenuItem1_Click(System::Object^ sender, System::EventArgs^ e) { identificar_mais_velho(); }
615697
private: System::Void IdentificarOMaisVelhoToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {identificar_mais_velho();
@@ -620,5 +702,6 @@ private: System::Void MostrarToolStripMenuItem_Click(System::Object^ sender, Sys
620702
private: System::Void MostrarOcultarSeletorDeLinhasToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { alternar_row_headers(); }
621703
private: System::Void MostrarOcultarColunaDelegadoToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {alternar_col_delegado();}
622704
private: System::Void NovoDelegadoSortearToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { sortear_delegado();}
705+
private: System::Void MédiaDeIdadesToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {calcular_media();}
623706
};
624707
}

0 commit comments

Comments
 (0)