Calcolare il CIN di un conto corrente

Moderator: Enrico Maria Giordano

Calcolare il CIN di un conto corrente

Postby Marco Turco » Wed Apr 12, 2006 9:57 am

Salve,
qualcuno di voi ha l'algoritmo per calcolare il CIN di un conto corrente ?

Grazie

Marco
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Postby Marco Turco » Wed Apr 12, 2006 10:50 am

Ho trovato questa funzione in visual basic ma ho difficoltà nel convertirla in harbour. Qualcuno di voi conosce visual basic e riesce a convertirla ?

Grazie in anticipo

Marco

public string CalcolaCin()
{
// costanti e variabili per calcolo pesi
const string numeri = "0123456789";
const string lettere = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-. ";
const int DIVISORE = 26;
int[] listaPari = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28};
int[] listaDispari = {1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23,27,28,26};


// normalizzazione dati
if (this.Abi.Length != L_ABI)
mAbi = NormalizzaDati(mAbi,L_ABI);
if (this.Cab.Length != L_CAB)
mCab = NormalizzaDati(mCab,L_CAB);
if (this.NormalizzaConto)
this.ContoCorrente = NormalizzaContoCorrente(this.ContoCorrente);

if (this.ContoCorrente.Length != L_CONTO)
this.ContoCorrente = this.ContoCorrente.PadRight(L_CONTO);


// codice normalizzato
string codice = this.Abi + this.Cab + this.ContoCorrente;


// calcolo valori caratteri
int somma = 0;
char[] c = codice.ToUpper().ToCharArray();

for (int k = 0; k < (L_CONTO + L_ABI + L_CAB); k++)
{
int i = numeri.IndexOf(c[k]);
if (i < 0)
i = lettere.IndexOf(c[k]);

// se ci sono caratteri errati usciamo con un valore
// impossibile da trovare sul cin

if (i < 0)
return Environment.NewLine;


if ((k % 2) == 0)
{
// valore dispari
somma += listaDispari[i];
}
else
{
// valore pari
somma += listaPari[i];
}

}
return lettere.Substring(somma % DIVISORE,1);
}
}
}
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Postby Enrico Maria Giordano » Wed Apr 12, 2006 12:28 pm

Il sorgente è incompleto.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Marco Turco » Wed Apr 12, 2006 2:35 pm

Hai ragione, scusa.
Questo è la routine completa.

----

La classe di controllo e calcolo CIN

Ho messo di default che il conto sia calcolato "normalizzato" a 12 caratteri con zeri a sinistra.
Visto che è una funzione poco utilizzata si limita ad eliminare gli spazi dal codice e a formattare il conto a 12 caratteri.
Il suo utilizzo deve essere pertanto limitato a: "consentimi di inserire il conto corrente senza zeri davanti, gli zeri che mancano aggiungili tu".
Se NON si vuole vengano aggiunti gli zeri sul conto si deve porre a false il campo "NormalizzaConto" (bool) prima del calcolo.

Esempio di utilizzo:

ClsBancari cls = new ClsBancari();
cls.Abi = this.txtAbi.Text
cls.Cab = this.txtCab.Text;
cls.Cin = this.txtCin.Text;
cls.ContoCorrente = this.txtCc.Text;
if (cls.VerificaCin())
MessageBox.Show("Corretto");
else
MessageBox.Show("Cin Calcolato: -" + cls.CalcolaCin() + "-");

---------------------------------------------


using System;
using System.Data;

namespace ProvaModuliCs

{

/// <summary>
/// Description of ClsBancari.
/// </summary>

public class ClsBancari
{
public ClsBancari()
{
mAbi = String.Empty;
mCab = String.Empty;
mContoCorrente = String.Empty;
mCin = String.Empty;
mNormalizzaConto = true;
}
private string mAbi;
private string mCab;
private string mContoCorrente;
private string mCin;
private const int L_CONTO = 12;
private const int L_ABI = 5;
private const int L_CAB = 5;
private bool mNormalizzaConto;


public string Abi
{
get
{
return mAbi;
}
set
{
mAbi = NormalizzaDati(value,L_ABI);
}
}

public string Cab
{
get
{
return mCab;
}
set
{
mCab = NormalizzaDati(value,L_CAB);
}
}


public string ContoCorrente
{
get
{
return mContoCorrente;
}
set
{
mContoCorrente = value;
}
}


public string Cin
{
get
{
return mCin;
}
set
{
mCin = value;
}
}


public bool NormalizzaConto
{
get
{
return mNormalizzaConto;
}
set
{
mNormalizzaConto = value;
}
}


private string NormalizzaDati(string codice, int lunghezza)
{
codice = codice.Trim();
int k = codice.Length;
if (k < lunghezza)
{
codice = "".PadLeft(lunghezza,'0') + codice;
k += lunghezza;
}
k -= lunghezza;
if (k < 0)
k = 0;
codice = codice.Substring(k);
return codice;
}


public string NormalizzaContoCorrente(string contoCorrenteValue)
{
contoCorrenteValue = contoCorrenteValue.Trim();
int k = contoCorrenteValue.IndexOf(' ');
while (k >= 0)
{
contoCorrenteValue = contoCorrenteValue.Remove(k,1);
k = contoCorrenteValue.IndexOf(' ');
}
return NormalizzaDati(contoCorrenteValue,L_CONTO);
}


public bool VerificaCin(string cinCode)
{
return (cinCode == CalcolaCin());
}


public string CalcolaCin()
{
// costanti e variabili per calcolo pesi
const string numeri = "0123456789";
const string lettere = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-. ";
const int DIVISORE = 26;
int[] listaPari = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28};
int[] listaDispari = {1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23,27,28,26};


// normalizzazione dati
if (this.Abi.Length != L_ABI)
mAbi = NormalizzaDati(mAbi,L_ABI);
if (this.Cab.Length != L_CAB)
mCab = NormalizzaDati(mCab,L_CAB);
if (this.NormalizzaConto)
this.ContoCorrente = NormalizzaContoCorrente(this.ContoCorrente);

if (this.ContoCorrente.Length != L_CONTO)
this.ContoCorrente = this.ContoCorrente.PadRight(L_CONTO);


// codice normalizzato
string codice = this.Abi + this.Cab + this.ContoCorrente;


// calcolo valori caratteri
int somma = 0;
char[] c = codice.ToUpper().ToCharArray();

for (int k = 0; k < (L_CONTO + L_ABI + L_CAB); k++)
{
int i = numeri.IndexOf(c[k]);
if (i < 0)
i = lettere.IndexOf(c[k]);

// se ci sono caratteri errati usciamo con un valore
// impossibile da trovare sul cin

if (i < 0)
return Environment.NewLine;


if ((k % 2) == 0)
{
// valore dispari
somma += listaDispari[i];
}
else
{
// valore pari
somma += listaPari[i];
}

}
return lettere.Substring(somma % DIVISORE,1);
}
}
}
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Postby Enrico Maria Giordano » Wed Apr 12, 2006 3:46 pm

Prova questo e fammi sapere se funziona:

Code: Select all  Expand view
FUNCTION MAIN()

    LOCAL cAbi := "12345"
    LOCAL cCab := "67890"
    LOCAL cCon := "000000012345"

    ? CALCOLACIN( cAbi, cCab, cCon )

    RETURN NIL


FUNCTION CALCOLACIN( cAbi, cCab, cCon )

    LOCAL aPari := { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 }
    LOCAL aDisp := { 1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23, 27, 28, 26 }

    LOCAL cNume := "0123456789"
    LOCAL cLett := "ABCDEFGHIJKLMNOPQRSTUVWXYZ-. "

    LOCAL cCod := UPPER( cAbi + cCab + cCon )

    LOCAL nSomma := 0

    LOCAL i, n

    FOR i = 1 TO LEN( cCod )
        n = AT( SUBSTR( cCod, i, 1 ), cNume )

        IF n = 0
            n = AT( SUBSTR( cCod, i, 1 ), cLett )
        ENDIF

        IF i % 2 = 0
            nSomma += aPari[ n ]
        ELSE
            nSomma += aDisp[ n ]
        ENDIF
    NEXT

    RETURN SUBSTR( cLett, ( nSomma + 1 ) % 26, 1 )


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Marco Turco » Thu Apr 13, 2006 8:28 am

Grazie. Funziona perfettamente.

Saluti

Marco Turco
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London


Return to All products support

Who is online

Users browsing this forum: No registered users and 27 guests