# Session Six: Repetição FOR Cauê Freitas Barreto INFOB 05 [toc] ## Treino Focado A ````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 List<int> GerarSquencia (int fim) { List<int> seq = new List<int> (); for (int i = 0; i <= fim; i++) { seq.Add(i); } return seq; } public List<int> GerarSquencia (int inicio, int fim) { List<int> seq = new List<int> (); for (int i = inicio; i <= fim; i++) { seq.Add(i); } return seq; } public List<int> GerarSquenciaPares (int inicio, int fim) { List<int> seq = new List<int> (); for (int i = inicio; i <= fim; i++) { if (i % 2 == 0) seq.Add(i); } return seq; } public int SomarAte (int fim) { int soma = 0; for (int i = 0; i <= fim; i++) { soma += i; } return soma; } public double Media (List<double> notas) { double soma = 0; for (int i = 0; i < notas.Count; i++) { double n = notas[i]; soma = soma + n; } double media = soma / notas.Count; return media; } public List<double> CalcularMedias (List<Notas> notas) { List<double> mediasAlunos = new List<double>(); { for (int i = 0; i< notas.Count; i++) { Notas notasAluno = notas[i]; double media = (notasAluno.Nota1 + notasAluno.Nota2 + notasAluno.Nota3) / 3; mediasAlunos.Add(media); } } return mediasAlunos; } public List<double> CalcularQuadrados (List<double> numeros) { List<double> quad = new List<double> (); for (int i = 0; i < numeros.Count; i++) { double n = numeros[i]; double quadrado = Math.Pow(n, 2); quad.Add(quadrado); } return quad; } private double AreaRetangulo (Retangulo retangulo) { return retangulo.Base * retangulo.Altura; } public List<double> AreaRetangulos (List<Retangulo> retangulos) { List<double> areas = new List<double>(); for (int i = 0; i < retangulos.Count; i++) { Retangulo a = retangulos[i]; double area = AreaRetangulo(a); areas.Add(area); } return areas; } } TreinoFocadoA treinoA = new TreinoFocadoA(); List<int> a = treinoA.GerarSquencia(5); Console.WriteLine(string.Join("-", a)); List<int> b = treinoA.GerarSquencia(2, 4); Console.WriteLine(string.Join("-", b)); List<int> c = treinoA.GerarSquenciaPares(2, 7); Console.WriteLine(string.Join("-", c)); int d = treinoA.SomarAte(5); Console.WriteLine(d); List<double> notas = new List<double> () { 5, 5, 5 }; double e = treinoA.Media(notas); Console.WriteLine(e); Notas n1 = new Notas(); n1.Nota1 = 5; n1.Nota2 = 5; n1.Nota3 = 5; Notas n2 = new Notas(); n2.Nota1 = 10; n2.Nota2 = 8; n2.Nota3 = 6; Notas n3 = new Notas(); n3.Nota1 = 4; n3.Nota2 = 2; n3.Nota3 = 3; List<Notas> notasAlunos = new List<Notas>(); notasAlunos.Add(n1); notasAlunos.Add(n2); notasAlunos.Add(n3); List<double> f = treinoA.CalcularMedias(notasAlunos); Console.WriteLine(string.Join("-", f)); List<double> num = new List<double> () { 2, 4, 10 }; List<double> g = treinoA.CalcularQuadrados(num); Console.WriteLine(string.Join("-", g)); List<Retangulo> retangulos = new List<Retangulo> { new Retangulo() {Base = 10, Altura = 5}, new Retangulo() {Base = 5, Altura = 4}, new Retangulo() {Base = 8, Altura = 3} }; List<double> h = treinoA.AreaRetangulos(retangulos); Console.WriteLine(string.Join("-", h)); // 0-1-2-3-4-5 // 2-3-4 // 2-4-6 // 15 // 5 // 5-8-3 // 4-16-100 // 50-20-24 ```` ## Treino Focado B ```csharp= using System; using System.Collections.Generic; public class Pessoa { public string Nome { get; set; } public DateTime Nascimento { get; set; } public override string ToString() { return "(" + Nome + ", " + Nascimento.ToShortDateString() + ")"; } } public class TreinoFocadoB { public List<DateTime> GerarSquencia (int qtd) { List<DateTime> seq = new List<DateTime>(); for (int i = 0; i < qtd; i++) { DateTime d = DateTime.Now; DateTime date = d.AddDays(i); seq.Add(date); } return seq; } public List<DateTime> GerarSquencia (int qtd, DateTime data) { List<DateTime> seq = new List<DateTime>(); for (int i = 0; i < qtd; i++) { DateTime date = data.AddDays(i); seq.Add(date); } return seq; } public List<DateTime> GerarSquenciaPares (int qtd) { List<DateTime> seq = new List<DateTime>(); for (int i = 0; i < qtd; i++) { if (i % 2 == 0) { DateTime d = DateTime.Now; DateTime date = d.AddDays(i); seq.Add(date); } } return seq; } private string Signo (DateTime nascimento) { string x = string.Empty; if (nascimento.Day >= 21 && nascimento.Month == 3 || nascimento.Day <= 20 && nascimento.Month == 4) x = "Áries"; else if (nascimento.Day >= 21 && nascimento.Month == 4 || nascimento.Day <= 20 && nascimento.Month == 5) x = "Touro"; else if (nascimento.Day >= 21 && nascimento.Month == 5 || nascimento.Day <= 20 && nascimento.Month == 6) x = "Gêmeos"; else if (nascimento.Day >= 21 && nascimento.Month == 6 || nascimento.Day <= 21 && nascimento.Month == 7) x = "Câncer"; else if (nascimento.Day >= 22 && nascimento.Month == 7 || nascimento.Day <= 22 && nascimento.Month == 8) x = "Leão"; else if (nascimento.Day >= 23 && nascimento.Month == 8 || nascimento.Day <= 22 && nascimento.Month == 9) x = "Virgem"; else if (nascimento.Day >= 23 && nascimento.Month == 9 || nascimento.Day <= 22 && nascimento.Month == 10) x = "Libra"; else if (nascimento.Day >= 23 && nascimento.Month == 10 || nascimento.Day <= 21 && nascimento.Month == 11) x = "Escorpião"; else if (nascimento.Day >= 22 && nascimento.Month == 11 || nascimento.Day <= 21 && nascimento.Month == 12) x = "Sargitário"; else if (nascimento.Day >= 22 && nascimento.Month == 12 || nascimento.Day <= 20 && nascimento.Month == 01) x = "Capricórnio"; else if (nascimento.Day >= 21 && nascimento.Month == 01 || nascimento.Day <= 19 && nascimento.Month == 02) x = "Áquario"; else x = "Peixes"; return x; } public List<string> signos (List<DateTime> nascimentos) { List<string> signos = new List<string>(); for (int i = 0; i < nascimentos.Count; i++) { DateTime nasc = nascimentos[i]; string signo = Signo(nasc); signos.Add(signo); } return signos; } public List<Pessoa> Maiores18 (List<Pessoa> pessoas) { List<Pessoa> nivers = new List<Pessoa>(); for(int i = 0; i < pessoas.Count; i++) { Pessoa niver = pessoas[i]; if(niver.Nascimento <= DateTime.Now.AddYears(-18)) nivers.Add(niver); } return nivers; } public bool TodosMaiores18 (List<Pessoa> pessoas) { List<Pessoa> nivers = new List<Pessoa>(); bool todosMaiores = true; for(int i = 0; i < pessoas.Count; i++) { Pessoa niver = pessoas[i]; if(niver.Nascimento > DateTime.Now.AddYears(-18)) todosMaiores = false; } return todosMaiores; } } TreinoFocadoB treinoB = new TreinoFocadoB (); List<DateTime> a = treinoB.GerarSquencia(5); Console.WriteLine(string.Join(" - ", a)); DateTime data = new DateTime (2021, 6, 25); List<DateTime> b = treinoB.GerarSquencia(2, data); Console.WriteLine(string.Join(" - ", b)); List<DateTime> c = treinoB.GerarSquenciaPares(5); Console.WriteLine(string.Join(" - ", c)); List<DateTime> nascimentos = new List<DateTime> { new DateTime(2004, 1, 5), new DateTime(2003, 11, 6), new DateTime(2001, 7, 15) }; List<string> d = treinoB.signos(nascimentos); Console.WriteLine(string.Join(" - ", d)); List<Pessoa> pessoas = new List<Pessoa> { new Pessoa () { Nome = "Letícia Queiroz", Nascimento = new DateTime(2004, 1, 5)}, new Pessoa () { Nome = "Kaio Eduardo", Nascimento = new DateTime(2003, 11, 6)}, new Pessoa () { Nome = "Lívia Queiroz", Nascimento = new DateTime(2001, 7, 15)}, new Pessoa () { Nome = "Maria Queiroz", Nascimento = new DateTime(1983, 3, 09)}, }; List<Pessoa> e = treinoB.Maiores18(pessoas); Console.WriteLine(string.Join(" - ", e)); List<Pessoa> pessoas2 = new List<Pessoa> { new Pessoa () { Nome = "", Nascimento = new DateTime(2000, 1, 5)}, new Pessoa () { Nome = "", Nascimento = new DateTime(2002, 11, 6)}, new Pessoa () { Nome = "", Nascimento = new DateTime(2001, 7, 15)}, new Pessoa () { Nome = "", Nascimento = new DateTime(1983, 3, 09)}, }; bool f = treinoB.TodosMaiores18(pessoas2); Console.WriteLine(string.Join(" - ", f)); ```` ## Treino Focado C ```csharp= using System; using System.Collections.Generic; public class Pessoa { public string Nome { get; set; } public DateTime Nascimento { get; set; } public string Cidade { get; set; } } public class TreinoFocadoC { public string SepararLetras (string frase) { string separado = string.Empty; for(int i = 0; i < frase.Length; i++) { char letra = frase[i]; separado += letra + "-"; } return separado; } public string Inverter (string frase) { string invertido = string.Empty; for(int i = 0; i < frase.Length; i++) { char letra = frase[i]; invertido = letra + invertido; } return invertido; } public bool TodasVogais(string frase) { bool todasVogais = true; for(int i = 0; i < frase.Length; i++) { char letra = frase[i]; if (letra != 'a' && letra != 'e' && letra != 'i' && letra != 'o' && letra != 'u' && letra != 'A' && letra != 'E' && letra != 'I' && letra != 'O' && letra != 'U') todasVogais = false; } return todasVogais; } private string UltimoNome(string nome1) { string extrair = nome1.Substring(nome1.LastIndexOf (" ")); return extrair; } public List<string> Ultimosnomes (List<string> nomes) { List<string> ultimosnomes= new List<string>(); for(int i = 0; i < nomes.Count; i++) { string nome = nomes[i]; string ultimonome = UltimoNome(nome); ultimosnomes.Add(ultimonome); } return ultimosnomes; } public bool ApenasCoresPrimarias (List<string> cores) { bool todasprimarias = true; for (int i = 0; i < cores.Count; i++) { string cor = cores[i]; if (cor != "Azul" && cor != "Vermelho" && cor != "Amarelo") { todasprimarias = false; break; } } return todasprimarias; } } TreinoFocadoC treinoC = new TreinoFocadoC(); string a = treinoC.SepararLetras("Olá, tudo bem?"); Console.WriteLine(a); string b = treinoC.Inverter("Olá, tudo bem?"); Console.WriteLine(b); bool c = treinoC.TodasVogais("Oie"); Console.WriteLine(c); List<string> nomes = new List<string> () { "Letícia Queiroz Moreira", "Bruno Oliveira", "Maria Clara Santos" }; List<string> d = treinoC.Ultimosnomes(nomes); Console.WriteLine(string.Join("-", d)); List<string> cores = new List<string> () { "Amarelo", "Vermelho" }; bool e = treinoC.ApenasCoresPrimarias(cores); Console.WriteLine(e); ````
{"metaMigratedAt":"2023-06-16T03:47:17.038Z","metaMigratedFrom":"Content","title":"Session Six: Repetição FOR","breaks":true,"contributors":"[{\"id\":\"4197b53a-4db5-46d1-a826-1e82b9d77550\",\"add\":10881,\"del\":0}]"}
Expand menu