sábado, 23 de febrero de 2013

Algoritmos Java y VB 6.0

este algorimo, es desarrollado por mi, es muy optimo y rapido no usa recursividad ni uso de substrings excesivos, ni tampoco divisiones concurrentes, se puede acceder casi directo a las cadenas de lectura, solo calculando la posicion con simples operaciones aritmeticas, copiar y pegar en una clase. y llamar al metodo desde el main abajo anexo la explicacion del algoritmo y los links de los codigos fuentes.

Ej: (clase).readNumber( "28377273.83", ".", "pesos" )
salida>>  veintiocho millones trescientos setenta y siete mil doscientos setenta y tres pesos con 83 centavos

private static final int  Unidad = 1; 
private static final int  Decena = 10;
private static final int  Centena = 100;

public static String readNumber( String Number, String sepDecimal, String sMoney ){
      
      String  V[] = initVector(), s = "", z = "", c = "", e = " ", t;
      int l = Number.length(), k = Number.indexOf( sepDecimal ), u = 1, n = 0, j = 0, b = 0, d, p, r;
     
      try{

           //obtiene los decimales
            if( k >= 0 ) { c = Number.substring( k + 1, l );  l = k; }
           
            if ( l <= 15 )
            {
                        for( int i = l ; i >= 1; i-- ){
                            d = Integer.parseInt( String.valueOf( Number.charAt( i - 1 ) ));
                            n = ( d * u ) + n;

                                switch( u ){
                                    case Unidad:
                                        s = V[ n ];
                                        if ( i == l && n == 1 ) b++;
                                    break;
                     case Decena:                                               
                                        p = d - 2;
                                   
                                        if( p < 0 )  
                                            s = V[ n ];                                     
                                        else{
                                            t =  V[ 20 + p ];
                                           
                                            if( n % 10 != 0 )
                                               s  =  (d == 2)? "veinti" + s : t + " y " + s;
                                            else   
                                               s = t;
                                         }
                                    break;
                                    case Centena:
                                       p = d - 1;
                                       t = V[ 30 + p ];
        
                                       if( n % 100  == 0 )
                                       { s = ""; e = ""; } 
                                       else
                                         if( d == 1 ) t += "to"; 
                                      
                                       s = t + e + s;                                      
                                       z = ( s + z );  
                                    break;         
                                } 
                               
                                e = " ";    
                              //ini. calcula los miles, millones, billones
                                r = l - i;                               
                                if( r > 0 && r % 3 == 0  ){
                                        p = ( r > 2 )?  2 : j++ & 1;    
                                        t = V[ 40 + p ];
                                       
                                        if( p > 0 )
                                           if( ( n == 1 && i > 1 ) || n > 1  ) t += "es";
                                       
                                        z = e + t + e + z;
                                }
                              //fin.
                                

                               //reinicia las variables
                               if ( u == Centena ){  u = 1;  n = 0;  s = "";  } else u *= 10;                                
                         }
                      
             }     

           //ini. adiciona la moneda y los centavos
                if ( !c.equals("") ) c = " con " + c + " centavos";           
                if ( !sMoney.equals("") )       
                    sMoney = " " + sMoney;
                else
                    if( b > 0 ) z += "o"; 
           //fin.
           
            z = ( s + z ) + sMoney + c;
      }
      catch(NumberFormatException ex){
            z = "ERROR [readNumber]: Formato numerico incorrecto.";
      }
     
     return z;
    
   }

  
  
   private static String[] initVector(  ){
       String V[] = new String[43];
       
        V[0] = "cero";
        V[1] = "un";
        V[2] = "dos";
        V[3] = "tres";
        V[4] = "cuatro";
        V[5] = "cinco";
        V[6] = "seis";
        V[7] = "siete";
        V[8] = "ocho";
        V[9] = "nueve";
        V[10] = "diez";
        V[11] = "once";
        V[12] = "doce";
        V[13] = "trece";
        V[14] = "catorce";
        V[15] = "quince";
        V[16] = "dieciseis";
        V[17] = "diecisiete";
        V[18] = "dieciocho";
        V[19] = "diecinueve";
        V[20] = "veinte";
        V[21] = "treinta";
        V[22] = "cuarenta";
        V[23] = "cincuenta";
        V[24] = "secenta";
        V[25] = "setenta";
        V[26] = "ochenta";
        V[27] = "noventa";
        V[28] = "";
        V[29] = "";
        V[30] = "cien";
        V[31] = "doscientos";
        V[32] = "trescientos";
        V[33] = "cuatrocientos";
        V[34] = "quinientos";
        V[35] = "seiscientos";
        V[36] = "setecientos";
        V[37] = "ochocientos";
        V[38] = "novecientos";
        V[39] = "";
        V[40] = "mil";
        V[41] = "millon";
        V[42] = "billon";
       
        return V;
   }


}





 este tambien lo hice es a misma version pero en visual vb 6 y sin algunos ajustes que tiene el anterior JAVA





Const Unidad = 1
Const Decena = 10
Const Centena = 100
Const UMil = 1000
Const CMil = 100000
Const Millon = 1000000
Const UMMillon = 1000000000
Const CMMillon = 100000000000#
Const MMMillon = 1000000000000#



' CONVIERTE DE NUMERO A TEXTO
Public Function cvtNumTxt(Numero) As Variant
Const b = 10
Dim C, R, U, S, P, T, D, M, K, V()

Call LLenarVector(V)

C = Numero
L = False
U = 1
M = 1

If C <> "" Then
  If C > 0 Then
     Do
            P = ""
            R = ModL(C, b)
            C = Int(C / b)
            T = T + R * U
       
           ' VERIFICA LAS UNIDADES, DECENAS, CENTENAS
            If R > 0 Then
                Select Case U
                    Case Unidad
                        If M > 100 Then _
                           V(1) = "Un"
                        D = V(T)
                        If C = 0 Then P = D
                    Case Decena
                        If R = 1 Then
                          P = V(T)
                        Else
                          P = V(20 + (R - 2))
                          If T Mod b > 0 Then
                            If T >= 30 Then
                              P = P & " y " & D
                            Else
                              P = "Veinti" & LCase(D)
                            End If
                          End If
                        End If
                        P = " " & P
                    Case Centena
                        P = V(30 + (R - 1))
                        If T > 100 And T < 200 Then _
                           P = P & "to"
                        P = " " & P
               End Select
            Else
              If U = b Then P = " " & D
            End If
   
            If M > 100 Then
                ' VERIFICA LAS UNIDADES DE MIL, MILLONES Y BILLONES
                  Select Case M
                     Case UMil To CMil, UMMillon To CMMillon
                      If U = 1 And R = 1 Then P = ""
                        If T > 0 And K = 0 Then
                         P = P & " Mil"
                         K = 1
                       End If
                     Case Millon
                       P = P & " Millon"
                       If Not (T = 1 And C = 0) Then _
                           P = P & "es"
                       K = 0
                     Case MMMillon
                       P = P & " Billon"
                       If Not (T = 1 And C = 0) Then _
                            P = P & "es"
                       M = 1
                 End Select
              End If
            
            If U = Centena Then
                U = 1
                T = 0
                D = ""
            Else
               U = U * b
            End If
   
                     
        M = M * b
        S = P & S
                               
      Loop While (C <> 0)
           
    Else
       S = V(0)
    End If
End If

cvtNumTxt = Trim(S)

End Function



' PERMITE INTRODUCIR NUMEROS MAYORES A 1000 MILLONES PARA SOLUCIONAR ERROR EN MOD
Public Function ModL(Num1, Num2) As Variant
ModL = Num1 - (Int(Num1 / Num2) * Num2)
End Function




Private Sub LLenarVector(ByRef Vec)

ReDim Vec(43)

Vec(0) = "Cero"
Vec(1) = "Uno"
Vec(2) = "Dos"
Vec(3) = "Tres"
Vec(4) = "Cuatro"
Vec(5) = "Cinco"
Vec(6) = "Seis"
Vec(7) = "Siete"
Vec(8) = "Ocho"
Vec(9) = "Nueve"
Vec(10) = "Diez"
Vec(11) = "Once"
Vec(12) = "Doce"
Vec(13) = "Trece"
Vec(14) = "Catorce"
Vec(15) = "Quince"
Vec(16) = "Dieciseis"
Vec(17) = "Diecisiete"
Vec(18) = "Dieciocho"
Vec(19) = "Diecinueve"
Vec(20) = "Veinte"
Vec(21) = "Treinta"
Vec(22) = "Cuarenta"
Vec(23) = "Cincuenta"
Vec(24) = "Secenta"
Vec(25) = "Setenta"
Vec(26) = "Ochenta"
Vec(27) = "Noventa"
Vec(28) = ""
Vec(29) = ""
Vec(30) = "Cien"
Vec(31) = "Doscientos"
Vec(32) = "Trescientos"
Vec(33) = "Cuatrocientos"
Vec(34) = "Quinientos"
Vec(35) = "Seiscientos"
Vec(36) = "Setecientos"
Vec(37) = "Ochocientos"
Vec(38) = "Novecientos"

End Sub