Autor Tema: Ayuda en convertidor de numero romano a decimal en C#  (Leído 2606 veces)

nya_26

  • Sin experiencia
  • *
  • APR2.COM
  • Mensajes: 1
    • Ver Perfil
Ayuda en convertidor de numero romano a decimal en C#
« en: 31 de Agosto 2016, 02:10 »
Hola, he buscado muchos códigos para convertir de numero romano a decimal pero a la mayoría no le entiendo a el 100% alguien me podría explicar qué hace este codigo :( :( :( :( :) :) :) :) :) ;D ;D ;D ;D ;D ;D



Código: [Seleccionar]
using System; 
    using System.Text; 
     
    namespace RomanNumerals 
    { 
        static class Roman 
        { 
            static string[] roman1 = { "MMM", "MM", "M" }; 
            static string[] roman2 = { "CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C" }; 
            static string[] roman3 = { "XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X" }; 
            static string[] roman4 = { "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I" }; 
     
            public static bool TryParse(string text, out int value) 
            { 
                value = 0; 
                if (String.IsNullOrEmpty(text)) return false; 
                text = text.ToUpper(); 
                int len = 0; 
     
                for (int i = 0; i < 3; i++) 
                { 
                    if (text.StartsWith(roman1[i])) 
                    { 
                        value += 1000 * (3 - i); 
                        len = roman1[i].Length; 
                        break; 
                    } 
                } 
     
                if (len > 0) 
                { 
                    text = text.Substring(len); 
                    len = 0; 
                } 
     
                for (int i = 0; i < 9; i++) 
                { 
                    if (text.StartsWith(roman2[i])) 
                    { 
                        value += 100 * (9 - i); 
                        len = roman2[i].Length; 
                        break; 
                    } 
                } 
     
                if (len > 0) 
                { 
                    text = text.Substring(len); 
                    len = 0; 
                } 
     
                for (int i = 0; i < 9; i++) 
                { 
                    if (text.StartsWith(roman3[i])) 
                    { 
                        value += 10 * (9 - i); 
                        len = roman3[i].Length; 
                        break; 
                    } 
                } 
     
                if (len > 0) 
                { 
                    text = text.Substring(len); 
                    len = 0; 
                } 
     
                for (int i = 0; i < 9; i++) 
                { 
                    if (text.StartsWith(roman4[i])) 
                    { 
                        value += 9 - i; 
                        len = roman4[i].Length; 
                        break; 
                    } 
                } 
     
                if (text.Length > len) 
                { 
                    value = 0; 
                    return false; 
                } 
     
                return true; 
            } 
     
            public static string ToRoman(int num) 
            { 
                if (num > 3999) throw new ArgumentException("Too big - can't exceed 3999"); 
                if (num < 1) throw new ArgumentException("Too small - can't be less than 1"); 
                int thousands, hundreds, tens, units; 
                thousands = num / 1000; 
                num %= 1000; 
                hundreds = num / 100; 
                num %= 100; 
                tens = num / 10; 
                units = num % 10; 
                var sb = new StringBuilder(); 
                if (thousands > 0) sb.Append(roman1[3 - thousands]); 
                if (hundreds > 0) sb.Append(roman2[9 - hundreds]); 
                if (tens > 0) sb.Append(roman3[9 - tens]); 
                if (units > 0) sb.Append(roman4[9 - units]); 
                return sb.ToString(); 
            } 
        }   
     
        class Program 
        { 
            static void Main() 
            { 
                string[] romans = { "I", "MMIV", "DCL", "CCXXII", "IIII", "CZ", "MLXVI", "III", "DCCXIV", "MIM" }; 
                int value = 0; 
     
                foreach (string roman in romans) 
                { 
                    if (Roman.TryParse(roman, out value)) Console.WriteLine("{0} = {1}", roman, value); 
                } 
     
                Console.WriteLine(); 
     
                int[] numbers = { 1, 2004, 650, 222, 4, 99, 1066, 3, 714, 1999, 3999 }; 
     
     
                foreach (int number in numbers) 
                { 
                    Console.WriteLine("{0} = {1}", number, Roman.ToRoman(number)); 
                } 
     
                Console.ReadKey(); 
            } 
             
        } 
    } 
« Última modificación: 06 de Septiembre 2016, 10:47 por César Krall »

César Krall

  • Moderador Global
  • Experto
  • *******
  • Mensajes: 2078
  • No vales por lo que dices, sino por lo que haces
    • Ver Perfil
    • aprenderaprogramar.com
Re:Ayuda en convertidor de numero romano a decimal en C#
« Respuesta #1 en: 06 de Septiembre 2016, 10:49 »
Hola! Para escribir en los foros es importante seguir las indicaciones que están explicadas en https://www.aprenderaprogramar.com/foros/index.php?topic=1460.0

Es importante que indiques con qué lenguaje estás trabajando (incluso si puedes informar de versión o compilador o cualquier otro dato mejor), también es importante que el código lo pegues conforme se indica en ese hilo

Para comprender el código hacen falta tener bases de programación, si estás empezando desde cero te recomiendo leer este hilo: https://www.aprenderaprogramar.com/foros/index.php?topic=1313.0

Saludos!
Responsable de departamento de producción aprenderaprogramar.com

 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".