Created
June 26, 2013 16:16
-
-
Save jomasero/5868819 to your computer and use it in GitHub Desktop.
[Falta probar este código] Convertir un número romano a un número arábigo.
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| namespace Romanos | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| string [] Romanos = new string [] {"MDCCCXII", "XXI", "IX", "I", "DXIV"}; | |
| foreach (string Número in Romanos) | |
| Console.WriteLine (Número + ": " + getArabigo (Número)); | |
| } | |
| class Símbolo | |
| { | |
| public char Clave; | |
| public int Valor; | |
| public Símbolo Prefijo; | |
| public Símbolo (char Clave, int Valor, Símbolo Prefijo) | |
| { | |
| this.Clave = Clave; | |
| this.Valor = Valor; | |
| this.Prefijo = Prefijo; | |
| } | |
| } | |
| static Símbolo I = new Símbolo ('I', 1, null); | |
| static Símbolo V = new Símbolo ('V', 5, I); | |
| static Símbolo X = new Símbolo ('X', 10, I); | |
| static Símbolo L = new Símbolo ('L', 50, X); | |
| static Símbolo C = new Símbolo ('C', 100, X); | |
| static Símbolo D = new Símbolo ('D', 500, C); | |
| static Símbolo M = new Símbolo ('M', 1000, C); | |
| static Símbolo [] Símbolos = new Símbolo [] {I, V, X, L, C, D, M}; | |
| static int getIndiceSímbolo (char Clave) | |
| { | |
| for (int i = 0; i < Símbolos.Length; ++i) | |
| if (Símbolos [i].Clave == Clave) | |
| return i; | |
| return -1; | |
| } | |
| static int getArabigo (string Romano) | |
| { | |
| int Resultado = 0; | |
| Símbolo Previo = null; | |
| for (int i = 0; i < Romano.Length; ++i) | |
| { | |
| Símbolo Actual = Símbolos [getIndiceSímbolo (Romano [i])]; | |
| Resultado += Actual.Valor; | |
| if (Previo != null && Actual.Prefijo == Previo) | |
| Resultado -= 2 * Previo.Valor; | |
| Previo = Actual; | |
| } | |
| return Resultado; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment