-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Proposta de Solução desafio-02. * Efetuadas correções sugeridas * Removendo op-desafios.sln * Proposta de solução do exercício 03 em csharp. * Corrigidos erros de Trailing Spaces * Adicionado instruções de como executar o teste. * Correção do nome do arquivo * Correção do desafio 03 * Alterado entrada da aplicação para que o usuário possa efetuar testes variados por input de parâmetros.
- Loading branch information
1 parent
fdd4749
commit 17de511
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
|
||
namespace palindromos; | ||
|
||
public class Palindromo | ||
{ | ||
public Palindromo() { } | ||
|
||
public bool EhPalindromo(ulong numero){ | ||
// Converte o número para uma string. | ||
string numeroStr = numero.ToString(); | ||
|
||
// Inverte a string. | ||
char[] charArray = numeroStr.ToCharArray(); | ||
Array.Reverse(charArray); | ||
string numeroInvertidoStr = new string(charArray); | ||
|
||
// Compara a string original com a string invertida. | ||
return numeroStr.Equals(numeroInvertidoStr); | ||
} | ||
|
||
public List<ulong> PalindromosEntre(string inicio, string fim) | ||
{ | ||
ValidarParametros(inicio); | ||
ValidarParametros(fim); | ||
|
||
List<ulong> resultado = new List<ulong>(); | ||
|
||
for (ulong i = UInt64.Parse(inicio); i <= UInt64.Parse(fim); i++) | ||
{ | ||
if (EhPalindromo(i)) | ||
{ | ||
resultado.Add(i); | ||
} | ||
} | ||
|
||
return resultado; | ||
} | ||
|
||
private static void ValidarParametros(string numero) | ||
{ | ||
try | ||
{ | ||
var conversao = UInt64.Parse(numero); | ||
} catch (OverflowException) | ||
{ | ||
throw new OverflowException("Valor maior ou menor que o permitido"); | ||
} catch (ArgumentNullException) | ||
{ | ||
throw new ArgumentNullException("Valor não pode ser nulo"); | ||
} catch (FormatException) | ||
{ | ||
throw new FormatException("Valor inválido"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace palindromos; | ||
|
||
class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
Palindromo palindromo = new Palindromo(); | ||
|
||
Console.WriteLine("Informe o número inicial para validação dos palíndromos: "); | ||
string inicio = Console.ReadLine(); | ||
Console.WriteLine("Informe o número final para validação dos palíndromos: "); | ||
string fim = Console.ReadLine(); | ||
|
||
Console.WriteLine("\nNumeros palíndromos entre " + inicio + " e " + fim + ": "); | ||
foreach (ulong item in palindromo.PalindromosEntre(inicio, fim)) | ||
{ | ||
Console.WriteLine(item); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
</Project> |