Ver Mensaje Individual
  #2 (permalink)  
Antiguo 19/04/2012, 11:31
ericklanford
 
Fecha de Ingreso: diciembre-2011
Mensajes: 77
Antigüedad: 12 años, 4 meses
Puntos: 2
Respuesta: identificar romano si es par o impar

class Program
{
static void Main(string[] args)
{
try
{
do
{
string roman;

Console.Clear();
Console.Write("\nIngrese un número romano entre 1 y 3999: ");

roman = Console.ReadLine();

if (RomanIsPair(roman.ToUpper()))
{
Console.WriteLine("-> El número romano: {0}, es par.", roman);
}
else
{
Console.WriteLine("-> El número romano: {0}, es impar.", roman);
}

Console.Write("\nPresione 'Esc' para salir o una tecla para continuar...");

} while (ConsoleKey.Escape != Console.ReadKey().Key);
}
catch (Exception e)
{
Console.WriteLine("Se ha producido el siguiente error:");
Console.WriteLine(e.Message);
Console.ReadKey();
}

}

/// <summary>
/// unknown function
/// </summary>
/// <param name="roman"></param>
/// <param name="unit"></param>
/// <returns></returns>
static bool RomanIsPair(string roman)
{
int x = 0;

for (int j = roman.Length - 1; j >= 0 && x < 10; j--)
{
#region switch
switch (roman[j])
{
case 'I': x++; break;
case 'V':
if (j != 0 && roman[j - 1] == 'I')
{
x--;
j--;
}
x += 5;
break;
case 'X':
if (j != 0 && roman[j - 1] == 'I')
{
x--;
j--;
}
if (j != 0 && roman[j - 1] == 'V')
{
x -= 5;
j--;
}
x += 10;
break;
case 'L':
if (j != 0 && roman[j - 1] == 'I')
{
x--;
j--;
}
if (j != 0 && roman[j - 1] == 'V')
{
x -= 5;
j--;
}
if (j != 0 && roman[j - 1] == 'X')
{
x -= 10;
j--;
}
x += 50;
break;
case 'C':
if (j != 0 && roman[j - 1] == 'I')
{
x--;
j--;
}
if (j != 0 && roman[j - 1] == 'V')
{
x -= 5;
j--;
}
if (j != 0 && roman[j - 1] == 'X')
{
x -= 10;
j--;
}
if (j != 0 && roman[j - 1] == 'L')
{
x -= 50;
j--;
}
x += 100;
break;
case 'D':
if (j != 0 && roman[j - 1] == 'I')
{
x--;
j--;
}
if (j != 0 && roman[j - 1] == 'V')
{
x -= 5;
j--;
}
if (j != 0 && roman[j - 1] == 'X')
{
x -= 10;
j--;
}
if (j != 0 && roman[j - 1] == 'L')
{
x -= 50;
j--;
}
if (j != 0 && roman[j - 1] == 'C')
{
x -= 100;
j--;
}
x += 500;
break;
case 'M':
if (j != 0 && roman[j - 1] == 'I')
{
x--;
j--;
}
if (j != 0 && roman[j - 1] == 'V')
{
x -= 5;
j--;
}
if (j != 0 && roman[j - 1] == 'X')
{
x -= 10;
j--;
}
if (j != 0 && roman[j - 1] == 'L')
{
x -= 50;
j--;
}
if (j != 0 && roman[j - 1] == 'C')
{
x -= 100;
j--;
}
if (j != 0 && roman[j - 1] == 'D')
{
x -= 500;
j--;
}
x += 1000;
break;
}//swicht
#endregion
}//for
return x % 2 == 0;
}

}


Espero que te sirva.