Ver Mensaje Individual
  #2 (permalink)  
Antiguo 19/01/2012, 03:14
htmorales
 
Fecha de Ingreso: enero-2012
Mensajes: 3
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: C Sharp Generador de CURP validado Parte 1

Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OBERON_12
{
    class CURP
    {
        public string GeneraCURP(string PrimerApellido, string SegundoApellido, string Nombres, string FechaNacimientoDMY, Boolean EsHombre, string EstadoNacimiento)
        {
//Declaración de variables.
            string Ap1 = PrimerApellido;
            string Ap2 = SegundoApellido;
            string Nom = Nombres;
            string FNac = FechaNacimientoDMY;
            string ENac = EstadoNacimiento;
            System.Text.StringBuilder CURP = new System.Text.StringBuilder("################");
            string Temp = "";
//Removemos cadenas que contienen solo caracteres vacios.
            Ap1 = Ap1.Trim();
            Ap2 = Ap2.Trim();
            Nom = Nom.Trim();
            ENac = ENac.Trim();
//Convertimos el texto a mayúsculas.
            Ap1 = Ap1.ToUpper();
            Ap2 = Ap2.ToUpper();
            Nom = Nom.ToUpper();
            ENac = ENac.ToUpper();
//Removemos acentos, diéresis, etc.
            Ap1 = RemoverSignosAcentos(Ap1);
            Ap2 = RemoverSignosAcentos(Ap2);
            Nom = RemoverSignosAcentos(Nom);
            ENac = RemoverSignosAcentos(ENac);
//Validamos que la informacion presentada sea correcta.
//Validaciones para Apellidos.
            if ((Ap1.Length > 0) || (Ap2.Length > 0))
            {   
                if ((Ap1.Length==0) && (Ap2.Length!=0))
                {
                    Ap1 = Ap2;
                    Ap2 = "";
                }
                if (EsCompuesto(Ap1))
                    Ap1 = RemoverPalabras(Ap1, false);
                CURP[0] = PrimerLetra(Ap1);
                CURP[1] = PrimerVocal(Ap1, 1);
                CURP[13] = PrimerConsonante(Ap1, 1);

                if (EsCompuesto(Ap2))
                    Ap2 = RemoverPalabras(Ap2, false);
                CURP[2] = PrimerLetra(Ap2);
                CURP[14] = PrimerConsonante(Ap2, 1);
            }
//Validaciones  y cálculo para el Nombre.
            if (Nom.Length != 0)
            {
                if (EsCompuesto(Nom))            
                    Nom = RemoverPalabras(Nom, true);
                CURP[3] = PrimerLetra(Nom);
                CURP[15] = PrimerConsonante(Nom, 1);
            }
//Validaciones  y cálculo para la FECHA DE NACIMIENTO.
            if (VerificaFecha(FNac))
            {
                Temp = FechaCortaYMD(FNac);
                for (int i = 0; i < 6; i++)
                {
                    CURP[i + 4] = Temp[i];
                }
            }
//Validaciones y cálculo para SEXO.
            if (EsHombre)
                CURP[10] = 'H';
            else
                CURP[10] = 'M';
//Validaciones y cálculo para SIGLAS DE ESTADO.
            Temp = SiglasEstado(ENac);
            CURP[11] = Temp[0];
            CURP[12] = Temp[1];
//Valida si es Palabra Inconveniente.
            Temp = "" + CURP[0] + CURP[1] + CURP[2] + CURP[3];
            if (EsPalabraInapropiada(Temp))
                CURP[1] = 'X';
//El resultado esta listo.
        return CURP.ToString();
        }

        public char PrimerLetra(string Palabra)
        {
            char Ppn = ' ';
            if (Palabra.Length == 0)
                Ppn = 'X';
            else
                if ((Palabra[0] == 'Ñ') || !(char.IsLetter(Palabra[0])))
                    Ppn = 'X';
                else
                    Ppn = Palabra[0];
            return Ppn;
        }

        public char PrimerVocal(string Palabra, int PosicionInicial)
        {
            char Vocal = ' ';
            if ((Palabra.Length-1) >= PosicionInicial)
            {
                for (int B = PosicionInicial; B < Palabra.Length; B++)
                {
                    if (Palabra[B] == 'A' || Palabra[B] == 'E' || Palabra[B] == 'I' || Palabra[B] == 'O' || Palabra[B] == 'U')
                    {
                        Vocal = Palabra[B];
                        break;
                    }
                }
            }
            if ((Vocal == ' ') || (Vocal == 'Ñ') || !(char.IsLetter(Vocal)))
                Vocal = 'X';
            return Vocal;
        }

        public char PrimerConsonante(string Palabra, int PosicionInicial)
        {
            char Consonante = ' ';

            if ((Palabra.Length-1) >= PosicionInicial)
            {
                for (int B = PosicionInicial; B < Palabra.Length; B++)
                {
                    if (Palabra[B] != 'A' && Palabra[B] != 'E' && Palabra[B] != 'I' && Palabra[B] != 'O' && Palabra[B] != 'U')
                    {
                        Consonante = Palabra[B];
                        break;
                    }
                }
            }
            if ((Consonante == ' ') || (Consonante == 'Ñ') || !(char.IsLetter(Consonante)))
                Consonante = 'X';
            return Consonante;
        }

        public static string RemoverPalabras(string Texto, Boolean EsNombre)
        {
            Boolean Concordancia = false;
            string s = Texto;
            string[] Words = s.Split(' ');
            string[] PCC = { "DA", "DAS", "DE", "DEL", "DER", "DI", "DIE", "DD", "EL", "LA", "LOS", "LAS", "LE", "LES", "MAC", "MC", "VAN", "VON", "Y" };
            string[] PCCN = { "MARIA", "MA.", "MA", "JOSE", "J", "J." };
            s = "";
            foreach (string xWords in Words)
            {
                Concordancia = false;
                foreach (string xPCC in PCC)
                {
                    if (xWords == xPCC)
                    {
                        Concordancia = true;
                        break;
                    }
                }
                if (!Concordancia)
                    s += xWords + " ";
            }
            s = s.Trim();
            if (EsNombre && EsCompuesto(s))
            {
                string[] WordsN = s.Split(' ');
                Boolean Concordado = false;
                s = "";
                foreach (string xWords in WordsN)
                {
                    Concordancia = false;
                    if (!Concordado)
                    {
                        foreach (string xPCCN in PCCN)
                        {
                            if (xWords == xPCCN)
                            {
                                Concordancia = true;
                                Concordado = true;
                                break;
                            }
                        }
                    }
                    if (!Concordancia)
                        s += xWords + " ";
                }
            }
            return s.Trim();
        }

        public static Boolean EsCompuesto(string Texto)
        {
            string s = Texto;
            string[] words = s.Split(' ');
            if (words.Length > 1)
                return true;
            else
                return false;
        }
(Continua mas abajo....)