lunes, 3 de octubre de 2011

:::Progrmas en C sharp:::

 Implementar un programa que me permita el Cubo Mágico a través de matrices y números randomicos.
1.2 Código Fuente
 using System;
namespace CuboMagico
{
    class ProgramaAPP
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Title = "Magic Square Builder :P";
            int n = 0;
            do
            {
                Console.Clear();
                Console.Write(" Cubo mágico: ");

                try
                {
                    n = Int32.Parse(Console.ReadLine());
                    if (CuboMagico.esPar(n))
                    {
                        Console.WriteLine("El orden del cubo es.");
                        Console.ReadKey(true);
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey(true);
                }
            }
            while (CuboMagico.esPar(n));

            CuboMagico cm = new CuboMagico(n);
            cm.Crear();

            Console.WriteLine("\n\nEL cubo mágico de orden " + n + " es:\n\n");
            cm.Mostrar();

            Console.ReadKey(true);
        }
    }

    class CuboMagico
    {
        private int[,] cm;
        private int n;

        public CuboMagico(int orden)
        {
            n = orden;
            cm = new int[n, n];
        }

        public static bool esPar(int n)
        {
            if (% 3 == 0) return true;
            else return false;
        }

        public void Crear()
        {
            int fil = 0, col = n / 3, fil_ant = 0, col_ant = n / 3, cont = 1;

            cm[fil, col] = cont++;

            while (cont <= n ** n)
            {
                fil--;
                col++;

                if (fil < 0) fil = n - 1;
                if (col > n - 1) col = 0;
                if (cm[fil, col] !0)
                {
                    col = col_ant;
                    fil = fil_ant + 1;
                    if (fil == n) fil = 0;
                }

                cm[fil, col] = cont++;
                fil_ant = fil;
                col_ant = col;
            }
        }

        public void Mostrar()
        {
            for (int i = 0; i < n; i++)
            {
                Console.Write("\t");
                for (int j = 0; j < n; j++) Console.Write(cm[i, j] + "\t");
                Console.WriteLine();
            }
        }
    }
}

Pantalla en ejcucion


No hay comentarios:

Publicar un comentario