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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
private string CodiceControllo(string codice_fiscale) { int somma = 0; int resto = 0; for (int i = 0; i < 15; i++){ if (i == 0 || i == 2 || i == 4 || i == 6 || i == 8 || i == 10 || i == 12 || i == 14) { switch (codice_fiscale[i]) { case 'A': somma += 1; break; case 'B': somma += 0; break; case 'C': somma += 5; break; case 'D': somma += 7; break; case 'E': somma += 9; break; case 'F': somma += 13; break; case 'G': somma += 15; break; case 'H': somma += 17; break; case 'I': somma += 19; break; case 'J': somma += 21; break; case 'K': somma += 2; break; case 'L': somma += 4; break; case 'M': somma += 18; break; case 'N': somma += 20; break; case 'O': somma += 11; break; case 'P': somma += 3; break; case 'Q': somma += 6; break; case 'R': somma += 8; break; case 'S': somma += 12; break; case 'T': somma += 14; break; case 'U': somma += 16; break; case 'V': somma += 10; break; case 'W': somma += 22; break; case 'X': somma += 25; break; case 'Y': somma += 24; break; case 'Z': somma += 23; break; case '0': somma += 1; break; case '1': somma += 0; break; case '2': somma += 5; break; case '3': somma += 7; break; case '4': somma += 9; break; case '5': somma += 13; break; case '6': somma += 15; break; case '7': somma += 17; break; case '8': somma += 19; break; case '9': somma += 21; break; } } else { switch (codice_fiscale[i]) { case 'A': somma += 0; break; case 'B': somma += 1; break; case 'C': somma += 2; break; case 'D': somma += 3; break; case 'E': somma += 4; break; case 'F': somma += 5; break; case 'G': somma += 6; break; case 'H': somma += 7; break; case 'I': somma += 8; break; case 'J': somma += 9; break; case 'K': somma += 10; break; case 'L': somma += 11; break; case 'M': somma += 12; break; case 'N': somma += 13; break; case 'O': somma += 14; break; case 'P': somma += 15; break; case 'Q': somma += 16; break; case 'R': somma += 17; break; case 'S': somma += 18; break; case 'T': somma += 19; break; case 'U': somma += 20; break; case 'V': somma += 21; break; case 'W': somma += 22; break; case 'X': somma += 23; break; case 'Y': somma += 24; break; case 'Z': somma += 25; break; case '0': somma += 0; break; case '1': somma += 1; break; case '2': somma += 2; break; case '3': somma += 3; break; case '4': somma += 4; break; case '5': somma += 5; break; case '6': somma += 6; break; case '7': somma += 7; break; case '8': somma += 8; break; case '9': somma += 9; break; } } } Math.DivRem(somma, 26, out resto); return char.ConvertFromUtf32(resto+65); }
Refactorings
No refactoring yet !
danielharan
December 19, 2007, December 19, 2007 18:19, permalink
That code is an abomination. Two things for you to look at: modulo operator and arrays. It should only be about 12 lines once you are done.
Jonathan Starr
December 20, 2007, December 20, 2007 00:11, permalink
It looks like you are enciphering some of the characters but not all of them.
I separated out the logic to decide which cipher to use in method isCipher1 base on character position.
I also tried to give variable names a more meaningful name - really "i" is a bad variable name....
I converted the input string to an array of characters as the string codice_fiscale[i] is meaningless - this wiull return nothing - but the character array chars_codice_fiscale[characterPosition] will return one chacter for each characterPosition. Also I like to use the IsNumber function wrapped in the Char class for testing whether a character is a number...
Finally, instead of long switch statements I used an int array, and position in the ascii table to assign values.
The end product is more maintainable, and I included some error checking as well.. (I think you still need to check that the string is an empty string or null to make this better, IMHO.
Hope this helped you,
Jonathan Starr
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
private bool isCipher1( int characterPosition) { int[] cipher1Chars = new int[] { 0, 2, 4, 6, 8, 10, 12, 14 }; foreach ( int cipher1Char in cipher1Chars) { if (characterPosition == cipher1Char) return true; } return false; } private string CodiceControllo(string codice_fiscale) { int somma = 0; int resto = 0; int [] cipher1 = new int [] {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,0,5,7,9,13,15,17,19,21}; char [] chars_codice_fiscale = codice_fiscale.ToUpper().ToCharArray(); for (int characterPosition = 0; characterPosition < 15; characterPosition++) { if (isCipher1(characterPosition)) { if (Char.IsNumber(chars_codice_fiscale[characterPosition])) { somma += cipher1 [ Int32.Parse(chars_codice_fiscale[characterPosition]) + 26] ; continue; } if (Char.IsLetter(chars_codice_fiscale[characterPosition])) { somma += cipher1 [Convert.ToInt32(chars_codice_fiscale[characterPosition]) - 65]; continue; } throw new ArgumentException("Invalid character in argument " + codice_fiscale); } else { if (Char.IsNumber(chars_codice_fiscale[characterPosition])) { somma += Int32.Parse(chars_codice_fiscale[characterPosition].ToString()) ; continue; } if (Char.IsLetter(chars_codice_fiscale[characterPosition])) { somma += Convert.ToInt32(chars_codice_fiscale[characterPosition]) - 65; continue; } throw new ArgumentException("Invalid character in argument " + codice_fiscale); } Math.DivRem(somma, 26, out resto); return char.ConvertFromUtf32(resto+65); }
nibbles&bits
December 20, 2007, December 20, 2007 21:13, permalink
for the sake of readability, i'm more of a lookup man myself.
it may not be as performant, but it's sure easier to read, and the ultimate addition line is much more understandable/simple.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// just for the switch Dictionary<char,int> dict = new Dictionary<char,int>(26); dict.Add('A',1); dict.Add('B',0); dict.Add('C',5); dict.Add('D',7); dict.Add('E',9); dict.Add('F',13); dict.Add('G',15); dict.Add('H',17); dict.Add('I',19); dict.Add('J',21); dict.Add('K',2); dict.Add('L',4); dict.Add('M',18); dict.Add('N',20); dict.Add('O',11); dict.Add('P',3); dict.Add('Q',6); dict.Add('R',8); dict.Add('S',12); dict.Add('T',14); dict.Add('U',16); dict.Add('V',10); dict.Add('W',22); dict.Add('X',25); dict.Add('Y',24); dict.Add('Z',23); dict.Add('0',1); dict.Add('1',0); dict.Add('2',5); dict.Add('3',7); dict.Add('4',9); dict.Add('5',13); dict.Add('6',15); dict.Add('7',17); dict.Add('8',19); dict.Add('9',21); // then instead of the big switch, use this: somma += dict[codice_fiscale[i]];
orip
January 18, 2008, January 18, 2008 18:51, permalink
nibbles&bits: I also think a dictionary captures the key->value mapping better than a switch or an array, but the verbose syntax for initializing it takes some readability away.
C# 3.0 syntax is nice, though:
using dictionary initializers
1 2 3 4 5 6 7 8
Dictionary<char,int> dict = new Dictionary<char,int> { {'A', 1}, {'B', 0}, {'C', 5}, //... {'8', 19}, {'9', 21} };
orip
January 18, 2008, January 18, 2008 18:55, permalink
I liked Jonathan Starr's approach, here's my variation.
[cs]
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 29 30 31 32 33 34 35 36 37
private int TableIndex(char c) { if (Char.IsLetter(c)) { return (int)Char.ToUpper(c) - (int)'A'; } else if (Char.IsNumber(c)) { return 26 + int.Parse(c.ToString()); } throw new ArgumentException("Invalid character"); } private string CodiceControllo2(string codice_fiscale) { int[] table_even = new int[] {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,1,0,5,7,9,13,15,17,19,21}; int[] table_odd = new int[] {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,0,1,2,3,4,5,6,7,8,9}; int somma = 0; for (int i = 0; i < 15; i++) { char c = codice_fiscale[i]; if (i % 2 == 0) { somma += table_even[TableIndex(c)]; } else { somma += table_odd[TableIndex(c)]; } } int resto = somma % 26; return char.ConvertFromUtf32(resto + 65); }
Hi, I am new on c#...
there is a way to simplify this code....
thanks