# Session Seven: Repetição FOREACH | Fundamentos da Lógica **Autor**: Vitório Trindade Santana **Turma**: Informática C **Número**: 50 [toc] ## Exercício 1 ```csharp= using System; using System.Collections.Generic; class MainClass { public static void Main (string[] args) { List<int> q1 = new List<int>(); q1.Add(8); q1.Add(9); q1.Add(11); q1.Add(5); List<int> q2 = new List<int>(); q2.Add(4); q2.Add(3); q2.Add(2); List<double> q3 = new List<double>(); q3.Add(4); q3.Add(16); q3.Add(121); List<double> q5 = new List<double>(); q5.Add(7); q5.Add(5); q5.Add(9); Notas aln1 = new Notas(); aln1.Nota1 = 5; aln1.Nota2 = 5; aln1.Nota3 = 5; Notas aln2 = new Notas(); aln2.Nota1 = 5; aln2.Nota2 = 7; aln2.Nota3 = 6; Notas aln3 = new Notas(); aln3.Nota1 = 8; aln3.Nota2 = 7; aln3.Nota3 = 6; List<Notas> q6 = new List<Notas>(); q6.Add(aln1); q6.Add(aln2); q6.Add(aln3); Retangulo ret1 = new Retangulo(); ret1.Altura = 2; ret1.Base = 5; Retangulo ret2 = new Retangulo(); ret2.Altura = 5; ret2.Base = 3; Retangulo ret3 = new Retangulo(); ret3.Altura = 8; ret3.Base = 6; List<Retangulo> q7 = new List<Retangulo>(); q7.Add(ret1); q7.Add(ret2); q7.Add(ret3); TreinoFocadoA tfa = new TreinoFocadoA(); int x1 = tfa.SomaImpares(q1); List<int> x2 = tfa.CalcularCubo(q2); List<double> x3 = tfa.CalcularRaiz(q3); int x4 = tfa.MaiorNumero(q1); double x5 = tfa.Media(q5); List<double> x6 = tfa.CalcularMedias(q6); List<double> x8 = tfa.AreaRetangulos(q7); Console.WriteLine(x1); Console.WriteLine(string.Join("-", x2)); Console.WriteLine(string.Join("-", x3)); Console.WriteLine(x4); Console.WriteLine(x5); Console.WriteLine(string.Join("-", x6)); Console.WriteLine(string.Join("-", x8)); // Resultados: //25 //64-27-8 //2-4-11 //11 //7 //5-6-7 //10-15-48 } } public class Notas { public double Nota1 {get;set;} public double Nota2 {get;set;} public double Nota3 {get;set;} } public class Retangulo { public double Base {get;set;} public double Altura {get;set;} } public class TreinoFocadoA { public int SomaImpares (List<int> numeros) { int soma = 0; foreach (int item in numeros) { if (item % 2 != 0) soma = soma + item; } return soma; } public List<int> CalcularCubo (List<int> numeros) { List<int> q2 = new List<int>(); int calc = 0; foreach (int nmr in numeros) { double pot = Math.Pow(nmr, 3); calc = Convert.ToInt32(pot); q2.Add(calc); } return q2; } public List<double> CalcularRaiz (List<double> numeros) { List<double> q3 = new List<double>(); double calc = 0; foreach (int nmr in numeros) { calc = Math.Sqrt(nmr); q3.Add(calc); } return q3; } public int MaiorNumero (List<int> numeros) { int maiorNmr = 0; foreach (int nmr in numeros) { if (nmr > maiorNmr) maiorNmr = nmr; } return maiorNmr; } public double Media (List<double> notas) { double soma = 0; foreach (int nta in notas) { soma += nta; } double calcMedia = soma / notas.Count; return calcMedia; } public List<double> CalcularMedias (List<Notas> notas) { List<double> q6 = new List<double>(); foreach (Notas nta in notas) { double media = (nta.Nota1 + nta.Nota2 + nta.Nota3) / notas.Count; q6.Add(media); } return q6; } private double AreaRetangulo (Retangulo retangulo) { double area = retangulo.Base * retangulo.Altura; return area; } public List<double> AreaRetangulos (List<Retangulo> retangulos) { List<double> rets = new List<double>(); foreach(Retangulo ret in retangulos) { double calretan = AreaRetangulo(ret); rets.Add(calretan); } return rets; } } ``` ## Exercício 2 ```csharp= using System; using System.Collections.Generic; class MainClass { public static void Main (string[] args) { DateTime dta1 = new DateTime(2003, 12, 01); DateTime dta2 = new DateTime(2006, 6, 12); DateTime dta3 = new DateTime(2000, 05, 8); List<DateTime> lista1 = new List<DateTime>(); lista1.Add(dta1); lista1.Add(dta2); lista1.Add(dta3); Pessoa pss1 = new Pessoa(); pss1.Nascimento = new DateTime(2004, 12, 26); pss1.Nome = "Antônio Pedro"; Pessoa pss2 = new Pessoa(); pss2.Nascimento = new DateTime(2000, 5, 8); pss2.Nome = "Joao Carlos"; Pessoa pss3 = new Pessoa(); pss3.Nascimento = new DateTime(1998, 8, 26); pss3.Nome = "Kaique Ferreira"; List<Pessoa> lista2 = new List<Pessoa>(); lista2.Add(pss1); lista2.Add(pss2); lista2.Add(pss3); TreinoFocadoB tfb = new TreinoFocadoB(); List<DateTime> x1 = tfb.UltimoDia(lista1); DateTime x2 = tfb.MaiorData(lista1); List<Pessoa> x3 = tfb.FiltrarMaiores18(lista2); bool x4 = tfb.TodosMaiores18(lista2); Console.WriteLine(string.Join(" - ", x1)); Console.WriteLine(x2.ToString("dd/MM/yyyy")); Console.WriteLine(string.Join(" - ", x3)); Console.WriteLine(x4); // Resultados: //12/31/2003 12:00:00 AM - 6/30/2006 12:00:00 AM - 5/31/2000 12:00:00 AM //12/06/2006 //Joao Carlos(08/05/2000) - Kaique Ferreira(26/08/1998) //False } } public class Pessoa { public string Nome {get;set;} public DateTime Nascimento {get;set;} public override string ToString () { return Nome + "(" + Nascimento.ToString("dd/MM/yyyy") + ")"; } } public class TreinoFocadoB { public List<DateTime> UltimoDia (List<DateTime> dias) { List<DateTime> q1 = new List<DateTime>(); foreach (DateTime dta in dias) { DateTime data = new DateTime(dta.Year, dta.Month, 1); DateTime calc = data.AddMonths(1).AddDays(-1); q1.Add(calc); } return q1; } public DateTime MaiorData (List<DateTime> dias) { DateTime maiorData = DateTime.MinValue; foreach (DateTime dta in dias) { if (dta > maiorData) maiorData = dta; } return maiorData; } public List<Pessoa> FiltrarMaiores18 (List<Pessoa> pessoas) { List<Pessoa> q3 = new List<Pessoa>(); DateTime conf = DateTime.Now.AddYears(-18); foreach (Pessoa data in pessoas) { if (data.Nascimento <= conf) q3.Add(data); } return q3; } public bool TodosMaiores18 (List<Pessoa> pessoas) { bool cnf = true; DateTime conf = DateTime.Now.AddYears(-18); foreach (Pessoa pss in pessoas) { if (pss.Nascimento > conf) { cnf = false; } } return cnf; } } ``` ## Exercício 3 ```csharp= using System; using System.Collections.Generic; class MainClass { public static void Main (string[] args) { TreinoFocadoC tfc = new TreinoFocadoC(); string x1 = tfc.SepararLetras("O corre corre da cidade grande, tanta gente passa estou só, o vento sopra pelo campo e traz uma lembraça sua estou só"); bool x3 = tfc.TodasVogais("ok i pull up"); string x5 = tfc.Inverter("sla mano to sem ideia"); bool x6 = tfc.Palindromo("Natan"); Console.WriteLine(x1); Console.WriteLine(x3); Console.WriteLine(x5); Console.WriteLine(x6); // Resultados: //O- -c-o-r-r-e- -c-o-r-r-e- -d-a- -c-i-d-a-d-e- -g-r-a-n-d-e-,- -t-a-n-t-a- -g-e-n-t-e- -p-a-s-s-a- -e-s-t-o-u- -s-ó-,- -o- -v-e-n-t-o- -s-o-p-r-a- -p-e-l-o- -c-a-m-p-o- -e- -t-r-a-z- -u-m-a- -l-e-m-b-r-a-ç-a- -s-u-a- -e-s-t-o-u- -s-ó- //False //aiedi mes ot onam als //True } } public class Pessoa { public string Nome {get;set;} public DateTime Nascimento {get;set;} public string Cidade {get;set;} public override string ToString () { return Nome + "(" + Nascimento.ToString("dd/MM/yyyy") + ")"; } } public class TreinoFocadoC { public string SepararLetras (string frase) { string ltrSep = ""; foreach (Char letra in frase) { ltrSep += letra + "-"; } return ltrSep; } public bool TodasVogais (string frase) { bool cnst = true; foreach (Char yo in frase) { if ((yo.Equals('b') || yo.Equals('c') || yo.Equals('d') || yo.Equals('f') || yo.Equals('g') || yo.Equals('h') || yo.Equals('j') || yo.Equals('k') || yo.Equals('l') || yo.Equals('m') || yo.Equals('n') || yo.Equals('p') || yo.Equals('q') || yo.Equals('r') || yo.Equals('s') || yo.Equals('t') || yo.Equals('v') || yo.Equals('w') || yo.Equals('x') || yo.Equals('y') || yo.Equals('z')) == true) { cnst = false; } } return cnst; } public string Inverter (string frase) { string invert = ""; foreach (char letra in frase) { invert = letra + invert; } return invert; } public bool Palindromo (string frase) { bool conf = false; string invert = ""; frase = frase.ToLower(); foreach (char letra in frase) { invert = letra + invert; if (invert.Equals(frase)) conf = true; } return conf; } } ``` ## Feito após a data de entrega ```csharp= using System; using System.Collections.Generic; class MainClass { public static void Main (string[] args) { TreinoFocadoC tfc = new TreinoFocadoC(); string x1 = tfc.SepararLetras("O corre corre da cidade grande, tanta gente passa estou só, o vento sopra pelo campo e traz uma lembraça sua estou só"); List<int> x2 = tfc.CodigoASCII("Vitório"); bool x3 = tfc.TodasVogais("ok i pull up"); string x5 = tfc.Inverter("sla mano to sem ideia"); bool x6 = tfc.Palindromo("Natan"); Console.WriteLine(x1); Console.WriteLine(string.Join(",", x2)); Console.WriteLine(x3); Console.WriteLine(x5); Console.WriteLine(x6); // Resultados: //O- -c-o-r-r-e- -c-o-r-r-e- -d-a- -c-i-d-a-d-e- -g-r-a-n-d-e-,- -t-a-n-t-a- -g-e-n-t-e- -p-a-s-s-a- -e-s-t-o-u- -s-ó-,- -o- -v-e-n-t-o- -s-o-p-r-a- -p-e-l-o- -c-a-m-p-o- -e- -t-r-a-z- -u-m-a- -l-e-m-b-r-a-ç-a- -s-u-a- -e-s-t-o-u- -s-ó- //86,105,116,243,114,105,111 //False //aiedi mes ot onam als //True } } public class Pessoa { public string Nome {get;set;} public DateTime Nascimento {get;set;} public string Cidade {get;set;} public override string ToString () { return Nome + "(" + Nascimento.ToString("dd/MM/yyyy") + ")"; } } public class TreinoFocadoC { public string SepararLetras (string frase) { string ltrSep = ""; foreach (Char letra in frase) { ltrSep += letra + "-"; } return ltrSep; } public List<int> CodigoASCII (string frase) { List<int> q2 = new List<int>(); int ascii = 0; foreach (Char letra in frase) { ascii = Convert.ToInt32(letra); q2.Add(ascii); } return q2; } public bool TodasVogais (string frase) { bool cnst = true; foreach (Char yo in frase) { if ((yo.Equals('b') || yo.Equals('c') || yo.Equals('d') || yo.Equals('f') || yo.Equals('g') || yo.Equals('h') || yo.Equals('j') || yo.Equals('k') || yo.Equals('l') || yo.Equals('m') || yo.Equals('n') || yo.Equals('p') || yo.Equals('q') || yo.Equals('r') || yo.Equals('s') || yo.Equals('t') || yo.Equals('v') || yo.Equals('w') || yo.Equals('x') || yo.Equals('y') || yo.Equals('z')) == true) { cnst = false; } } return cnst; } public string Inverter (string frase) { string invert = ""; foreach (char letra in frase) { invert = letra + invert; } return invert; } public bool Palindromo (string frase) { bool conf = false; string invert = ""; frase = frase.ToLower(); foreach (char letra in frase) { invert = letra + invert; if (invert.Equals(frase)) conf = true; } return conf; } } ```
{"metaMigratedAt":"2023-06-16T03:47:03.946Z","metaMigratedFrom":"Content","title":"Session Seven: Repetição FOREACH | Fundamentos da Lógica","breaks":true,"contributors":"[{\"id\":\"052c8e10-b233-429a-a1e0-0d8df7864830\",\"add\":11825,\"del\":27}]"}
Expand menu