# Session six | Repetição FOR autora: Isabela Silva Sousa [toc] ## Exercício 1 ```csharp= using System; using System.Collections.Generic; 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 List<int> GerarSequencia(int fim) { List<int> l = new List<int>(); for (int i = 0; i <= fim; i++) { l.Add(i); } return l; } public List<int> GerarSequencia(int inicio, int fim) { List<int> l = new List<int>(); for (int i = inicio; i <= fim; i++) { l.Add(i); } return l; } public List<int> GerarSequenciaPares(int inicio, int fim) { List<int> l = new List<int>(); for (int i = inicio; i <= fim; i++) { if (i % 2 == 0) l.Add(i); } return l; } public int SomarAte(int fim) { int soma = 0; for (int i = 1; i <= fim; i++) { soma += i; } return soma; } public double CalcularMedias(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> Quadrados (List<double> numeros) { List<double> quads = new List<double>(); for (int i = 0; i < numeros.Count; i++) { double n = numeros[i]; double q = Math.Pow(n, 2); quads.Add(q); } return quads; } private double AreaRetangulo(Retangulo r) { return r.Base * r.Altura; } public List<double> AreaRetangulos(List<Retangulo> retangulos) { List<double> areas = new List<double>(); for (int i = 0; i < retangulos.Count; i++) { Retangulo r = retangulos[i]; double area = AreaRetangulo(r); areas.Add(area); } return areas; } } TreinoFocadoA treinoA = new TreinoFocadoA(); List<int> x1 = treinoA.GerarSequencia(0); Console.WriteLine(string.Join("-", x1)); List<int> x2 = treinoA.GerarSequencia(2, 4); Console.WriteLine(string.Join("-", x2)); List<int> x3 = treinoA.GerarSequenciaPares(2, 4); Console.WriteLine(string.Join("-", x3)); int x4 = treinoA.SomarAte(3); Console.WriteLine(x4); List<double> lx5 = new List<double>() {4, 6, 8, 10}; double x5 = treinoA.CalcularMedias(lx5); Console.WriteLine(x5); Notas n1 = new Notas(); n1.Nota1 = 8; n1.Nota2 = 9; n1.Nota3 = 7; Notas n2 = new Notas(); n2.Nota1 = 6; n2.Nota2 = 10; n2.Nota3 = 8; Notas n3 = new Notas(); n3.Nota1 = 10; n3.Nota2 = 10; n3.Nota3 = 7; List<Notas> notas = new List<Notas>() {n1, n2, n3}; List<double> x6 = treinoA.CalcularMedias(notas); Console.WriteLine(string.Join("-", x6)); List<double> n7 = new List<double>() {2,3,4,5}; List<double> x7 = treinoA.Quadrados(n7); Console.WriteLine(string.Join("-", x7)); List<Retangulo> retangs = new List<Retangulo>() { new Retangulo() {Base = 10, Altura = 2}, new Retangulo() {Base = 5, Altura = 5}, new Retangulo() {Base = 2, Altura = 5} }; List<double> areas = treinoA.AreaRetangulos(retangs); Console.WriteLine(string.Join("-", areas)); // 0 // 2-3-4 // 2-4 // 6 // 7 // 8-8-9 // 4-9-16-25 // 20-25-10 ``` ## Exercício 2 ```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.ToString("dd/MM/yyyy"); } }; public class TreinoFocadoB { public List<DateTime> GerarSequencia(int fim) { List<DateTime> listaa = new List<DateTime>(); DateTime inicio = DateTime.Now.Date; for (int i = 0; i <= fim; i++) { inicio = inicio.AddDays(1); listaa.Add(inicio); } return listaa; } public List<DateTime> GerarSequencia(int fim, DateTime data) { List<DateTime> listaa = new List<DateTime>(); DateTime inicio = data; for (int i = 0; i <= fim; i++) { inicio = inicio.AddDays(1); listaa.Add(inicio); } return listaa; } public List<DateTime> GerarSequenciaPares(int qtd, DateTime data) { List<DateTime> listaa = new List <DateTime>(); if (data.Day % 2 == 0) { for (int i= 0; i <= qtd; i++) { if (i % 2 == 0 ) { DateTime inicio = data.AddDays(i); listaa.Add(inicio); } } } else { for (int i= 0; i <= qtd; i++) { if (i % 2 != 0) { DateTime inicio = data.AddDays(i); listaa.Add(inicio); } } } return listaa; } public string Signo(DateTime nasc) { string signoo = string.Empty; if ((nasc.Day > 20 && nasc.Month == 3) || (nasc.Day < 21 && nasc.Month == 4)) { signoo = "Áries"; } else if ((nasc.Day > 20 && nasc.Month == 4) || (nasc.Day < 21 && nasc.Month == 5)) { signoo = "Touro"; } else if ((nasc.Day > 20 && nasc.Month == 5) || (nasc.Day < 21 && nasc.Month == 6)) { signoo = "Gêmeos"; } else if ((nasc.Day > 20 && nasc.Month == 6) || (nasc.Day < 23 && nasc.Month == 7)) { signoo = "Câncer"; } else if ((nasc.Day > 22 && nasc.Month == 7) || (nasc.Day < 23 && nasc.Month == 8)) { signoo = "Leão"; } else if ((nasc.Day > 22 && nasc.Month == 8) || (nasc.Day < 23 && nasc.Month == 9)) { signoo = "Virgem"; } else if ((nasc.Day > 22 && nasc.Month == 9) || (nasc.Day < 23 && nasc.Month == 10)) { signoo = "Libra"; } else if ((nasc.Day > 22 && nasc.Month == 10) || (nasc.Day < 22 && nasc.Month == 11)) { signoo = "Escorpião"; } else if ((nasc.Day > 21 && nasc.Month == 11) || (nasc.Day < 22 && nasc.Month == 12)) { signoo = "Sargitário"; } else if ((nasc.Day > 21 && nasc.Month == 12) || (nasc.Day < 21 && nasc.Month == 1)) { signoo = "Capricórnio"; } else if ((nasc.Day > 20 && nasc.Month == 1) || (nasc.Day < 19 && nasc.Month == 2)) { signoo = "Aquário"; } else if ((nasc.Day > 18 && nasc.Month == 2) || (nasc.Day < 21 && nasc.Month == 3)) { signoo = "Peixes"; } return signoo; } public List<string> Signos(List<DateTime> nasc) { List<string> signos = new List<string>(); for (int i = 0; i < nasc.Count; i++) { DateTime d = nasc [i]; string s = Signo(d); signos.Add(s); } return signos; } public List<Pessoa> FiltrarMaiores18(List<Pessoa> pessoas) { List<Pessoa> filtrados = new List<Pessoa>(); for (int i = 0; i < pessoas.Count; i++) { Pessoa p = pessoas[i]; if (p.Nascimento <= DateTime.Now.AddYears(-18)) { filtrados.Add(p); } } return filtrados; } public bool TodosMaiores18(List<Pessoa> pessoas) { bool TodosMaiores = true; for (int a = 0; a < pessoas.Count; a++) { Pessoa p = pessoas[1]; if (p.Nascimento <= DateTime.Now.AddYears(-18)) { TodosMaiores = false; } } return TodosMaiores; } } TreinoFocadoB treinoB = new TreinoFocadoB(); List<DateTime> x1 = treinoB.GerarSequencia(2); Console.WriteLine(string.Join("-", x1)); List<DateTime> x2 = treinoB.GerarSequencia(2, new DateTime(2021, 6, 10)); Console.WriteLine(string.Join("-", x2)); List<DateTime> x3 = treinoB.GerarSequenciaPares(2, new DateTime(2021, 6, 10)); Console.WriteLine(string.Join("-", x3)); DateTime data1 = new DateTime(2004, 3, 17); string x4 = treinoB.Signo(data1); Console.WriteLine(string.Join(" - ", x4)); List<DateTime> data = new List<DateTime>(); data.Add(new DateTime(2004, 10, 30)); data.Add(new DateTime(2002, 02, 19)); data.Add(new DateTime(2002, 08, 28)); data.Add(new DateTime(2002, 05, 1)); List<string> x5 = treinoB.Signos(data); Console.WriteLine(string.Join(" - ", x5)); List<Pessoa> pessoas = new List<Pessoa>(); { new Pessoa() { Nome = "Barbara", Nascimento = new DateTime(1995, 3, 17) }; new Pessoa() { Nome = "Yasmin", Nascimento = new DateTime(1999, 10, 27) }; new Pessoa() { Nome = "Larissa", Nascimento = new DateTime(2005, 4, 13) }; }; List<Pessoa> x6 = treinoB.FiltrarMaiores18(pessoas); Console.WriteLine(string.Join(" - ", x6)); bool x7 = treinoB.TodosMaiores18(pessoas); Console.WriteLine(string.Join("-", x7)); // 6/27/2021 12:00:00 AM-6/28/2021 12:00:00 AM-6/29/2021 12:00:00 AM // 6/11/2021 12:00:00 AM-6/12/2021 12:00:00 AM-6/13/2021 12:00:00 AM // 6/10/2021 12:00:00 AM-6/12/2021 12:00:00 AM // Peixes // Escorpião - Peixes - Virgem - Touro // // True ``` ## Exercício 3 ```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 nova = string.Empty; for (int i = 0; i < frase.Length; i++) { char letra = frase[i]; nova = nova + letra + "-"; } return nova; } public string Inverter(string frase) { string nova = string.Empty; for (int i = 0; i < frase.Length; i++) { char letra = frase[i]; nova = letra + nova; } return nova; } public bool TodasVogais (string frase) { bool resposta = true; for (int i = 0; i < frase.Length; i ++) { char letra = frase [i]; if (letra != 'a' && letra != 'e' && letra != 'i' && letra != 'o' && letra != 'u') { resposta = false; } } return resposta; } public string UltimoNome(string nome) { int posicaoUltEspaco = nome.LastIndexOf(" "); string sobrenome = nome.Substring(posicaoUltEspaco + 1); return sobrenome; } public List<string> UltimosNomes (List<string> nome) { List<string> ultimoNome = new List<string> (); for (int i = 0; i < nome.Count; i++) { string nomes = nome[i]; string ultNome = UltimoNome(nomes); ultimoNome.Add(ultNome); } return ultimoNome; } public bool ApenasCoresPrimarias(List<string> cores) { bool todasPrimarias = true; for (int i = 0; i < cores.Count; i++) { string cor = cores[i]; if (cor != "vermelho" && cor != "amarelo" && cor != "azul") { todasPrimarias = false; } } return todasPrimarias; } public bool SenhaForte(string senha) { bool senhaforte = false; for(int i = 0; i < senha.Length; i++) { bool caracter = senha.Contains("!") || senha.Contains("@") || senha.Contains("#") || senha.Contains("$") || senha.Contains("%") || senha.Contains("&") || senha.Contains("+") || senha.Contains("-") || senha.Contains("*"); bool digitos = senha.Length >= 8; bool senhaf = caracter == digitos; if(senhaf) { senhaforte = true; } } return senhaforte; } } TreinoFocadoC treinoC = new TreinoFocadoC(); string x1 = treinoC.SepararLetras("Barbara"); Console.WriteLine(x1); string x2 = treinoC.Inverter("Barbara"); Console.WriteLine(x2); bool frase = treinoC.TodasVogais("eaee"); Console.WriteLine(frase); string a = treinoC.UltimoNome("Barbara Barbosa dos Santos"); Console.WriteLine(a); List<string> n = new List<string>() {"Barbara Barbosa", "Gabriel Rafael", "Larissa Medeiros"}; List<string> nomecomp = treinoC.UltimosNomes(n); Console.WriteLine(string.Join(", ", nomecomp)); bool x3 = treinoC.ApenasCoresPrimarias(new List<string>() {"preto", "branco", "azul"}); Console.WriteLine(x3); bool senhaF = treinoC.SenhaForte("eu-amo-csarp-com-ironia"); Console.WriteLine(senhaF); // B-a-r-b-a-r-a- // arabraB // True // Santos // Barbosa, Rafael, Medeiros // False // True ```
{"metaMigratedAt":"2023-06-16T07:38:27.791Z","metaMigratedFrom":"Content","title":"Session six | Repetição FOR","breaks":true,"contributors":"[{\"id\":\"efa8ac71-fa69-4ab5-8a89-20016d037e45\",\"add\":11725,\"del\":0}]"}
Expand menu