Foros del Web » Programación para mayores de 30 ;) » .NET »

identificar romano si es par o impar

Estas en el tema de identificar romano si es par o impar en el foro de .NET en Foros del Web. Hola amigos mi profesor me paso este codigo Código: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace STLibraries.Roman { class Program { static void ...
  #1 (permalink)  
Antiguo 18/04/2012, 16:44
Avatar de newmesis  
Fecha de Ingreso: octubre-2010
Ubicación: Chillán, Chile, Chile
Mensajes: 42
Antigüedad: 13 años, 6 meses
Puntos: 0
identificar romano si es par o impar

Hola amigos mi profesor me paso este codigo


Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace STLibraries.Roman
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                do
                {
                    string roman, unit;

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

                    roman = Console.ReadLine();

                    if (RomanIsPair(roman.ToUpper(), out unit))
                    {
                        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, out string unit)
        {
            bool isPair;
            int @long;
            char[] cRoman;

            isPair = false;
            roman = "@" + roman;
            @long = roman.Length;
            unit = string.Empty;
            cRoman = roman.ToCharArray();

            for (int i = 0; i < @long; i++)
            {
                int j = (@long - 1) - i;

                if (cRoman[j] != '@')
                {
                    if ((cRoman[j].ToString() == "I") ||
                            (cRoman[j].ToString() == "V") ||
                                (cRoman[j - 1].ToString() + cRoman[j].ToString() == "IX"))
                    {
                        unit = cRoman[j].ToString() + unit;
                    }
                    else { break; }
                }
            }

            return isPair;
        }

    }
}


el código debe identificar


se ingresar numero en romano

ejemplo

V que es igual a 5 el programa deve identificar si ese es numero par o impar

nesesito solo una referencia para poder ejecutar y arreglarlo
gracias
  #2 (permalink)  
Antiguo 19/04/2012, 11:31
 
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.

Etiquetas: impar, par, identificador
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:34.