# Foreach | Fund. Lógica
**Author:** Matheus Rafael Morato Rocha
**Turma:** InfoC
**Número:** 40
[TOC]
## Nível 1
```csharp=
using System;
using System.Collections.Generic;
public class Retangulo
{
public double Base {get; set;}
public double Altura {get; set;}
}
public class Notas
{
public double Nota1 {get; set;}
public double Nota2 {get; set;}
public double Nota3 {get; set;}
}
public class TreinoFocadoA
{
public int SomaImpares (List<int> numeros)
{
int impares = 0;
foreach (int item in numeros)
{
if (item % 2 == 1)
{
impares = impares + item;
}
}
return impares;
}
public List<int> CalcularCubo (List<int> numeros)
{
List<int> Cubo = new List<int>();
foreach (int item in numeros)
{
int a = item * item * item;
Cubo.Add(a);
}
return Cubo;
}
public List<double> CalcularRaiz (List<double> numeros)
{
List<double> Raiz = new List<double>();
foreach (double item in numeros)
{
double a = Math.Sqrt(item);
Raiz.Add(a);
}
return Raiz;
}
public int MaiorNumero (List<int> numeros)
{
int Maior = 0;
foreach (int item in numeros)
{
if (item > Maior)
{
Maior = item;
}
}
return Maior;
}
public double Media (List<double> numeros)
{
double a = 0;
double b = 0;
double Media = 0;
foreach (double item in numeros)
{
b += item;
a = numeros.Count;
Media = b / a;
}
return Media;
}
public List<double> CalcularMedias (List<Notas> notas)
{
double x = 0;
List<double> NotasFinais = new List<double>();
foreach (Notas item in notas)
{
double a = item.Nota1 + item.Nota2 + item.Nota3;
x = a / 3;
NotasFinais.Add(x);
}
return NotasFinais;
}
private double AreaRetangulo (Retangulo retangulo)
{
double area = retangulo.Base * retangulo.Altura;
return area;
}
public List<double> AreaRetangulos (List<Retangulo> retangulos)
{
List<double> retangulo = new List<double>();
foreach (Retangulo item in retangulos)
{
double area = AreaRetangulo(item);
retangulo.Add(area);
}
return retangulo;
}
}
Notas nota1 = new Notas();
nota1.Nota1 = 10;
nota1.Nota2 = 8;
nota1.Nota3 = 6;
Notas nota2 = new Notas();
nota2.Nota1 = 8;
nota2.Nota2 = 6;
nota2.Nota3 = 4;
Notas nota3 = new Notas();
nota3.Nota1 = 8;
nota3.Nota2 = 10;
nota3.Nota3 = 6;
List<Notas> Notasss = new List<Notas>() { nota1, nota2, nota3 };
List<int> x = new List<int>() {2, 3, 4, 5};
List<int> x2 = new List<int>() {2, 3, 4};
List<double> x3 = new List<double>() {25, 16, 9};
List<double> x4 = new List<double>() {5, 5, 5};
List<Retangulo> retangulos = new List<Retangulo>()
{
new Retangulo() { Base = 5, Altura = 4 },
new Retangulo() { Base = 8, Altura = 4 },
new Retangulo() { Base = 10, Altura = 2 }
};
TreinoFocadoA a = new TreinoFocadoA();
int impares = a.SomaImpares(x);
List<int> cubos = a.CalcularCubo(x2);
List<double> Raiz = a.CalcularRaiz(x3);
int Maior = a.MaiorNumero(x2);
double Media = a.Media(x4);
List<double> Medias = a.CalcularMedias(Notasss);
List<double> Retangulos = a.AreaRetangulos(retangulos);
Console.WriteLine(impares);
Console.WriteLine(string.Join(", ", cubos));
Console.WriteLine(string.Join(", ", Raiz));
Console.WriteLine(Maior);
Console.WriteLine(Media);
Console.WriteLine(string.Join(", ", Medias));
Console.WriteLine(string.Join(", ", Retangulos));
// 8
// 8, 27, 64
// 5, 4, 3
// 4
// 5
// 8, 6, 8
// 20, 32, 20
```
## Nível 2
```csharp=
public class Pessoa
{
public string Nome;
public DateTime Nascimento;
public override string ToString()
{
return $" ({Nome} - {Nascimento.ToString("dd/MM/yyyy")})";
}
}
public class TreinoFocadoB
{
public List<DateTime> UltimoDia (List<DateTime> dias)
{
List<DateTime> DiaFinal = new List<DateTime>();
foreach (DateTime item in dias)
{
DateTime a = new DateTime (item.Year, item.Month, 1);
a = a.AddMonths(1);
a = a.AddDays(-1);
DiaFinal.Add(a);
}
return DiaFinal;
}
public DateTime MaiorData (List<DateTime> dias)
{
DateTime Data = new DateTime (0001,01,01);
foreach (DateTime item in dias)
{
if (item > Data)
{
Data = item;
}
}
return Data;
}
public List<Pessoa> FiltrarMaiores18(List<Pessoa> pessoas)
{
List<Pessoa> a = new List<Pessoa>();
foreach (Pessoa item in pessoas)
{
if (item.Nascimento.AddYears(18) <= DateTime.Now)
{
a.Add(item);
}
else
{
continue;
}
}
return a;
}
public bool TodosMaiores18 (List<Pessoa> pessoas)
{
bool a = true;
foreach (Pessoa item in pessoas)
{
if (item.Nascimento.AddYears(18) <= DateTime.Now)
{
a = true;
}
else
{
a = false;
break;
}
}
return a;
}
}
List<Pessoa> Maior = new List<Pessoa>()
{
new Pessoa() { Nome = "Matheus", Nascimento = new DateTime(2004 , 09, 10)},
new Pessoa() { Nome = "Vasco", Nascimento = new DateTime(1898 , 08, 21) },
new Pessoa() { Nome = "Ronaldinho", Nascimento = new DateTime(1980 , 03, 21) }
};
DateTime NascimentoMeu = new DateTime(2004 , 09, 10);
DateTime NascimentoMiguel = new DateTime(2019 , 10, 16);
DateTime DataNova1 = new DateTime(2020 , 10, 16);
List<DateTime> DataNova = new List<DateTime> () {NascimentoMeu,NascimentoMiguel,DataNova1};
TreinoFocadoB a = new TreinoFocadoB();
List<DateTime> UltimosDias = a.UltimoDia(DataNova);
DateTime MaiorData = a.MaiorData(DataNova);
List<Pessoa> FiltrarPessoa = a.FiltrarMaiores18(Maior);
bool TodosMaiores18 = a.TodosMaiores18(Maior);
Console.WriteLine(string.Join(", ",UltimosDias));
Console.WriteLine(MaiorData);
Console.WriteLine(string.Join(",",FiltrarPessoa));
Console.WriteLine(TodosMaiores18);
// 30/09/2004 00:00:00, 31/10/2019 00:00:00, 31/10/2020 00:00:00
// 16/10/2020 00:00:00
// (Vasco - 21/08/1898), (Ronaldinho - 21/03/1980)
// False
```
## Nível 3
```csharp=
public class Pessoa
{
public string Nome;
public DateTime Nascimento;
public string Cidade;
}
public class TreinoFocadoC
{
public string SepararLetras(string frase)
{
string FraseFinal = "";
foreach (char item in frase)
{
char letra = item;
FraseFinal += letra + "-";
}
return FraseFinal;
}
public List<int> CodigoASCII(string frase)
{
List<int> x = new List<int>();
foreach (char item in frase)
{
Convert.ToInt32(item);
x.Add(item);
}
return x;
}
public bool TodasVogais(string frase)
{
frase = frase.ToLower();
bool a = false;
foreach (char item in frase)
{
if (item == 'a' || item == 'e' || item == 'i' || item == 'o' || item == 'u')
{
a = true;
}
else
{
a = false;
}
}
return a;
}
public string Alternar(Pessoa pessoa)
{
string Alterar = "";
char anterior = ' ';
foreach (char item in pessoa.Nome)
{
anterior = item;
if (item == anterior)
{
Alterar += item.ToString().ToLower();
}
else
{
Alterar += item.ToString().ToLower();
}
}
return Alterar;
}
public string Inverter(string frase)
{
string inverter = "";
foreach (char item in frase)
{
inverter = item + inverter;
}
return inverter;
}
public bool Palindromo(string frase)
{
string inverter = "";
bool a = false;
foreach (char item in frase)
{
inverter = item + inverter.ToLower();
frase = frase.ToLower();
if (inverter == frase)
{
a = true;
}
else
{
a = false;
}
}
return a;
}
}
DateTime DataNiver = new DateTime(1980 , 03, 21);
Pessoa x = new Pessoa();
x.Nome = "Matheus Rafael Morato Rocha";
x.Nascimento = DataNiver;
x.Cidade = "Cidade";
TreinoFocadoC a = new TreinoFocadoC();
string SepararLetras = a.SepararLetras("Vasco Minha Vida Minha Historia");
List<int> CodigoASCII = a.CodigoASCII("Vasco Minha Vida Minha Historia");
bool TodasVogais = a.TodasVogais("JBL");
string Alterar = a.Alternar(x);
string Inverter = a.Inverter("Vasco Minha Vida Minha Historia");
bool Palindromo = a.Palindromo("SubiNoOnibus");
Console.WriteLine(SepararLetras);
Console.WriteLine(string.Join(", ",CodigoASCII));
Console.WriteLine(TodasVogais);
Console.WriteLine(Alterar);
Console.WriteLine(Inverter);
Console.WriteLine(Palindromo);
// V-a-s-c-o- -M-i-n-h-a- -V-i-d-a- -M-i-n-h-a- -H-i-s-t-o-r-i-a-
// 86, 97, 115, 99, 111, 32, 77, 105, 110, 104, 97, // 32, 86, 105, 100, 97, 32, 77, 105, 110, 104, 97, // 32, 72, 105, 115, 116, 111, 114, 105, 97
// False
// matheus rafael morato rocha
// airotsiH ahniM adiV ahniM ocsaV
// True
```